test_new_random_alias.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. db.session.commit()
  24. # create api_key
  25. api_key = ApiKey.create(user.id, "for test")
  26. db.session.commit()
  27. # create 3 random alias to run out of quota
  28. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  29. GenEmail.create_new(user.id, prefix="test1")
  30. GenEmail.create_new(user.id, prefix="test2")
  31. GenEmail.create_new(user.id, prefix="test3")
  32. r = flask_client.post(
  33. url_for("api.new_random_alias", hostname="www.test.com"),
  34. headers={"Authentication": api_key.code},
  35. )
  36. assert r.status_code == 400
  37. assert (
  38. r.json["error"]
  39. == "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  40. )