2019-12-04 07:48:30 +08:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
from app.extensions import db
|
2020-03-17 18:51:40 +08:00
|
|
|
from app.models import User, ApiKey, AliasUsedOn, Alias
|
2019-12-04 07:48:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_different_scenarios(flask_client):
|
2019-12-08 02:04:42 +08:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2019-12-04 07:48:30 +08: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,
|
2019-12-05 03:46:19 +08:00
|
|
|
# "custom": {"suffixes": ["azdwbw@sl.local"], "suggestion": ""},
|
2019-12-04 07:48:30 +08:00
|
|
|
# "existing": ["cat_cat_cat@sl.local"],
|
|
|
|
# }
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.json["can_create_custom"]
|
|
|
|
assert len(r.json["existing"]) == 1
|
2020-03-06 03:32:08 +08:00
|
|
|
assert len(r.json["custom"]["suffixes"]) == 4
|
2020-01-22 17:22:59 +08:00
|
|
|
|
2019-12-04 07:48:30 +08: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 17:38:35 +08:00
|
|
|
assert r.json["custom"]["suggestion"] == "test"
|
2019-12-04 07:48:30 +08:00
|
|
|
|
|
|
|
# <<< with recommendation >>>
|
2020-03-17 18:51:40 +08:00
|
|
|
alias = Alias.create_new(user, prefix="test")
|
2019-12-04 07:48:30 +08:00
|
|
|
db.session.commit()
|
2020-03-20 19:29:11 +08:00
|
|
|
AliasUsedOn.create(alias_id=alias.id, hostname="www.test.com", user_id=user.id)
|
2019-12-04 07:48:30 +08:00
|
|
|
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"
|
2020-01-06 03:47:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_different_scenarios_v2(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
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_v2"), headers={"Authentication": api_key.code}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
# {'can_create': True, 'existing': ['my-first-alias.chat@sl.local'], 'prefix_suggestion': '', 'suffixes': ['.meo@sl.local']}
|
|
|
|
|
|
|
|
assert r.json["can_create"]
|
|
|
|
assert len(r.json["existing"]) == 1
|
|
|
|
assert r.json["suffixes"]
|
|
|
|
assert r.json["prefix_suggestion"] == "" # no hostname => no suggestion
|
|
|
|
|
|
|
|
# <<< with hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options_v2", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.json["prefix_suggestion"] == "test"
|
|
|
|
|
|
|
|
# <<< with recommendation >>>
|
2020-03-17 18:51:40 +08:00
|
|
|
alias = Alias.create_new(user, prefix="test")
|
2020-01-06 03:47:09 +08:00
|
|
|
db.session.commit()
|
2020-03-20 19:29:11 +08:00
|
|
|
AliasUsedOn.create(
|
|
|
|
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
|
|
|
|
)
|
2020-01-06 03:47:09 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options_v2", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["recommendation"]["alias"] == alias.email
|
|
|
|
assert r.json["recommendation"]["hostname"] == "www.test.com"
|
2020-03-30 02:25:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_different_scenarios_v3(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
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_v3"), headers={"Authentication": api_key.code}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
assert r.json["can_create"]
|
|
|
|
assert r.json["suffixes"]
|
|
|
|
assert r.json["prefix_suggestion"] == "" # no hostname => no suggestion
|
|
|
|
|
|
|
|
# <<< with hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options_v3", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.json["prefix_suggestion"] == "test"
|
|
|
|
|
|
|
|
# <<< with recommendation >>>
|
|
|
|
alias = Alias.create_new(user, prefix="test")
|
|
|
|
db.session.commit()
|
|
|
|
AliasUsedOn.create(
|
|
|
|
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options_v3", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["recommendation"]["alias"] == alias.email
|
|
|
|
assert r.json["recommendation"]["hostname"] == "www.test.com"
|
2020-05-02 22:21:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_different_scenarios_v4(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
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_v4"), headers={"Authentication": api_key.code}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
assert r.json["can_create"]
|
|
|
|
assert r.json["suffixes"]
|
|
|
|
assert r.json["prefix_suggestion"] == "" # no hostname => no suggestion
|
|
|
|
|
|
|
|
for (suffix, signed_suffix) in r.json["suffixes"]:
|
|
|
|
assert signed_suffix.startswith(suffix)
|
|
|
|
|
|
|
|
# <<< with hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options_v4", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.json["prefix_suggestion"] == "test"
|
|
|
|
|
|
|
|
# <<< with recommendation >>>
|
|
|
|
alias = Alias.create_new(user, prefix="test")
|
|
|
|
db.session.commit()
|
|
|
|
AliasUsedOn.create(
|
|
|
|
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.options_v4", hostname="www.test.com"),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["recommendation"]["alias"] == alias.email
|
|
|
|
assert r.json["recommendation"]["hostname"] == "www.test.com"
|