test_new_custom_alias.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. from flask import url_for
  2. from app.alias_utils import delete_alias
  3. from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
  4. from app.dashboard.views.custom_alias import signer
  5. from app.extensions import db
  6. from app.models import User, ApiKey, Alias, CustomDomain, Mailbox
  7. from app.utils import random_word
  8. def test_success(flask_client):
  9. user = User.create(
  10. email="a@b.c", password="password", name="Test User", activated=True
  11. )
  12. db.session.commit()
  13. # create api_key
  14. api_key = ApiKey.create(user.id, "for test")
  15. db.session.commit()
  16. # create new alias with note
  17. word = random_word()
  18. r = flask_client.post(
  19. url_for("api.new_custom_alias", hostname="www.test.com"),
  20. headers={"Authentication": api_key.code},
  21. json={
  22. "alias_prefix": "prefix",
  23. "alias_suffix": f".{word}@{EMAIL_DOMAIN}",
  24. "note": "test note",
  25. },
  26. )
  27. assert r.status_code == 201
  28. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  29. # assert returned field
  30. res = r.json
  31. assert "id" in res
  32. assert "email" in res
  33. assert "creation_date" in res
  34. assert "creation_timestamp" in res
  35. assert "nb_forward" in res
  36. assert "nb_block" in res
  37. assert "nb_reply" in res
  38. assert "enabled" in res
  39. assert "note" in res
  40. new_ge = Alias.get_by(email=r.json["alias"])
  41. assert new_ge.note == "test note"
  42. def test_create_custom_alias_without_note(flask_client):
  43. user = User.create(
  44. email="a@b.c", password="password", name="Test User", activated=True
  45. )
  46. db.session.commit()
  47. # create api_key
  48. api_key = ApiKey.create(user.id, "for test")
  49. db.session.commit()
  50. # create alias without note
  51. word = random_word()
  52. r = flask_client.post(
  53. url_for("api.new_custom_alias", hostname="www.test.com"),
  54. headers={"Authentication": api_key.code},
  55. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  56. )
  57. assert r.status_code == 201
  58. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  59. new_ge = Alias.get_by(email=r.json["alias"])
  60. assert new_ge.note is None
  61. def test_out_of_quota(flask_client):
  62. user = User.create(
  63. email="a@b.c", password="password", name="Test User", activated=True
  64. )
  65. user.trial_end = None
  66. db.session.commit()
  67. # create api_key
  68. api_key = ApiKey.create(user.id, "for test")
  69. db.session.commit()
  70. # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
  71. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  72. Alias.create_new(user, prefix="test")
  73. word = random_word()
  74. r = flask_client.post(
  75. url_for("api.new_custom_alias", hostname="www.test.com"),
  76. headers={"Authentication": api_key.code},
  77. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  78. )
  79. assert r.status_code == 400
  80. assert r.json == {
  81. "error": "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  82. }
  83. def test_success_v2(flask_client):
  84. user = User.create(
  85. email="a@b.c", password="password", name="Test User", activated=True
  86. )
  87. db.session.commit()
  88. # create api_key
  89. api_key = ApiKey.create(user.id, "for test")
  90. db.session.commit()
  91. # create new alias with note
  92. word = random_word()
  93. suffix = f".{word}@{EMAIL_DOMAIN}"
  94. suffix = signer.sign(suffix).decode()
  95. r = flask_client.post(
  96. url_for("api.new_custom_alias_v2", hostname="www.test.com"),
  97. headers={"Authentication": api_key.code},
  98. json={"alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note",},
  99. )
  100. assert r.status_code == 201
  101. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  102. # assert returned field
  103. res = r.json
  104. assert "id" in res
  105. assert "email" in res
  106. assert "creation_date" in res
  107. assert "creation_timestamp" in res
  108. assert "nb_forward" in res
  109. assert "nb_block" in res
  110. assert "nb_reply" in res
  111. assert "enabled" in res
  112. assert "note" in res
  113. new_ge = Alias.get_by(email=r.json["alias"])
  114. assert new_ge.note == "test note"
  115. def test_cannot_create_alias_in_trash(flask_client):
  116. user = User.create(
  117. email="a@b.c", password="password", name="Test User", activated=True
  118. )
  119. db.session.commit()
  120. # create api_key
  121. api_key = ApiKey.create(user.id, "for test")
  122. db.session.commit()
  123. # create a custom domain
  124. CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True)
  125. db.session.commit()
  126. # create new alias with note
  127. suffix = f"@ab.cd"
  128. suffix = signer.sign(suffix).decode()
  129. r = flask_client.post(
  130. url_for("api.new_custom_alias_v2", hostname="www.test.com"),
  131. headers={"Authentication": api_key.code},
  132. json={"alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note",},
  133. )
  134. # assert alias creation is successful
  135. assert r.status_code == 201
  136. assert r.json["alias"] == f"prefix@ab.cd"
  137. # delete alias: it's going to be moved to ab.cd trash
  138. alias = Alias.get_by(email="prefix@ab.cd")
  139. assert alias.custom_domain_id
  140. delete_alias(alias, user)
  141. # try to create the same alias, will fail as the alias is in trash
  142. r = flask_client.post(
  143. url_for("api.new_custom_alias_v2", hostname="www.test.com"),
  144. headers={"Authentication": api_key.code},
  145. json={"alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note",},
  146. )
  147. assert r.status_code == 409
  148. def test_success_v3(flask_client):
  149. user = User.create(
  150. email="a@b.c", password="password", name="Test User", activated=True,
  151. )
  152. db.session.commit()
  153. # create api_key
  154. api_key = ApiKey.create(user.id, "for test")
  155. db.session.commit()
  156. # create another mailbox
  157. mb = Mailbox.create(user_id=user.id, email="abcd@gmail.com", verified=True)
  158. db.session.commit()
  159. # create new alias with note
  160. word = random_word()
  161. suffix = f".{word}@{EMAIL_DOMAIN}"
  162. suffix = signer.sign(suffix).decode()
  163. r = flask_client.post(
  164. url_for("api.new_custom_alias_v3", hostname="www.test.com"),
  165. headers={"Authentication": api_key.code},
  166. json={
  167. "alias_prefix": "prefix",
  168. "signed_suffix": suffix,
  169. "note": "test note",
  170. "mailbox_ids": [user.default_mailbox_id, mb.id],
  171. "name": "your name",
  172. },
  173. )
  174. assert r.status_code == 201
  175. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  176. # assert returned field
  177. res = r.json
  178. assert "id" in res
  179. assert "email" in res
  180. assert "creation_date" in res
  181. assert "creation_timestamp" in res
  182. assert "nb_forward" in res
  183. assert "nb_block" in res
  184. assert "nb_reply" in res
  185. assert "enabled" in res
  186. assert "note" in res
  187. assert res["name"] == "your name"
  188. new_alias: Alias = Alias.get_by(email=r.json["alias"])
  189. assert new_alias.note == "test note"
  190. assert len(new_alias.mailboxes) == 2