From aee917a3efaedc5c23aa789749f5174d7d7fabc9 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sun, 15 Nov 2020 19:24:54 +0100 Subject: [PATCH] Add GET /api/custom_domains/:custom_domain_id/trash --- app/api/views/custom_domain.py | 25 ++++++++++++++++++++++++- docs/api.md | 21 +++++++++++++++++++++ tests/api/test_custom_domain.py | 29 ++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/app/api/views/custom_domain.py b/app/api/views/custom_domain.py index ae1873a7..95497392 100644 --- a/app/api/views/custom_domain.py +++ b/app/api/views/custom_domain.py @@ -2,7 +2,7 @@ from flask import g from flask import jsonify from app.api.base import api_bp, require_api_auth -from app.models import CustomDomain +from app.models import CustomDomain, DomainDeletedAlias def custom_domain_to_dict(custom_domain: CustomDomain): @@ -21,3 +21,26 @@ def get_custom_domains(): custom_domains = CustomDomain.filter_by(user_id=user.id).all() return jsonify(custom_domains=[custom_domain_to_dict(cd) for cd in custom_domains]) + + +@api_bp.route("/custom_domains//trash", methods=["GET"]) +@require_api_auth +def get_custom_domain_trash(custom_domain_id: int): + user = g.user + custom_domain = CustomDomain.get(custom_domain_id) + if not custom_domain or custom_domain.user_id != user.id: + return jsonify(error="Forbidden"), 403 + + domain_deleted_aliases = DomainDeletedAlias.filter_by( + domain_id=custom_domain.id + ).all() + + return jsonify( + aliases=[ + { + "alias": dda.email, + "creation_timestamp": dda.created_at.timestamp, + } + for dda in domain_deleted_aliases + ] + ) diff --git a/docs/api.md b/docs/api.md index b9c38ba9..93c6ce48 100644 --- a/docs/api.md +++ b/docs/api.md @@ -652,6 +652,27 @@ List of custom domains. } ``` +#### GET /api/custom_domains/:custom_domain_id/trash + +Get deleted alias for a custom domain + +Input: +- `Authentication` header that contains the api key + +Output: +List of deleted alias. + +```json +{ + "aliases": [ + { + "alias": "first@test1.org", + "creation_timestamp": 1605464595 + } + ] +} +``` + ### Contact endpoints #### DELETE /api/contacts/:contact_id diff --git a/tests/api/test_custom_domain.py b/tests/api/test_custom_domain.py index a254544e..6ce5cf6c 100644 --- a/tests/api/test_custom_domain.py +++ b/tests/api/test_custom_domain.py @@ -1,6 +1,7 @@ import json -from app.models import CustomDomain +from app.alias_utils import delete_alias +from app.models import CustomDomain, DomainDeletedAlias, Alias from tests.utils import login @@ -23,3 +24,29 @@ def test_get_custom_domains(flask_client): {"domain": "test2.org", "id": 2, "nb_alias": 0, "verified": False}, ] } + + +def test_get_custom_domain_trash(flask_client): + user = login(flask_client) + + cd = CustomDomain.create( + user_id=user.id, domain="test1.org", verified=True, commit=True + ) + + alias = Alias.create( + user_id=user.id, + email="first@test1.org", + custom_domain_id=cd.id, + mailbox_id=user.default_mailbox_id, + commit=True, + ) + + delete_alias(alias, user) + + r = flask_client.get( + f"/api/custom_domains/{cd.id}/trash", + ) + + for deleted_alias in r.json["aliases"]: + assert deleted_alias["alias"] + assert deleted_alias["creation_timestamp"] > 0