test_new_random_alias.py 1.4 KB

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