diff --git a/app/api/views/setting.py b/app/api/views/setting.py index 33eb3a27..ded7bd2f 100644 --- a/app/api/views/setting.py +++ b/app/api/views/setting.py @@ -10,6 +10,7 @@ from app.models import ( SLDomain, CustomDomain, SenderFormatEnum, + AliasSuffixEnum, ) @@ -21,6 +22,7 @@ def setting_to_dict(user: User): else "uuid", "random_alias_default_domain": user.default_random_alias_domain(), "sender_format": SenderFormatEnum.get_name(user.sender_format), + "random_alias_suffix": AliasSuffixEnum.get_name(user.random_alias_suffix), } return ret @@ -71,6 +73,13 @@ def update_setting(): user.sender_format = SenderFormatEnum.get_value(sender_format) user.sender_format_updated_at = arrow.now() + if "random_alias_suffix" in data: + random_alias_suffix = data["random_alias_suffix"] + if not AliasSuffixEnum.has_name(random_alias_suffix): + return jsonify(error="Invalid random_alias_suffix"), 400 + + user.random_alias_suffix = AliasSuffixEnum.get_value(random_alias_suffix) + if "random_alias_default_domain" in data: default_domain = data["random_alias_default_domain"] sl_domain: SLDomain = SLDomain.get_by(domain=default_domain) diff --git a/docs/api.md b/docs/api.md index 352cf050..519ac3f0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -717,16 +717,16 @@ List of deleted alias. Delete a contact Input: + - `Authentication` header that contains the api key - `contact_id` in url. Output: If success, 200. - ```json { - "deleted": true + "deleted": true } ``` @@ -735,6 +735,7 @@ If success, 200. Block/unblock contact Input: + - `Authentication` header that contains the api key - `contact_id` in url. @@ -743,20 +744,23 @@ If success, 200 along with the new alias status: ```json { - "block_forward": false + "block_forward": false } ``` ### Notification endpoints + #### GET /api/notifications Get notifications Input: + - `Authentication` in header: the api key - page in url: the page number, starts at 0 Output: + - more: whether there's more notifications - notifications: list of notification, each notification has: - id @@ -768,15 +772,15 @@ For example ```json { - "more": false, - "notifications": [ - { - "created_at": "2 minutes ago", - "id": 1, - "message": "Hey!", - "read": false - } - ] + "more": false, + "notifications": [ + { + "created_at": "2 minutes ago", + "id": 1, + "message": "Hey!", + "read": false + } + ] } ``` @@ -785,24 +789,26 @@ For example Mark a notification as read Input: + - `Authentication` in header: the api key - notification_id in url: the page number, starts at 0 -Output: +Output: 200 if success ### Settings endpoints #### GET /api/setting -Return user setting. +Return user setting. ```json { "alias_generator": "word", "notification": true, "random_alias_default_domain": "sl.local", - "sender_format": "VIA" + "sender_format": "AT", + "random_alias_suffix": "random_string" } ``` @@ -811,10 +817,12 @@ Return user setting. Update user setting. All input fields are optional. Input: -- alias_generator (string): uuid or word -- notification (boolean): true or false + +- alias_generator (string): `uuid` or `word` +- notification (boolean): `true` or `false` - random_alias_default_domain (string): one of the domains returned by `GET /api/setting/domains` -- sender_format (string): possible values are AT, VIA, A, FULL +- sender_format (string): possible values are `AT`, `A`, `NAME_ONLY`, `AT_ONLY`, `NO_NAME` +- random_alias_suffix (string): possible values are `word`, `random_string` Output: same as `GET /api/setting` diff --git a/tests/api/test_setting.py b/tests/api/test_setting.py index d88ebd83..51e1dc4a 100644 --- a/tests/api/test_setting.py +++ b/tests/api/test_setting.py @@ -1,4 +1,9 @@ -from app.models import CustomDomain, AliasGeneratorEnum, SenderFormatEnum +from app.models import ( + CustomDomain, + AliasGeneratorEnum, + SenderFormatEnum, + AliasSuffixEnum, +) from tests.utils import login @@ -12,6 +17,7 @@ def test_get_setting(flask_client): "notification": True, "random_alias_default_domain": "sl.local", "sender_format": "AT", + "random_alias_suffix": "random_string", } @@ -63,6 +69,10 @@ def test_update_settings_sender_format(flask_client): assert r.status_code == 200 assert user.sender_format == SenderFormatEnum.A.value + r = flask_client.patch("/api/setting", json={"sender_format": "NAME_ONLY"}) + assert r.status_code == 200 + assert user.sender_format == SenderFormatEnum.NAME_ONLY.value + def test_get_setting_domains(flask_client): user = login(flask_client) @@ -78,3 +88,16 @@ def test_get_setting_domains_v2(flask_client): r = flask_client.get("/api/v2/setting/domains") assert r.status_code == 200 + + +def test_update_settings_random_alias_suffix(flask_client): + user = login(flask_client) + # default random_alias_suffix is random_string + assert user.random_alias_suffix == AliasSuffixEnum.random_string.value + + r = flask_client.patch("/api/setting", json={"random_alias_suffix": "invalid"}) + assert r.status_code == 400 + + r = flask_client.patch("/api/setting", json={"random_alias_suffix": "word"}) + assert r.status_code == 200 + assert user.random_alias_suffix == AliasSuffixEnum.word.value