From 3b454b9a80fc31d1d63bbfff08517dcf23f9fec2 Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 5 Mar 2020 20:32:08 +0100 Subject: [PATCH] fix test --- app/models.py | 19 ++++++++++--------- server.py | 2 +- tests/api/test_alias_options.py | 6 +++--- tests/api/test_new_custom_alias.py | 2 +- tests/api/test_new_random_alias.py | 2 +- tests/test.env | 2 +- tests/test_models.py | 2 +- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/models.py b/app/models.py index 83e6867e..849ee6a3 100644 --- a/app/models.py +++ b/app/models.py @@ -154,14 +154,14 @@ class User(db.Model, ModelMixin, UserMixin): db.session.flush() - # create a first alias mail to show user how to use when they login - GenEmail.create_new(user.id, prefix="my-first-alias") - db.session.flush() - mb = Mailbox.create(user_id=user.id, email=user.email, verified=True) db.session.flush() user.default_mailbox_id = mb.id + # create a first alias mail to show user how to use when they login + GenEmail.create_new(user, prefix="my-first-alias", mailbox_id=mb.id) + db.session.flush() + # Schedule onboarding emails Job.create( name=JOB_ONBOARDING_1, @@ -272,9 +272,7 @@ class User(db.Model, ModelMixin, UserMixin): all_gen_emails = [ge.email for ge in GenEmail.filter_by(user_id=self.id)] if self.can_create_new_alias(): - suggested_gen_email = GenEmail.create_new( - self.id, prefix=website_name - ).email + suggested_gen_email = GenEmail.create_new(self, prefix=website_name).email else: # pick an email from the list of gen emails suggested_gen_email = random.choice(all_gen_emails) @@ -553,7 +551,7 @@ class GenEmail(db.Model, ModelMixin): mailbox = db.relationship("Mailbox") @classmethod - def create_new(cls, user_id, prefix, note=None, mailbox_id=None): + def create_new(cls, user, prefix, note=None, mailbox_id=None): if not prefix: raise Exception("alias prefix cannot be empty") @@ -566,7 +564,10 @@ class GenEmail(db.Model, ModelMixin): break return GenEmail.create( - user_id=user_id, email=email, note=note, mailbox_id=mailbox_id + user_id=user.id, + email=email, + note=note, + mailbox_id=mailbox_id or user.default_mailbox_id, ) @classmethod diff --git a/server.py b/server.py index f57adeb5..663fb8f9 100644 --- a/server.py +++ b/server.py @@ -160,7 +160,7 @@ def fake_data(): user.default_mailbox_id = m1.id - GenEmail.create_new(user.id, "e1@", mailbox_id=m1.id) + GenEmail.create_new(user, "e1@", mailbox_id=m1.id) CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True) CustomDomain.create( diff --git a/tests/api/test_alias_options.py b/tests/api/test_alias_options.py index 47b34ab7..ecf7251c 100644 --- a/tests/api/test_alias_options.py +++ b/tests/api/test_alias_options.py @@ -27,7 +27,7 @@ def test_different_scenarios(flask_client): assert r.status_code == 200 assert r.json["can_create_custom"] assert len(r.json["existing"]) == 1 - assert len(r.json["custom"]["suffixes"]) == 3 + assert len(r.json["custom"]["suffixes"]) == 4 assert r.json["custom"]["suggestion"] == "" # no hostname => no suggestion @@ -40,7 +40,7 @@ def test_different_scenarios(flask_client): assert r.json["custom"]["suggestion"] == "test" # <<< with recommendation >>> - alias = GenEmail.create_new(user.id, prefix="test") + alias = GenEmail.create_new(user, prefix="test") db.session.commit() AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com") db.session.commit() @@ -85,7 +85,7 @@ def test_different_scenarios_v2(flask_client): assert r.json["prefix_suggestion"] == "test" # <<< with recommendation >>> - alias = GenEmail.create_new(user.id, prefix="test") + alias = GenEmail.create_new(user, prefix="test") db.session.commit() AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com") db.session.commit() diff --git a/tests/api/test_new_custom_alias.py b/tests/api/test_new_custom_alias.py index 3d75d612..40172ba2 100644 --- a/tests/api/test_new_custom_alias.py +++ b/tests/api/test_new_custom_alias.py @@ -41,7 +41,7 @@ def test_out_of_quota(flask_client): # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota for _ in range(MAX_NB_EMAIL_FREE_PLAN): - GenEmail.create_new(user.id, prefix="test") + GenEmail.create_new(user, prefix="test") word = random_word() r = flask_client.post( diff --git a/tests/api/test_new_random_alias.py b/tests/api/test_new_random_alias.py index 8cbbaaa6..eeb54a90 100644 --- a/tests/api/test_new_random_alias.py +++ b/tests/api/test_new_random_alias.py @@ -61,7 +61,7 @@ def test_out_of_quota(flask_client): # create MAX_NB_EMAIL_FREE_PLAN random alias to run out of quota for _ in range(MAX_NB_EMAIL_FREE_PLAN): - GenEmail.create_new(user.id, prefix="test1") + GenEmail.create_new(user, prefix="test1") r = flask_client.post( url_for("api.new_random_alias", hostname="www.test.com"), diff --git a/tests/test.env b/tests/test.env index 7835aae4..94ccee4c 100644 --- a/tests/test.env +++ b/tests/test.env @@ -5,7 +5,7 @@ URL=http://localhost # Only print email content, not sending it NOT_SEND_EMAIL=true EMAIL_DOMAIN=sl.local -OTHER_ALIAS_DOMAINS=["d1.test", "d2.test"] +OTHER_ALIAS_DOMAINS=["d1.test", "d2.test", "sl.local"] SUPPORT_EMAIL=support@sl.local ADMIN_EMAIL=to_fill # Max number emails user can generate for free plan diff --git a/tests/test_models.py b/tests/test_models.py index ce1186d5..e8c54b61 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -42,7 +42,7 @@ def test_suggested_emails_for_user_who_cannot_create_new_alias(flask_client): # make sure user runs out of quota to create new email for i in range(MAX_NB_EMAIL_FREE_PLAN): - GenEmail.create_new(user_id=user.id, prefix="test") + GenEmail.create_new(user=user, prefix="test") db.session.commit() suggested_email, other_emails = user.suggested_emails(website_name="test")