make sure to set mailbox_id when creating GenEmail

This commit is contained in:
Son NK 2020-03-05 17:03:07 +01:00
parent faa82e7b5a
commit a6f4059d5d
9 changed files with 34 additions and 23 deletions

View file

@ -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:

View file

@ -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")

View file

@ -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")

View file

@ -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

View file

@ -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:

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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)