test_alias_options.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from flask import url_for
  2. from app.extensions import db
  3. from app.models import User, ApiKey, AliasUsedOn, GenEmail
  4. def test_different_scenarios(flask_client):
  5. user = User.create(
  6. email="a@b.c", password="password", name="Test User", activated=True
  7. )
  8. db.session.commit()
  9. # create api_key
  10. api_key = ApiKey.create(user.id, "for test")
  11. db.session.commit()
  12. # <<< without hostname >>>
  13. r = flask_client.get(
  14. url_for("api.options"), headers={"Authentication": api_key.code}
  15. )
  16. # {
  17. # "can_create_custom": True,
  18. # "custom": {"suffixes": ["azdwbw@sl.local"], "suggestion": ""},
  19. # "existing": ["cat_cat_cat@sl.local"],
  20. # }
  21. assert r.status_code == 200
  22. assert r.json["can_create_custom"]
  23. assert len(r.json["existing"]) == 1
  24. assert r.json["custom"]["suffixes"]
  25. assert r.json["custom"]["suggestion"] == "" # no hostname => no suggestion
  26. # <<< with hostname >>>
  27. r = flask_client.get(
  28. url_for("api.options", hostname="www.test.com"),
  29. headers={"Authentication": api_key.code},
  30. )
  31. assert r.json["custom"]["suggestion"] == "test"
  32. # <<< with recommendation >>>
  33. alias = GenEmail.create_new(user.id, prefix="test")
  34. db.session.commit()
  35. AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com")
  36. db.session.commit()
  37. r = flask_client.get(
  38. url_for("api.options", hostname="www.test.com"),
  39. headers={"Authentication": api_key.code},
  40. )
  41. assert r.json["recommendation"]["alias"] == alias.email
  42. assert r.json["recommendation"]["hostname"] == "www.test.com"