test_new_custom_alias.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from flask import url_for
  2. from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
  3. from app.extensions import db
  4. from app.models import User, ApiKey, GenEmail
  5. from app.utils import random_word
  6. def test_success(flask_client):
  7. user = User.create(
  8. email="a@b.c", password="password", name="Test User", activated=True
  9. )
  10. db.session.commit()
  11. # create api_key
  12. api_key = ApiKey.create(user.id, "for test")
  13. db.session.commit()
  14. # create new alias with note
  15. word = random_word()
  16. r = flask_client.post(
  17. url_for("api.new_custom_alias", hostname="www.test.com"),
  18. headers={"Authentication": api_key.code},
  19. json={
  20. "alias_prefix": "prefix",
  21. "alias_suffix": f".{word}@{EMAIL_DOMAIN}",
  22. "note": "test note",
  23. },
  24. )
  25. assert r.status_code == 201
  26. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  27. new_ge = GenEmail.get_by(email=r.json["alias"])
  28. assert new_ge.note == "test note"
  29. def test_create_custom_alias_without_note(flask_client):
  30. user = User.create(
  31. email="a@b.c", password="password", name="Test User", activated=True
  32. )
  33. db.session.commit()
  34. # create api_key
  35. api_key = ApiKey.create(user.id, "for test")
  36. db.session.commit()
  37. # create alias without note
  38. word = random_word()
  39. r = flask_client.post(
  40. url_for("api.new_custom_alias", hostname="www.test.com"),
  41. headers={"Authentication": api_key.code},
  42. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  43. )
  44. assert r.status_code == 201
  45. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  46. new_ge = GenEmail.get_by(email=r.json["alias"])
  47. assert new_ge.note is None
  48. def test_out_of_quota(flask_client):
  49. user = User.create(
  50. email="a@b.c", password="password", name="Test User", activated=True
  51. )
  52. user.trial_end = None
  53. db.session.commit()
  54. # create api_key
  55. api_key = ApiKey.create(user.id, "for test")
  56. db.session.commit()
  57. # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
  58. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  59. GenEmail.create_new(user, prefix="test")
  60. word = random_word()
  61. r = flask_client.post(
  62. url_for("api.new_custom_alias", hostname="www.test.com"),
  63. headers={"Authentication": api_key.code},
  64. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  65. )
  66. assert r.status_code == 400
  67. assert r.json == {
  68. "error": "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  69. }