diff --git a/README.md b/README.md index d36a5dc0..7bb2b6e3 100644 --- a/README.md +++ b/README.md @@ -802,6 +802,7 @@ Get user aliases. Input: - `Authentication` header that contains the api key - `page_id` used for the pagination. The endpoint returns maximum 20 aliases for each page. `page_id` starts at 0. +- (Optional) query: included in request body. Some frameworks might prevent GET request having a non-empty body, in this case this endpoint also supports POST. Output: If success, 200 with the list of aliases, for example: diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 233e725a..04802f9d 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -17,7 +17,7 @@ from app.models import Alias, Contact from app.utils import random_string -@api_bp.route("/aliases") +@api_bp.route("/aliases", methods=["GET", "POST"]) @cross_origin() @verify_api_key def get_aliases(): @@ -43,7 +43,12 @@ def get_aliases(): except (ValueError, TypeError): return jsonify(error="page_id must be provided in request query"), 400 - alias_infos: [AliasInfo] = get_alias_info(user, page_id=page_id) + query = None + data = request.get_json(silent=True) + if data: + query = data.get("query") + + alias_infos: [AliasInfo] = get_alias_info(user, page_id=page_id, query=query) return ( jsonify( diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index 285641f7..7b067eac 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -8,7 +8,7 @@ from app.models import User, ApiKey, Alias, Contact, EmailLog from app.utils import random_word -def test_error_without_pagination(flask_client): +def test_get_aliases_error_without_pagination(flask_client): user = User.create( email="a@b.c", password="password", name="Test User", activated=True ) @@ -26,7 +26,7 @@ def test_error_without_pagination(flask_client): assert r.json["error"] -def test_success_with_pagination(flask_client): +def test_get_aliases_with_pagination(flask_client): user = User.create( email="a@b.c", password="password", name="Test User", activated=True ) @@ -70,6 +70,38 @@ def test_success_with_pagination(flask_client): assert len(r.json["aliases"]) == 2 +def test_get_aliases_with_pagination(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.create_new(user, "prefix1") + Alias.create_new(user, "prefix2") + db.session.commit() + + # get aliases without query, should return 3 aliases as one alias is created when user is created + r = flask_client.get( + url_for("api.get_aliases", page_id=0), headers={"Authentication": api_key.code} + ) + assert r.status_code == 200 + assert len(r.json["aliases"]) == 3 + + # get aliases with "prefix1" query, should return 1 alias + r = flask_client.get( + url_for("api.get_aliases", page_id=0), + headers={"Authentication": api_key.code}, + json={"query": "prefix1"}, + ) + assert r.status_code == 200 + assert len(r.json["aliases"]) == 1 + + def test_delete_alias(flask_client): user = User.create( email="a@b.c", password="password", name="Test User", activated=True