test_alias_options.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from flask import url_for
  2. from app.extensions import db
  3. from app.models import User, ApiKey, AliasUsedOn, Alias
  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 len(r.json["custom"]["suffixes"]) == 4
  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 = Alias.create_new(user, prefix="test")
  34. db.session.commit()
  35. AliasUsedOn.create(alias_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"
  43. def test_different_scenarios_v2(flask_client):
  44. user = User.create(
  45. email="a@b.c", password="password", name="Test User", activated=True
  46. )
  47. db.session.commit()
  48. # create api_key
  49. api_key = ApiKey.create(user.id, "for test")
  50. db.session.commit()
  51. # <<< without hostname >>>
  52. r = flask_client.get(
  53. url_for("api.options_v2"), headers={"Authentication": api_key.code}
  54. )
  55. assert r.status_code == 200
  56. # {'can_create': True, 'existing': ['my-first-alias.chat@sl.local'], 'prefix_suggestion': '', 'suffixes': ['.meo@sl.local']}
  57. assert r.json["can_create"]
  58. assert len(r.json["existing"]) == 1
  59. assert r.json["suffixes"]
  60. assert r.json["prefix_suggestion"] == "" # no hostname => no suggestion
  61. # <<< with hostname >>>
  62. r = flask_client.get(
  63. url_for("api.options_v2", hostname="www.test.com"),
  64. headers={"Authentication": api_key.code},
  65. )
  66. assert r.json["prefix_suggestion"] == "test"
  67. # <<< with recommendation >>>
  68. alias = Alias.create_new(user, prefix="test")
  69. db.session.commit()
  70. AliasUsedOn.create(alias_id=alias.id, hostname="www.test.com")
  71. db.session.commit()
  72. r = flask_client.get(
  73. url_for("api.options_v2", hostname="www.test.com"),
  74. headers={"Authentication": api_key.code},
  75. )
  76. assert r.json["recommendation"]["alias"] == alias.email
  77. assert r.json["recommendation"]["hostname"] == "www.test.com"