Son NK hace 5 años
padre
commit
3b454b9a80

+ 10 - 9
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

+ 1 - 1
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(

+ 3 - 3
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()

+ 1 - 1
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(

+ 1 - 1
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"),

+ 1 - 1
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

+ 1 - 1
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")