test_new_custom_alias.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. user.trial_end = None
  27. db.session.commit()
  28. # create api_key
  29. api_key = ApiKey.create(user.id, "for test")
  30. db.session.commit()
  31. # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota
  32. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  33. GenEmail.create_new(user, prefix="test")
  34. word = random_word()
  35. r = flask_client.post(
  36. url_for("api.new_custom_alias", hostname="www.test.com"),
  37. headers={"Authentication": api_key.code},
  38. json={"alias_prefix": "prefix", "alias_suffix": f".{word}@{EMAIL_DOMAIN}"},
  39. )
  40. assert r.status_code == 400
  41. assert r.json == {
  42. "error": "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  43. }