test_new_random_alias.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import uuid
  2. from flask import url_for
  3. from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
  4. from app.extensions import db
  5. from app.models import User, ApiKey, GenEmail
  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. r = flask_client.post(
  15. url_for("api.new_random_alias", hostname="www.test.com"),
  16. headers={"Authentication": api_key.code},
  17. )
  18. assert r.status_code == 201
  19. assert r.json["alias"].endswith(EMAIL_DOMAIN)
  20. def test_custom_mode(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. # without note
  29. r = flask_client.post(
  30. url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"),
  31. headers={"Authentication": api_key.code},
  32. )
  33. assert r.status_code == 201
  34. # extract the uuid part
  35. alias = r.json["alias"]
  36. uuid_part = alias[: len(alias) - len(EMAIL_DOMAIN) - 1]
  37. assert is_valid_uuid(uuid_part)
  38. # with note
  39. r = flask_client.post(
  40. url_for("api.new_random_alias", hostname="www.test.com", mode="uuid"),
  41. headers={"Authentication": api_key.code},
  42. json={"note": "test note"},
  43. )
  44. assert r.status_code == 201
  45. alias = r.json["alias"]
  46. ge = GenEmail.get_by(email=alias)
  47. assert ge.note == "test note"
  48. def test_out_of_quota(flask_client):
  49. user = User.create(
  50. email="a@b.c", password="password", name="Test User", activated=True
  51. )
  52. user.trial_end = None
  53. db.session.commit()
  54. # create api_key
  55. api_key = ApiKey.create(user.id, "for test")
  56. db.session.commit()
  57. # create MAX_NB_EMAIL_FREE_PLAN random alias to run out of quota
  58. for _ in range(MAX_NB_EMAIL_FREE_PLAN):
  59. GenEmail.create_new(user, prefix="test1")
  60. r = flask_client.post(
  61. url_for("api.new_random_alias", hostname="www.test.com"),
  62. headers={"Authentication": api_key.code},
  63. )
  64. assert r.status_code == 400
  65. assert (
  66. r.json["error"]
  67. == "You have reached the limitation of a free account with the maximum of 3 aliases, please upgrade your plan to create more aliases"
  68. )
  69. def is_valid_uuid(val):
  70. try:
  71. uuid.UUID(str(val))
  72. return True
  73. except ValueError:
  74. return False