diff --git a/README.md b/README.md index a2c37df2..66e46207 100644 --- a/README.md +++ b/README.md @@ -442,6 +442,26 @@ Some errors should be fixed during development however: for example error like ` All following endpoint return `401` status code if the API Key is incorrect. +#### GET /api/user_info + +Given the API Key, return user name and whether user is premium. +This endpoint could be used to validate the api key. + +Input: +- `Authentication` header that contains the api key + +Output: if api key is correct, return a json with user name and whether user is premium, for example: + +```json +{ + "name": "John Wick", + "is_premium": false +} +``` + +If api key is incorrect, return 401. + + #### GET /api/v2/alias/options User alias info and suggestion. Used by the first extension screen when user opens the extension. diff --git a/app/api/__init__.py b/app/api/__init__.py index 612893b0..268191e9 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -1 +1 @@ -from .views import alias_options, new_custom_alias, new_random_alias +from .views import alias_options, new_custom_alias, new_random_alias, user_info diff --git a/app/api/views/user_info.py b/app/api/views/user_info.py new file mode 100644 index 00000000..e8a2dd94 --- /dev/null +++ b/app/api/views/user_info.py @@ -0,0 +1,22 @@ +from flask import jsonify, request, g +from flask_cors import cross_origin +from sqlalchemy import desc + +from app.api.base import api_bp, verify_api_key +from app.config import EMAIL_DOMAIN +from app.extensions import db +from app.log import LOG +from app.models import AliasUsedOn, GenEmail, User +from app.utils import convert_to_id, random_word + + +@api_bp.route("/user_info") +@cross_origin() +@verify_api_key +def user_info(): + """ + Return user info given the api-key + """ + user = g.user + + return jsonify({"name": user.name, "is_premium": user.is_premium()}) diff --git a/tests/api/test_user_info.py b/tests/api/test_user_info.py new file mode 100644 index 00000000..ffa96edf --- /dev/null +++ b/tests/api/test_user_info.py @@ -0,0 +1,32 @@ +from flask import url_for + +from app.extensions import db +from app.models import User, ApiKey, AliasUsedOn, GenEmail + + +def test_success(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() + + r = flask_client.get( + url_for("api.user_info"), headers={"Authentication": api_key.code} + ) + + assert r.status_code == 200 + assert r.json == {"is_premium": False, "name": "Test User"} + + +def test_wrong_api_key(flask_client): + r = flask_client.get( + url_for("api.user_info"), headers={"Authentication": "Invalid code"} + ) + + assert r.status_code == 401 + + assert r.json == {"error": "Wrong api key"}