test_new_random_alias.py 2.2 KB

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