diff --git a/app/api/__init__.py b/app/api/__init__.py index a98f8182..42a7cf19 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -1,6 +1,7 @@ from .views import ( alias_options, new_custom_alias, + custom_domain, new_random_alias, user_info, auth, diff --git a/app/api/views/custom_domain.py b/app/api/views/custom_domain.py new file mode 100644 index 00000000..98bfe5bd --- /dev/null +++ b/app/api/views/custom_domain.py @@ -0,0 +1,22 @@ +from flask import g +from flask import jsonify + +from app.api.base import api_bp, require_api_auth +from app.models import CustomDomain + + +def custom_domain_to_dict(custom_domain: CustomDomain): + return { + "id": custom_domain.id, + "domain": custom_domain.domain, + "verified": custom_domain.verified, + } + + +@api_bp.route("/custom_domains", methods=["GET"]) +@require_api_auth +def get_custom_domains(): + user = g.user + custom_domains = CustomDomain.filter_by(user_id=user.id).all() + + return jsonify(custom_domains=[custom_domain_to_dict(cd) for cd in custom_domains]) diff --git a/docs/api.md b/docs/api.md index 8f145b25..69a29ec8 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1,18 +1,20 @@ ## API -[1. Account endpoints](#account-endpoints) +[Account endpoints](#account-endpoints) -[2. Alias endpoints](#alias-endpoints) +[Alias endpoints](#alias-endpoints) -[3. Mailbox endpoints](#mailbox-endpoints) +[Mailbox endpoints](#mailbox-endpoints) -[4. Contact endpoints](#contact-endpoints) +[Custom domain endpoints](#custom-domain-endpoints) -[5. Notification endpoints](#notification-endpoints) +[Contact endpoints](#contact-endpoints) -[6. Settings endpoints](#settings-endpoints) +[Notification endpoints](#notification-endpoints) -[7. MISC endpoints](#misc-endpoints) +[Settings endpoints](#settings-endpoints) + +[MISC endpoints](#misc-endpoints) --- @@ -619,6 +621,34 @@ Output: - 200 if updated successfully - 400 if error +### Custom domain endpoints + +#### GET /api/custom_domains + +Return user's custom domains + +Input: +- `Authentication` header that contains the api key + +Output: +List of custom domains. + +```json +{ + "custom_domains": [ + { + "domain": "test1.org", + "id": 1, + "verified": true + }, + { + "domain": "test2.org", + "id": 2, + "verified": false + } + ] +} +``` ### Contact endpoints diff --git a/tests/api/test_custom_domain.py b/tests/api/test_custom_domain.py new file mode 100644 index 00000000..1a4cf680 --- /dev/null +++ b/tests/api/test_custom_domain.py @@ -0,0 +1,25 @@ +import json + +from app.models import CustomDomain +from tests.utils import login + + +def test_get_custom_domains(flask_client): + user = login(flask_client) + + CustomDomain.create(user_id=user.id, domain="test1.org", verified=True, commit=True) + CustomDomain.create( + user_id=user.id, domain="test2.org", verified=False, commit=True + ) + + r = flask_client.get( + "/api/custom_domains", + ) + + assert r.status_code == 200 + assert r.json == { + "custom_domains": [ + {"domain": "test1.org", "id": 1, "verified": True}, + {"domain": "test2.org", "id": 2, "verified": False}, + ] + }