From a6f4059d5dbaa0d1e6c89b1c5941e6d5df21db49 Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 5 Mar 2020 17:03:07 +0100 Subject: [PATCH] make sure to set mailbox_id when creating GenEmail --- app/api/views/new_custom_alias.py | 8 +++++--- app/api/views/new_random_alias.py | 2 +- app/dashboard/views/custom_alias.py | 12 ++++++------ app/dashboard/views/index.py | 4 +--- app/models.py | 6 ++++-- app/oauth/views/authorize.py | 8 ++++++-- email_handler.py | 7 ++++++- tests/api/test_alias.py | 8 ++++---- tests/test_models.py | 2 +- 9 files changed, 34 insertions(+), 23 deletions(-) diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py index e920ddf1..5e739692 100644 --- a/app/api/views/new_custom_alias.py +++ b/app/api/views/new_custom_alias.py @@ -7,7 +7,7 @@ from app.config import MAX_NB_EMAIL_FREE_PLAN from app.dashboard.views.custom_alias import verify_prefix_suffix from app.extensions import db from app.log import LOG -from app.models import GenEmail, AliasUsedOn +from app.models import GenEmail, AliasUsedOn, User from app.utils import convert_to_id @@ -26,7 +26,7 @@ def new_custom_alias(): 409 if the alias already exists """ - user = g.user + user: User = g.user if not user.can_create_new_alias(): LOG.d("user %s cannot create any custom alias", user) return ( @@ -56,7 +56,9 @@ def new_custom_alias(): LOG.d("full alias already used %s", full_alias) return jsonify(error=f"alias {full_alias} already exists"), 409 - gen_email = GenEmail.create(user_id=user.id, email=full_alias) + gen_email = GenEmail.create( + user_id=user.id, email=full_alias, mailbox_id=user.default_mailbox_id + ) db.session.commit() if hostname: diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index 15ff5d3a..5cc30d60 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -40,7 +40,7 @@ def new_random_alias(): else: return jsonify(error=f"{mode} must be either word or alias"), 400 - gen_email = GenEmail.create_new_random(user_id=user.id, scheme=scheme) + gen_email = GenEmail.create_new_random(user=user, scheme=scheme) db.session.commit() hostname = request.args.get("hostname") diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index d0928a46..2f6fc6c0 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -69,8 +69,13 @@ def custom_alias(): "warning", ) else: + mailbox = Mailbox.get_by(email=mailbox_email) + gen_email = GenEmail.create( - user_id=current_user.id, email=full_alias, note=alias_note + user_id=current_user.id, + email=full_alias, + note=alias_note, + mailbox_id=mailbox.id, ) # get the custom_domain_id if alias is created with a custom domain @@ -80,11 +85,6 @@ def custom_alias(): LOG.d("Set alias %s domain to %s", full_alias, custom_domain) gen_email.custom_domain_id = custom_domain.id - # assign alias to a mailbox - mailbox = Mailbox.get_by(email=mailbox_email) - gen_email.mailbox_id = mailbox.id - LOG.d("Set alias %s mailbox to %s", full_alias, mailbox) - db.session.commit() flash(f"Alias {full_alias} has been created", "success") diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index e65dbe65..486910b4 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -71,9 +71,7 @@ def index(): ) if not scheme or not AliasGeneratorEnum.has_value(scheme): scheme = current_user.alias_generator - gen_email = GenEmail.create_new_random( - user_id=current_user.id, scheme=scheme - ) + gen_email = GenEmail.create_new_random(user=current_user, scheme=scheme) gen_email.mailbox_id = current_user.default_mailbox_id diff --git a/app/models.py b/app/models.py index 341e27cd..83e6867e 100644 --- a/app/models.py +++ b/app/models.py @@ -571,11 +571,13 @@ class GenEmail(db.Model, ModelMixin): @classmethod def create_new_random( - cls, user_id, scheme: int = AliasGeneratorEnum.word.value, in_hex: bool = False + cls, user, scheme: int = AliasGeneratorEnum.word.value, in_hex: bool = False ): """create a new random alias""" random_email = generate_email(scheme=scheme, in_hex=in_hex) - return GenEmail.create(user_id=user_id, email=random_email) + return GenEmail.create( + user_id=user.id, email=random_email, mailbox_id=user.default_mailbox_id + ) def mailbox_email(self): if self.mailbox_id: diff --git a/app/oauth/views/authorize.py b/app/oauth/views/authorize.py index 734e50d5..a78c6e65 100644 --- a/app/oauth/views/authorize.py +++ b/app/oauth/views/authorize.py @@ -184,7 +184,9 @@ def authorize(): return redirect(request.url) else: gen_email = GenEmail.create( - user_id=current_user.id, email=full_alias + user_id=current_user.id, + email=full_alias, + mailbox_id=current_user.default_mailbox_id, ) # get the custom_domain_id if alias is created with a custom domain @@ -207,7 +209,9 @@ def authorize(): gen_email = GenEmail.get_by(email=chosen_email) if not gen_email: gen_email = GenEmail.create( - email=chosen_email, user_id=current_user.id + email=chosen_email, + user_id=current_user.id, + mailbox_id=current_user.default_mailbox_id, ) db.session.flush() diff --git a/email_handler.py b/email_handler.py index aa87be7a..6c9319f1 100644 --- a/email_handler.py +++ b/email_handler.py @@ -146,7 +146,10 @@ def try_auto_create_directory(alias: str) -> Optional[GenEmail]: LOG.d("create alias %s for directory %s", alias, directory) gen_email = GenEmail.create( - email=alias, user_id=directory.user_id, directory_id=directory.id, + email=alias, + user_id=directory.user_id, + directory_id=directory.id, + mailbox_id=dir_user.default_mailbox_id, ) db.session.commit() return gen_email @@ -191,7 +194,9 @@ def try_auto_create_catch_all_domain(alias: str) -> Optional[GenEmail]: user_id=custom_domain.user_id, custom_domain_id=custom_domain.id, automatic_creation=True, + mailbox_id=domain_user.default_mailbox_id, ) + db.session.commit() return gen_email diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index 5db8f3e9..95c937d4 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -38,7 +38,7 @@ def test_success_with_pagination(flask_client): # create more aliases than PAGE_LIMIT for _ in range(PAGE_LIMIT + 1): - GenEmail.create_new_random(user.id) + GenEmail.create_new_random(user) db.session.commit() # get aliases on the 1st page, should return PAGE_LIMIT aliases @@ -68,7 +68,7 @@ def test_delete_alias(flask_client): api_key = ApiKey.create(user.id, "for test") db.session.commit() - gen_email = GenEmail.create_new_random(user.id) + gen_email = GenEmail.create_new_random(user) db.session.commit() r = flask_client.delete( @@ -90,7 +90,7 @@ def test_toggle_alias(flask_client): api_key = ApiKey.create(user.id, "for test") db.session.commit() - gen_email = GenEmail.create_new_random(user.id) + gen_email = GenEmail.create_new_random(user) db.session.commit() r = flask_client.post( @@ -112,7 +112,7 @@ def test_alias_activities(flask_client): api_key = ApiKey.create(user.id, "for test") db.session.commit() - gen_email = GenEmail.create_new_random(user.id) + gen_email = GenEmail.create_new_random(user) db.session.commit() # create some alias log diff --git a/tests/test_models.py b/tests/test_models.py index e386c20e..ce1186d5 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -61,5 +61,5 @@ def test_gen_email_create_random(flask_client): ) db.session.commit() - alias = GenEmail.create_new_random(user.id) + alias = GenEmail.create_new_random(user) assert alias.email.endswith(EMAIL_DOMAIN)