2019-12-04 07:48:30 +08:00
|
|
|
from flask import url_for
|
|
|
|
|
2021-10-12 20:36:47 +08:00
|
|
|
from app.db import Session
|
2020-03-17 18:51:40 +08:00
|
|
|
from app.models import User, ApiKey, AliasUsedOn, Alias
|
2019-12-04 07:48:30 +08:00
|
|
|
|
|
|
|
|
2021-06-05 23:48:41 +08:00
|
|
|
def test_different_scenarios_v4(flask_client):
|
2020-10-20 23:32:01 +08:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-10-20 23:32:01 +08:00
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-10-20 23:32:01 +08:00
|
|
|
|
|
|
|
# <<< without hostname >>>
|
|
|
|
r = flask_client.get(
|
2021-06-05 23:48:41 +08:00
|
|
|
"/api/v4/alias/options", headers={"Authentication": api_key.code}
|
2020-10-20 23:32:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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(
|
2021-06-18 05:54:14 +08:00
|
|
|
url_for("api.options_v4", hostname="www.test.com"),
|
2020-10-20 23:32:01 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.json["prefix_suggestion"] == "test"
|
|
|
|
|
|
|
|
# <<< with recommendation >>>
|
|
|
|
alias = Alias.create_new(user, prefix="test")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-10-20 23:32:01 +08:00
|
|
|
AliasUsedOn.create(
|
|
|
|
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
|
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-10-20 23:32:01 +08:00
|
|
|
|
|
|
|
r = flask_client.get(
|
2021-06-18 05:54:14 +08:00
|
|
|
url_for("api.options_v4", hostname="www.test.com"),
|
2020-10-20 23:32:01 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["recommendation"]["alias"] == alias.email
|
|
|
|
assert r.json["recommendation"]["hostname"] == "www.test.com"
|
|
|
|
|
|
|
|
|
2021-06-18 05:24:07 +08:00
|
|
|
def test_different_scenarios_v4_2(flask_client):
|
2020-05-02 22:21:18 +08:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-05-02 22:21:18 +08:00
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-05-02 22:21:18 +08:00
|
|
|
|
|
|
|
# <<< 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")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-05-02 22:21:18 +08:00
|
|
|
AliasUsedOn.create(
|
|
|
|
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
|
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-05-02 22:21:18 +08:00
|
|
|
|
|
|
|
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"
|
2020-11-14 23:45:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_different_scenarios_v5(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-11-14 23:45:22 +08:00
|
|
|
|
|
|
|
# create api_key
|
|
|
|
api_key = ApiKey.create(user.id, "for test")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-11-14 23:45:22 +08:00
|
|
|
|
|
|
|
# <<< without hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
"/api/v5/alias/options", 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_payload in r.json["suffixes"]:
|
|
|
|
suffix, signed_suffix = (
|
|
|
|
suffix_payload["suffix"],
|
|
|
|
suffix_payload["signed_suffix"],
|
|
|
|
)
|
|
|
|
assert signed_suffix.startswith(suffix)
|
|
|
|
|
|
|
|
# <<< with hostname >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
"/api/v5/alias/options?hostname=www.test.com",
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["prefix_suggestion"] == "test"
|
|
|
|
|
2021-10-18 17:45:48 +08:00
|
|
|
# <<< with hostname with 2 parts TLD, for example wwww.numberoneshoes.co.nz >>>
|
|
|
|
r = flask_client.get(
|
|
|
|
"/api/v5/alias/options?hostname=wwww.numberoneshoes.co.nz",
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
assert r.json["prefix_suggestion"] == "numberoneshoes"
|
|
|
|
|
2020-11-14 23:45:22 +08:00
|
|
|
# <<< with recommendation >>>
|
|
|
|
alias = Alias.create_new(user, prefix="test")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-11-14 23:45:22 +08:00
|
|
|
AliasUsedOn.create(
|
|
|
|
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
|
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-11-14 23:45:22 +08:00
|
|
|
|
|
|
|
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"
|