Take into account query in GET /api/aliases

This commit is contained in:
Son NK 2020-03-17 19:32:45 +01:00 committed by Son NK
parent 54a30a62d2
commit 2d570f7290
3 changed files with 42 additions and 4 deletions

View file

@ -819,6 +819,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:

View file

@ -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(

View file

@ -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