From 78b24623af6666f5e04f09133f7a2ceafa085a75 Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 26 Mar 2020 19:35:44 +0100 Subject: [PATCH 1/4] reformat: create serialize_alias_info() --- app/api/views/alias.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 52dfa7e6..74e616f1 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -57,25 +57,26 @@ def get_aliases(): return ( jsonify( - aliases=[ - { - "id": alias_info.id, - "email": alias_info.alias.email, - "creation_date": alias_info.alias.created_at.format(), - "creation_timestamp": alias_info.alias.created_at.timestamp, - "nb_forward": alias_info.nb_forward, - "nb_block": alias_info.nb_blocked, - "nb_reply": alias_info.nb_reply, - "enabled": alias_info.alias.enabled, - "note": alias_info.note, - } - for alias_info in alias_infos - ] + aliases=[serialize_alias_info(alias_info) for alias_info in alias_infos] ), 200, ) +def serialize_alias_info(alias_info: AliasInfo) -> dict: + return { + "id": alias_info.id, + "email": alias_info.alias.email, + "creation_date": alias_info.alias.created_at.format(), + "creation_timestamp": alias_info.alias.created_at.timestamp, + "nb_forward": alias_info.nb_forward, + "nb_block": alias_info.nb_blocked, + "nb_reply": alias_info.nb_reply, + "enabled": alias_info.alias.enabled, + "note": alias_info.note, + } + + @api_bp.route("/aliases/", methods=["DELETE"]) @cross_origin() @verify_api_key From 159aa76aae4b727d66a8804a309c88d54d4bd8d0 Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 26 Mar 2020 19:44:00 +0100 Subject: [PATCH 2/4] GET /api/aliases/:alias_id --- README.md | 25 +++++++++++++++++++++++++ app/api/views/alias.py | 22 ++++++++++++++++++++++ tests/api/test_alias.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/README.md b/README.md index 0222566d..4c489754 100644 --- a/README.md +++ b/README.md @@ -861,6 +861,31 @@ If success, 200 with the list of aliases, for example: } ``` +#### GET /api/aliases/:alias_id + +Get alias info + +Input: +- `Authentication` header that contains the api key +- `alias_id` in url + +Output: +Alias info + +```json +{ + "creation_date": "2020-02-04 16:23:02+00:00", + "creation_timestamp": 1580833382, + "email": "e3@.alo@sl.local", + "id": 4, + "nb_block": 0, + "nb_forward": 0, + "nb_reply": 0, + "enabled": true, + "note": "This is a note" +} +``` + #### DELETE /api/aliases/:alias_id Delete an alias diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 74e616f1..25a18de3 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -12,6 +12,7 @@ from app.dashboard.views.alias_log import get_alias_log from app.dashboard.views.index import ( AliasInfo, get_alias_infos_with_pagination, + get_alias_info, ) from app.extensions import db from app.log import LOG @@ -210,6 +211,27 @@ def update_alias(alias_id): return jsonify(note=new_note), 200 +@api_bp.route("/aliases/", methods=["GET"]) +@cross_origin() +@verify_api_key +def get_alias(alias_id): + """ + Get alias + Input: + alias_id: in url + Output: + Alias info, same as in get_aliases + + """ + user = g.user + alias: Alias = Alias.get(alias_id) + + if alias.user_id != user.id: + return jsonify(error="Forbidden"), 403 + + return jsonify(**serialize_alias_info(get_alias_info(alias))), 200 + + def serialize_contact(fe: Contact) -> dict: res = { diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index d5ee3fa2..6f9e821f 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -331,3 +331,37 @@ def test_delete_contact(flask_client): assert r.status_code == 200 assert r.json == {"deleted": True} + + +def test_get_alias(flask_client): + user = User.create( + email="a@b.c", password="password", name="Test User", activated=True + ) + db.session.commit() + + # create api_key + api_key = ApiKey.create(user.id, "for test") + db.session.commit() + + # create more aliases than PAGE_LIMIT + alias = Alias.create_new_random(user) + db.session.commit() + + # get aliases on the 1st page, should return PAGE_LIMIT aliases + r = flask_client.get( + url_for("api.get_alias", alias_id=alias.id), + headers={"Authentication": api_key.code}, + ) + assert r.status_code == 200 + + # assert returned field + res = r.json + assert "id" in res + assert "email" in res + assert "creation_date" in res + assert "creation_timestamp" in res + assert "nb_forward" in res + assert "nb_block" in res + assert "nb_reply" in res + assert "enabled" in res + assert "note" in res From d4a32451c16df8931de0669cbce32a6ddace990a Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 26 Mar 2020 19:48:36 +0100 Subject: [PATCH 3/4] return full alias info in POST /api/alias/custom/new --- README.md | 11 ++++++++++- app/api/views/new_custom_alias.py | 4 +++- tests/api/test_new_custom_alias.py | 12 ++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4c489754..4803319e 100644 --- a/README.md +++ b/README.md @@ -714,7 +714,16 @@ If success, 201 with the new alias, for example ```json { - "alias": "www_groupon_com@my_domain.com" + "alias": "www_groupon_com@my_domain.com", + "creation_date": "2020-02-04 16:23:02+00:00", + "creation_timestamp": 1580833382, + "email": "www_groupon_com@my_domain.com", + "id": 4, + "nb_block": 0, + "nb_forward": 0, + "nb_reply": 0, + "enabled": true, + "note": "This is a note" } ``` diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py index c097bc1a..2f52fb45 100644 --- a/app/api/views/new_custom_alias.py +++ b/app/api/views/new_custom_alias.py @@ -3,8 +3,10 @@ from flask import jsonify, request from flask_cors import cross_origin from app.api.base import api_bp, verify_api_key +from app.api.views.alias import serialize_alias_info from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_DOMAINS from app.dashboard.views.custom_alias import verify_prefix_suffix +from app.dashboard.views.index import get_alias_info from app.extensions import db from app.log import LOG from app.models import Alias, AliasUsedOn, User, CustomDomain @@ -75,4 +77,4 @@ def new_custom_alias(): AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id) db.session.commit() - return jsonify(alias=full_alias), 201 + return jsonify(alias=full_alias, **serialize_alias_info(get_alias_info(alias))), 201 diff --git a/tests/api/test_new_custom_alias.py b/tests/api/test_new_custom_alias.py index 6e6db5b0..bf3432a6 100644 --- a/tests/api/test_new_custom_alias.py +++ b/tests/api/test_new_custom_alias.py @@ -31,6 +31,18 @@ def test_success(flask_client): assert r.status_code == 201 assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" + # assert returned field + res = r.json + assert "id" in res + assert "email" in res + assert "creation_date" in res + assert "creation_timestamp" in res + assert "nb_forward" in res + assert "nb_block" in res + assert "nb_reply" in res + assert "enabled" in res + assert "note" in res + new_ge = Alias.get_by(email=r.json["alias"]) assert new_ge.note == "test note" From 97dff83453f007584a81c8b3313b282f671554b3 Mon Sep 17 00:00:00 2001 From: Son NK Date: Thu, 26 Mar 2020 19:50:22 +0100 Subject: [PATCH 4/4] return full alias info in POST /api/alias/random/new --- README.md | 11 ++++++++++- app/api/views/new_random_alias.py | 7 ++++++- tests/api/test_new_random_alias.py | 12 ++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4803319e..384c7a02 100644 --- a/README.md +++ b/README.md @@ -743,7 +743,16 @@ If success, 201 with the new alias, for example ```json { - "alias": "www_groupon_com@my_domain.com" + "alias": "prefix.suffix@my_domain.com", + "creation_date": "2020-02-04 16:23:02+00:00", + "creation_timestamp": 1580833382, + "email": "www_groupon_com@my_domain.com", + "id": 4, + "nb_block": 0, + "nb_forward": 0, + "nb_reply": 0, + "enabled": true, + "note": "This is a note" } ``` diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index 62dd175a..a29d1bd1 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -3,7 +3,9 @@ from flask import jsonify, request from flask_cors import cross_origin from app.api.base import api_bp, verify_api_key +from app.api.views.alias import serialize_alias_info from app.config import MAX_NB_EMAIL_FREE_PLAN +from app.dashboard.views.index import get_alias_info from app.extensions import db from app.log import LOG from app.models import Alias, AliasUsedOn, AliasGeneratorEnum @@ -55,4 +57,7 @@ def new_random_alias(): AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id) db.session.commit() - return jsonify(alias=alias.email), 201 + return ( + jsonify(alias=alias.email, **serialize_alias_info(get_alias_info(alias))), + 201, + ) diff --git a/tests/api/test_new_random_alias.py b/tests/api/test_new_random_alias.py index f2d8c287..5f94e1f0 100644 --- a/tests/api/test_new_random_alias.py +++ b/tests/api/test_new_random_alias.py @@ -25,6 +25,18 @@ def test_success(flask_client): assert r.status_code == 201 assert r.json["alias"].endswith(EMAIL_DOMAIN) + # assert returned field + res = r.json + assert "id" in res + assert "email" in res + assert "creation_date" in res + assert "creation_timestamp" in res + assert "nb_forward" in res + assert "nb_block" in res + assert "nb_reply" in res + assert "enabled" in res + assert "note" in res + def test_custom_mode(flask_client): user = User.create(