2019-12-04 00:48:30 +01:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
from app.models import User, ApiKey, AliasUsedOn, GenEmail
|
|
|
|
|
|
|
|
|
|
|
|
def test_different_scenarios(flask_client):
|
2019-12-07 19:04:42 +01:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2019-12-04 00:48:30 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# <<< without hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options"), headers={"Authentication": api_key.code}
|
|
|
|
)
|
|
|
|
|
|
|
|
# {
|
|
|
|
# "can_create_custom": True,
|
|
|
|
# "can_create_random": True,
|
2019-12-04 20:46:19 +01:00
|
|
|
# "custom": {"suffixes": ["azdwbw@sl.local"], "suggestion": ""},
|
2019-12-04 00:48:30 +01:00
|
|
|
# "existing": ["cat_cat_cat@sl.local"],
|
|
|
|
# }
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json["can_create_custom"]
|
|
|
|
assert r.json["can_create_random"]
|
|
|
|
assert len(r.json["existing"]) == 1
|
2019-12-04 20:46:19 +01:00
|
|
|
assert r.json["custom"]["suffixes"]
|
2019-12-04 00:48:30 +01:00
|
|
|
assert r.json["custom"]["suggestion"] == "" # no hostname => no suggestion
|
|
|
|
|
|
|
|
# <<< with hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
2019-12-06 10:38:35 +01:00
|
|
|
assert r.json["custom"]["suggestion"] == "test"
|
2019-12-04 00:48:30 +01:00
|
|
|
|
|
|
|
# <<< with recommendation >>>
|
2019-12-08 17:01:24 +01:00
|
|
|
alias = GenEmail.create_custom_alias(user.id, prefix="test")
|
2019-12-04 00:48:30 +01:00
|
|
|
db.session.commit()
|
|
|
|
AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com")
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["recommendation"]["alias"] == alias.email
|
|
|
|
assert r.json["recommendation"]["hostname"] == "www.test.com"
|