From debed67c68726197c676dccf26a0a3e03fbb79c6 Mon Sep 17 00:00:00 2001 From: Son Date: Thu, 14 Apr 2022 17:28:40 +0200 Subject: [PATCH 1/3] return whether a domain is custom or primary in GET /api/v5/alias/options --- app/api/views/alias_options.py | 11 +++++++++-- docs/api.md | 15 +++++++++++---- tests/api/test_alias_options.py | 2 ++ tests/test.env | 6 +++--- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/api/views/alias_options.py b/app/api/views/alias_options.py index 1dc40d20..9f998258 100644 --- a/app/api/views/alias_options.py +++ b/app/api/views/alias_options.py @@ -90,7 +90,9 @@ def options_v5(): suffixes: [ { suffix: "suffix", - signed_suffix: "signed_suffix" + signed_suffix: "signed_suffix", + is_custom: true, + is_premium: false } ] prefix_suggestion: str @@ -141,7 +143,12 @@ def options_v5(): # custom domain should be put first ret["suffixes"] = [ - {"suffix": suffix.suffix, "signed_suffix": suffix.signed_suffix} + { + "suffix": suffix.suffix, + "signed_suffix": suffix.signed_suffix, + "is_custom": suffix.is_custom, + "is_premium": suffix.is_premium, + } for suffix in suffixes ] diff --git a/docs/api.md b/docs/api.md index 4588a54a..a5332a83 100644 --- a/docs/api.md +++ b/docs/api.md @@ -231,7 +231,8 @@ Input: Output: a json with the following field: - can_create: boolean. Whether user can create new alias -- suffixes: list of dictionary with `suffix` and `signed-suffix`. List of alias `suffix` that user can use. +- suffixes: list of alias suffix that user can use. + Each item is a dictionary with `suffix`, `signed-suffix`, `is_custom`, `is_premium` as keys. The `signed-suffix` is necessary to avoid request tampering. - prefix_suggestion: string. Suggestion for the `alias prefix`. Usually this is the website name extracted from `hostname`. If no `hostname`, then the `prefix_suggestion` is empty. @@ -248,15 +249,21 @@ For ex: "suffixes": [ { "signed_suffix": ".cat@d1.test.X6_7OQ.0e9NbZHE_bQvuAapT6NdBml9m6Q", - "suffix": ".cat@d1.test" + "suffix": ".cat@d1.test", + "is_custom": true, + "is_premium": false }, { "signed_suffix": ".chat@d2.test.X6_7OQ.TTgCrfqPj7UmlY723YsDTHhkess", - "suffix": ".chat@d2.test" + "suffix": ".chat@d2.test", + "is_custom": false, + "is_premium": false }, { "signed_suffix": ".yeah@sl.local.X6_7OQ.i8XL4xsMsn7dxDEWU8eF-Zap0qo", - "suffix": ".yeah@sl.local" + "suffix": ".yeah@sl.local", + "is_custom": true, + "is_premium": false } ] } diff --git a/tests/api/test_alias_options.py b/tests/api/test_alias_options.py index f5e32525..6bd3c037 100644 --- a/tests/api/test_alias_options.py +++ b/tests/api/test_alias_options.py @@ -124,6 +124,8 @@ def test_different_scenarios_v5(flask_client): suffix_payload["signed_suffix"], ) assert signed_suffix.startswith(suffix) + assert "is_custom" in suffix_payload + assert "is_premium" in suffix_payload # <<< with hostname >>> r = flask_client.get( diff --git a/tests/test.env b/tests/test.env index ae3dff84..5f6fc935 100644 --- a/tests/test.env +++ b/tests/test.env @@ -25,9 +25,9 @@ AWS_ACCESS_KEY_ID=to_fill AWS_SECRET_ACCESS_KEY=to_fill # Paddle -PADDLE_VENDOR_ID = 1 -PADDLE_MONTHLY_PRODUCT_ID = 2 -PADDLE_YEARLY_PRODUCT_ID = 3 +PADDLE_VENDOR_ID=1 +PADDLE_MONTHLY_PRODUCT_ID=2 +PADDLE_YEARLY_PRODUCT_ID=3 PADDLE_PUBLIC_KEY_PATH=local_data/paddle.key.pub # OpenId key From 217518c00e7afc2a850afd4f6090ce0c6853e41f Mon Sep 17 00:00:00 2001 From: Son Date: Thu, 14 Apr 2022 18:37:55 +0200 Subject: [PATCH 2/3] refactor --- tests/api/test_alias_options.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/tests/api/test_alias_options.py b/tests/api/test_alias_options.py index 6bd3c037..dfe41511 100644 --- a/tests/api/test_alias_options.py +++ b/tests/api/test_alias_options.py @@ -2,6 +2,7 @@ from flask import url_for from app.db import Session from app.models import User, ApiKey, AliasUsedOn, Alias +from tests.utils import login def test_different_scenarios_v4(flask_client): @@ -98,18 +99,11 @@ def test_different_scenarios_v4_2(flask_client): def test_different_scenarios_v5(flask_client): - user = User.create( - email="a@b.c", password="password", name="Test User", activated=True - ) - Session.commit() - - # create api_key - api_key = ApiKey.create(user.id, "for test") - Session.commit() + user = login(flask_client) # <<< without hostname >>> r = flask_client.get( - "/api/v5/alias/options", headers={"Authentication": api_key.code} + "/api/v5/alias/options" ) assert r.status_code == 200 @@ -129,15 +123,13 @@ def test_different_scenarios_v5(flask_client): # <<< with hostname >>> r = flask_client.get( - "/api/v5/alias/options?hostname=www.test.com", - headers={"Authentication": api_key.code}, + "/api/v5/alias/options?hostname=www.test.com" ) assert r.json["prefix_suggestion"] == "test" # <<< 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}, + "/api/v5/alias/options?hostname=wwww.numberoneshoes.co.nz" ) assert r.json["prefix_suggestion"] == "numberoneshoes" @@ -150,8 +142,7 @@ def test_different_scenarios_v5(flask_client): Session.commit() r = flask_client.get( - url_for("api.options_v4", hostname="www.test.com"), - headers={"Authentication": api_key.code}, + url_for("api.options_v4", hostname="www.test.com") ) assert r.json["recommendation"]["alias"] == alias.email assert r.json["recommendation"]["hostname"] == "www.test.com" From 7edbc3a5d548f27ca9577916cc8e9a01ccb9732d Mon Sep 17 00:00:00 2001 From: Son Date: Thu, 14 Apr 2022 18:53:27 +0200 Subject: [PATCH 3/3] black --- tests/api/test_alias_options.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/api/test_alias_options.py b/tests/api/test_alias_options.py index dfe41511..e55a5148 100644 --- a/tests/api/test_alias_options.py +++ b/tests/api/test_alias_options.py @@ -102,9 +102,7 @@ def test_different_scenarios_v5(flask_client): user = login(flask_client) # <<< without hostname >>> - r = flask_client.get( - "/api/v5/alias/options" - ) + r = flask_client.get("/api/v5/alias/options") assert r.status_code == 200 @@ -122,15 +120,11 @@ def test_different_scenarios_v5(flask_client): assert "is_premium" in suffix_payload # <<< with hostname >>> - r = flask_client.get( - "/api/v5/alias/options?hostname=www.test.com" - ) + r = flask_client.get("/api/v5/alias/options?hostname=www.test.com") assert r.json["prefix_suggestion"] == "test" # <<< 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" - ) + r = flask_client.get("/api/v5/alias/options?hostname=wwww.numberoneshoes.co.nz") assert r.json["prefix_suggestion"] == "numberoneshoes" # <<< with recommendation >>> @@ -141,8 +135,6 @@ def test_different_scenarios_v5(flask_client): ) Session.commit() - r = flask_client.get( - url_for("api.options_v4", hostname="www.test.com") - ) + r = flask_client.get(url_for("api.options_v4", hostname="www.test.com")) assert r.json["recommendation"]["alias"] == alias.email assert r.json["recommendation"]["hostname"] == "www.test.com"