소스 검색

Add /api/user_info

Son NK 5 년 전
부모
커밋
f52f4c821b
4개의 변경된 파일75개의 추가작업 그리고 1개의 파일을 삭제
  1. 20 0
      README.md
  2. 1 1
      app/api/__init__.py
  3. 22 0
      app/api/views/user_info.py
  4. 32 0
      tests/api/test_user_info.py

+ 20 - 0
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.
 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.

+ 1 - 1
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

+ 22 - 0
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()})

+ 32 - 0
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"}