From f452c79aecda9ee48025a9581e698d221db3f8ef Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 14 Nov 2020 16:37:36 +0100 Subject: [PATCH] Add /v2/setting/domains --- app/api/views/setting.py | 16 ++++++++++++++++ docs/api.md | 36 +++++++++++++++++++----------------- tests/api/test_setting.py | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/app/api/views/setting.py b/app/api/views/setting.py index 0873d7d3..bc0fefba 100644 --- a/app/api/views/setting.py +++ b/app/api/views/setting.py @@ -97,3 +97,19 @@ def get_available_domains_for_random_alias(): ] return jsonify(ret) + + +@api_bp.route("/v2/setting/domains") +@require_api_auth +def get_available_domains_for_random_alias_v2(): + """ + Available domains for random alias + """ + user = g.user + + ret = [ + {"domain": domain, "is_custom": not is_sl} + for is_sl, domain in user.available_domains_for_random_alias() + ] + + return jsonify(ret) diff --git a/docs/api.md b/docs/api.md index 2da13156..ea6db474 100644 --- a/docs/api.md +++ b/docs/api.md @@ -701,28 +701,30 @@ Input: Output: same as `GET /api/setting` -#### GET /api/setting/domains +#### GET /api/v2/setting/domains Return domains that user can use to create random alias +`is_custom` is true if this is a user's domain, otherwise false. + ```json [ - [ - true, - "d1.test" - ], - [ - true, - "d2.test" - ], - [ - true, - "sl.local" - ], - [ - false, - "ab.cd" - ] + { + "domain": "d1.test", + "is_custom": false + }, + { + "domain": "d2.test", + "is_custom": false + }, + { + "domain": "sl.local", + "is_custom": false + }, + { + "domain": "ab.cd", + "is_custom": true + } ] ``` diff --git a/tests/api/test_setting.py b/tests/api/test_setting.py index 8ccf3c93..964fe00c 100644 --- a/tests/api/test_setting.py +++ b/tests/api/test_setting.py @@ -65,3 +65,17 @@ def test_get_setting_domains(flask_client): [True, "sl.local"], [False, "ab.cd"], ] + + +def test_get_setting_domains_v2(flask_client): + user = login(flask_client) + CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True) + + r = flask_client.get("/api/v2/setting/domains") + assert r.status_code == 200 + assert r.json == [ + {"domain": "d1.test", "is_custom": False}, + {"domain": "d2.test", "is_custom": False}, + {"domain": "sl.local", "is_custom": False}, + {"domain": "ab.cd", "is_custom": True}, + ]