test_new_custom_alias.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. word = random_word()
  15. r = flask_client.post(
  16. url_for("api.new_custom_alias", hostname="www.test.com"),
  17. headers={"Authentication": api_key.code},
  18. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  19. )
  20. assert r.status_code == 201
  21. assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}"
  22. def test_out_of_quota(flask_client):
  23. user = User.create(
  24. email="a@b.c", password="password", name="Test User", activated=True
  25. )
  26. db.session.commit()
  27. # create api_key
  28. api_key = ApiKey.create(user.id, "for test")
  29. db.session.commit()
  30. # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
  31. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  32. GenEmail.create_new(user.id, prefix="test")
  33. word = random_word()
  34. r = flask_client.post(
  35. url_for("api.new_custom_alias", hostname="www.test.com"),
  36. headers={"Authentication": api_key.code},
  37. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  38. )
  39. assert r.status_code == 400
  40. assert r.json == {
  41. "error": "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  42. }