From ace51851defe9b23330c41c8b76bea2a2171e03b Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 20 Jun 2021 20:13:19 +0200 Subject: [PATCH] implement update profile endpoint --- README.md | 10 ++++++++++ ki/auth.py | 2 +- ki/routes.py | 26 +++++++++++++++++++++++-- ki/test/test_profile_endpoint.py | 33 ++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 61f7648..7a38e97 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,16 @@ curl -s \ http://localhost:5000/users/login | jq ``` +``` +curl -s \ + -D "/dev/stderr" \ + -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer 22e6c5fc-8a5a-440e-b1f4-018deb9fd24e" \ + -d '{"pronouns": "Herr Dr. Dr."}' \ + http://localhost:5000/users/1/profile | jq +``` + ``` curl -s \ -D "/dev/stderr" \ diff --git a/ki/auth.py b/ki/auth.py index 6350a5a..b5e346f 100644 --- a/ki/auth.py +++ b/ki/auth.py @@ -22,7 +22,7 @@ def auth(username, password): user = User.query.filter(User.auth_id.__eq__(username)).first() if user is None: - user = User(nickname=username, auth_id=username) + user = User(auth_id=username) db.session.add(user) token = Token(token=str(uuid.uuid4()), user=user) diff --git a/ki/routes.py b/ki/routes.py index 98dbc29..e1e7604 100644 --- a/ki/routes.py +++ b/ki/routes.py @@ -3,8 +3,8 @@ from flask import g, make_response, request, send_file from functools import wraps from ki.auth import auth -from ki.models import Language, Skill, Token, User -from app import app +from ki.models import Language, Skill, Token, User, Profile +from app import app, db def token_auth(func): @@ -118,6 +118,28 @@ def get_user_profile(user_id): return make_response({"profile": profile.to_dict()}) +@app.route("/users//profile", methods=["POST"]) +def update_profile(user_id): + user = User.query.filter(User.id == int(user_id)).first() + + if user is None: + return make_response({}, 404) + + profile = user.profile + + if (profile is None): + profile = Profile(user=user, nickname=user.auth_id) + db.session.add(profile) + + profile.pronouns = request.json.get("pronouns", "") + profile.volunteerwork = request.json.get("volunteerwork", "") + profile.freetext = request.json.get("freetext", "") + + db.session.commit() + + return make_response(profile.to_dict(), 200) + + @app.route("/skills") def get_skills(): return handle_completion_request(Skill, "skills") diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index 7dd1bec..5678a0f 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -10,6 +10,7 @@ from ki.models import Profile, User class TestProfileEndpoint(unittest.TestCase): def setUp(self): app.debug = True + app.config["TESTING"] = True app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" self.client = app.test_client() @@ -19,6 +20,38 @@ class TestProfileEndpoint(unittest.TestCase): seed() + def tearDown(self): + db.engine.dispose() + + def test_create_profile(self): + user = User(auth_id="peter") + db.session.add(user) + db.session.commit() + + login_data = {"username": "peter", "password": "geheim"} + login_response = self.client.post("/users/login", + data=json.dumps(login_data), + content_type="application/json") + + self.assertEqual(login_response.status_code, 200) + self.assertIn("token", login_response.json) + + data = { + "pronouns": "Herr Dr. Dr.", + "volunteerwork": "ja", + "freetext": "Hallo", + } + response = self.client.post("/users/1/profile", + data=json.dumps(data), + content_type="application/json", + headers={ + "Authorization": + "Bearer " + + login_response.json["token"] + }) + + self.assertEqual(response.status_code, 200) + def test_get_profile(self): user = User(auth_id="peter") db.session.add(user)