Add PATCH /api/user_info

This commit is contained in:
Son NK 2020-10-28 17:12:21 +01:00
parent 3f40e3c1cf
commit 91534d3cf2
4 changed files with 93 additions and 0 deletions

View file

@ -852,6 +852,17 @@ Output: if api key is correct, return a json with user name and whether user is
If api key is incorrect, return 401.
#### PATCH /api/user_info
Update user info
Input:
- profile_picture: the profile picture in base64. Setting to `null` remove the current profile picture.
- name
Output: same as GET /api/user_info
#### POST /api/api_key
Create a new API Key

View file

@ -39,6 +39,43 @@ def user_info():
return jsonify(user_to_dict(user))
@api_bp.route("/user_info", methods=["PATCH"])
@require_api_auth
def update_user_info():
"""
Input
- profile_picture (optional): base64 of the profile picture. Set to null to remove the profile picture
- name (optional)
"""
user = g.user
data = request.get_json() or {}
if "profile_picture" in data:
if data["profile_picture"] is None:
if user.profile_picture_id:
file = user.profile_picture
File.delete(file.id)
s3.delete(file.path)
user.profile_picture_id = None
else:
raw_data = base64.decodebytes(data["profile_picture"].encode())
file_path = random_string(30)
file = File.create(user_id=user.id, path=file_path)
db.session.flush()
s3.upload_from_bytesio(file_path, BytesIO(raw_data))
user.profile_picture_id = file.id
db.session.flush()
if "name" in data:
user.name = data["name"]
db.session.commit()
return jsonify(user_to_dict(user))
@api_bp.route("/api_key", methods=["POST"])
@require_api_auth
def create_api_key():

View file

@ -77,3 +77,47 @@ def test_logout(flask_client):
)
assert r.status_code == 200
def test_change_profile_picture(flask_client):
user = login(flask_client)
assert not user.profile_picture_id
# <<< Set the profile picture >>>
img_base64 = """iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="""
r = flask_client.patch(
"/api/user_info",
json={"profile_picture": img_base64},
)
assert r.status_code == 200
assert r.json["profile_picture_url"] is not None
user = User.get(user.id)
assert user.profile_picture_id
# <<< remove the profile picture >>>
r = flask_client.patch(
"/api/user_info",
json={"profile_picture": None},
)
assert r.status_code == 200
assert r.json["profile_picture_url"] is None
user = User.get(user.id)
assert not user.profile_picture_id
def test_change_name(flask_client):
user = login(flask_client)
assert user.name != "new name"
r = flask_client.patch(
"/api/user_info",
json={"name": "new name"},
)
assert r.status_code == 200
assert r.json["name"] == "new name"
assert user.name == "new name"

View file

@ -1,5 +1,6 @@
# Server url
URL=http://localhost
LOCAL_FILE_UPLOAD=1
# Email related settings
# Only print email content, not sending it