test_new_custom_alias.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. # create alias without note
  30. word = random_word()
  31. r = flask_client.post(
  32. url_for("api.new_custom_alias", hostname="www.test.com"),
  33. headers={"Authentication": api_key.code},
  34. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}",},
  35. )
  36. assert r.status_code == 201
  37. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  38. new_ge = GenEmail.get_by(email=r.json["alias"])
  39. assert new_ge.note is None
  40. def test_out_of_quota(flask_client):
  41. user = User.create(
  42. email="a@b.c", password="password", name="Test User", activated=True
  43. )
  44. user.trial_end = None
  45. db.session.commit()
  46. # create api_key
  47. api_key = ApiKey.create(user.id, "for test")
  48. db.session.commit()
  49. # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
  50. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  51. GenEmail.create_new(user, prefix="test")
  52. word = random_word()
  53. r = flask_client.post(
  54. url_for("api.new_custom_alias", hostname="www.test.com"),
  55. headers={"Authentication": api_key.code},
  56. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  57. )
  58. assert r.status_code == 400
  59. assert r.json == {
  60. "error": "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  61. }