From f69c9583fb92e332d9f1b6b4852b28ed998ba8b5 Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Thu, 1 Sep 2022 14:59:16 +0200 Subject: [PATCH] fix proton partner error when self host (#1255) * fix proton partner error when self host * fix test * fix test * remove a@b.c --- app/api/views/user_info.py | 11 ++++-- tests/api/test_alias.py | 46 +++-------------------- tests/api/test_user_info.py | 1 + tests/handler/test_provider_complaints.py | 3 +- 4 files changed, 16 insertions(+), 45 deletions(-) diff --git a/app/api/views/user_info.py b/app/api/views/user_info.py index e97600bc..335e1846 100644 --- a/app/api/views/user_info.py +++ b/app/api/views/user_info.py @@ -1,17 +1,17 @@ import base64 from io import BytesIO +from typing import Optional from flask import jsonify, g, request, make_response from flask_login import logout_user -from typing import Optional -from app import s3 +from app import s3, config from app.api.base import api_bp, require_api_auth from app.config import SESSION_COOKIE_NAME from app.db import Session from app.models import ApiKey, File, PartnerUser, User -from app.utils import random_string from app.proton.utils import get_proton_partner +from app.utils import random_string def get_connected_proton_address(user: User) -> Optional[str]: @@ -29,9 +29,12 @@ def user_to_dict(user: User) -> dict: "email": user.email, "in_trial": user.in_trial(), "max_alias_free_plan": user.max_alias_for_free_account(), - "connected_proton_address": get_connected_proton_address(user), + "connected_proton_address": None, } + if config.CONNECT_WITH_PROTON: + ret["connected_proton_address"] = get_connected_proton_address(user) + if user.profile_picture_id: ret["profile_picture_url"] = user.profile_picture.get_url() else: diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index b66e5e65..65e47c40 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -1,27 +1,17 @@ -from flask import url_for import arrow +from flask import url_for # Need to import directly from config to allow modification from the tests from app import config from app.db import Session from app.email_utils import is_reverse_alias -from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox +from app.models import User, Alias, Contact, EmailLog, Mailbox from tests.api.utils import get_new_user_and_api_key from tests.utils import login, random_domain def test_get_aliases_error_without_pagination(flask_client): - user = User.create( - email="a@b.c", - password="password", - name="Test User", - activated=True, - commit=True, - ) - - # create api_key - api_key = ApiKey.create(user.id, "for test") - Session.commit() + user, api_key = get_new_user_and_api_key() r = flask_client.get( url_for("api.get_aliases"), headers={"Authentication": api_key.code} @@ -32,17 +22,7 @@ def test_get_aliases_error_without_pagination(flask_client): def test_get_aliases_with_pagination(flask_client): - user = User.create( - email="a@b.c", - password="password", - name="Test User", - activated=True, - commit=True, - ) - - # create api_key - api_key = ApiKey.create(user.id, "for test") - Session.commit() + user, api_key = get_new_user_and_api_key() # create more aliases than config.PAGE_LIMIT for _ in range(config.PAGE_LIMIT + 1): @@ -79,14 +59,7 @@ def test_get_aliases_with_pagination(flask_client): def test_get_aliases_query(flask_client): - user = User.create( - email="a@b.c", password="password", name="Test User", activated=True - ) - Session.commit() - - # create api_key - api_key = ApiKey.create(user.id, "for test") - Session.commit() + user, api_key = get_new_user_and_api_key() # create more aliases than config.PAGE_LIMIT Alias.create_new(user, "prefix1") @@ -491,14 +464,7 @@ def test_alias_contacts(flask_client): def test_create_contact_route(flask_client): - user = User.create( - email="a@b.c", password="password", name="Test User", activated=True - ) - Session.commit() - - # create api_key - api_key = ApiKey.create(user.id, "for test") - Session.commit() + user, api_key = get_new_user_and_api_key() alias = Alias.create_new_random(user) Session.commit() diff --git a/tests/api/test_user_info.py b/tests/api/test_user_info.py index 8a06a169..7b67f297 100644 --- a/tests/api/test_user_info.py +++ b/tests/api/test_user_info.py @@ -27,6 +27,7 @@ def test_user_in_trial(flask_client): def test_user_linked_to_proton(flask_client): + config.CONNECT_WITH_PROTON = True user, api_key = get_new_user_and_api_key() partner = get_proton_partner() partner_email = random_email() diff --git a/tests/handler/test_provider_complaints.py b/tests/handler/test_provider_complaints.py index 7f95249f..e5b8c89c 100644 --- a/tests/handler/test_provider_complaints.py +++ b/tests/handler/test_provider_complaints.py @@ -1,3 +1,4 @@ +import random from email.message import Message import pytest @@ -36,7 +37,7 @@ def prepare_complaint( contact = Contact.create( user_id=alias.user.id, alias_id=alias.id, - website_email="a@b.c", + website_email=f"contact{random.random()}@mailbox.test", reply_email="d@e.f", commit=True, )