app-MAIL-temp/tests/api/test_alias_options.py

128 lines
3.9 KiB
Python
Raw Normal View History

2019-12-04 00:48:30 +01:00
from flask import url_for
from app.db import Session
from app.models import AliasUsedOn, Alias
from tests.api.utils import get_new_user_and_api_key
2022-04-14 18:37:55 +02:00
from tests.utils import login
2019-12-04 00:48:30 +01:00
2021-06-05 17:48:41 +02:00
def test_different_scenarios_v4(flask_client):
user, api_key = get_new_user_and_api_key()
# <<< without hostname >>>
r = flask_client.get(
2021-06-05 17:48:41 +02:00
"/api/v4/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
# <<< with hostname >>>
r = flask_client.get(
2021-06-17 23:54:14 +02:00
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")
Session.commit()
AliasUsedOn.create(
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
)
Session.commit()
r = flask_client.get(
2021-06-17 23:54:14 +02:00
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"
2021-06-17 23:24:07 +02:00
def test_different_scenarios_v4_2(flask_client):
user, api_key = get_new_user_and_api_key()
2020-05-02 16:21:18 +02: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"]:
2020-05-02 16:21:18 +02:00
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")
Session.commit()
2020-05-02 16:21:18 +02:00
AliasUsedOn.create(
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
)
Session.commit()
2020-05-02 16:21:18 +02: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 16:45:22 +01:00
def test_different_scenarios_v5(flask_client):
2022-04-14 18:37:55 +02:00
user = login(flask_client)
2020-11-14 16:45:22 +01:00
# <<< without hostname >>>
2022-04-14 18:53:27 +02:00
r = flask_client.get("/api/v5/alias/options")
2020-11-14 16:45:22 +01:00
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)
assert "is_custom" in suffix_payload
assert "is_premium" in suffix_payload
2020-11-14 16:45:22 +01:00
# <<< with hostname >>>
2022-04-14 18:53:27 +02:00
r = flask_client.get("/api/v5/alias/options?hostname=www.test.com")
2020-11-14 16:45:22 +01:00
assert r.json["prefix_suggestion"] == "test"
2021-10-18 11:45:48 +02:00
# <<< with hostname with 2 parts TLD, for example wwww.numberoneshoes.co.nz >>>
2022-04-14 18:53:27 +02:00
r = flask_client.get("/api/v5/alias/options?hostname=wwww.numberoneshoes.co.nz")
2021-10-18 11:45:48 +02:00
assert r.json["prefix_suggestion"] == "numberoneshoes"
2020-11-14 16:45:22 +01:00
# <<< with recommendation >>>
alias = Alias.create_new(user, prefix="test")
Session.commit()
2020-11-14 16:45:22 +01:00
AliasUsedOn.create(
alias_id=alias.id, hostname="www.test.com", user_id=alias.user_id
)
Session.commit()
2020-11-14 16:45:22 +01:00
2022-04-14 18:53:27 +02:00
r = flask_client.get(url_for("api.options_v4", hostname="www.test.com"))
2020-11-14 16:45:22 +01:00
assert r.json["recommendation"]["alias"] == alias.email
assert r.json["recommendation"]["hostname"] == "www.test.com"