test_alias_options.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. """Start with a blank database."""
  6. # create user, user is not activated
  7. user = User.create(email="a@b.c", password="password", name="Test User")
  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. # "can_create_random": True,
  19. # "custom": {"suffixes": ["azdwbw@sl.local"], "suggestion": ""},
  20. # "existing": ["cat_cat_cat@sl.local"],
  21. # }
  22. assert r.status_code == 200
  23. assert r.json["can_create_custom"]
  24. assert r.json["can_create_random"]
  25. assert len(r.json["existing"]) == 1
  26. assert r.json["custom"]["suffixes"]
  27. assert r.json["custom"]["suggestion"] == "" # no hostname => no suggestion
  28. # <<< with hostname >>>
  29. r = flask_client.get(
  30. url_for("api.options", hostname="www.test.com"),
  31. headers={"Authentication": api_key.code},
  32. )
  33. assert r.json["custom"]["suggestion"] == "www_test_com"
  34. # <<< with recommendation >>>
  35. alias = GenEmail.create_new_gen_email(user.id)
  36. db.session.commit()
  37. AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com")
  38. db.session.commit()
  39. r = flask_client.get(
  40. url_for("api.options", hostname="www.test.com"),
  41. headers={"Authentication": api_key.code},
  42. )
  43. assert r.json["recommendation"]["alias"] == alias.email
  44. assert r.json["recommendation"]["hostname"] == "www.test.com"