test_new_custom_alias.py 1.6 KB

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