Add /api/user_info

This commit is contained in:
Son NK 2020-01-05 22:48:38 +01:00
parent 377e6c657d
commit f52f4c821b
4 changed files with 75 additions and 1 deletions

View File

@ -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. 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 #### GET /api/v2/alias/options
User alias info and suggestion. Used by the first extension screen when user opens the extension. User alias info and suggestion. Used by the first extension screen when user opens the extension.

View File

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

View File

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

View File

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