implement update profile endpoint

This commit is contained in:
weeman 2021-06-20 20:13:19 +02:00
parent 5c21e4c9b6
commit ace51851de
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
4 changed files with 68 additions and 3 deletions

View File

@ -74,6 +74,16 @@ curl -s \
http://localhost:5000/users/login | jq 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 \ curl -s \
-D "/dev/stderr" \ -D "/dev/stderr" \

View File

@ -22,7 +22,7 @@ def auth(username, password):
user = User.query.filter(User.auth_id.__eq__(username)).first() user = User.query.filter(User.auth_id.__eq__(username)).first()
if user is None: if user is None:
user = User(nickname=username, auth_id=username) user = User(auth_id=username)
db.session.add(user) db.session.add(user)
token = Token(token=str(uuid.uuid4()), user=user) token = Token(token=str(uuid.uuid4()), user=user)

View File

@ -3,8 +3,8 @@ from flask import g, make_response, request, send_file
from functools import wraps from functools import wraps
from ki.auth import auth from ki.auth import auth
from ki.models import Language, Skill, Token, User from ki.models import Language, Skill, Token, User, Profile
from app import app from app import app, db
def token_auth(func): def token_auth(func):
@ -118,6 +118,28 @@ def get_user_profile(user_id):
return make_response({"profile": profile.to_dict()}) return make_response({"profile": profile.to_dict()})
@app.route("/users/<user_id>/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") @app.route("/skills")
def get_skills(): def get_skills():
return handle_completion_request(Skill, "skills") return handle_completion_request(Skill, "skills")

View File

@ -10,6 +10,7 @@ from ki.models import Profile, User
class TestProfileEndpoint(unittest.TestCase): class TestProfileEndpoint(unittest.TestCase):
def setUp(self): def setUp(self):
app.debug = True app.debug = True
app.config["TESTING"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
self.client = app.test_client() self.client = app.test_client()
@ -19,6 +20,38 @@ class TestProfileEndpoint(unittest.TestCase):
seed() 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): def test_get_profile(self):
user = User(auth_id="peter") user = User(auth_id="peter")
db.session.add(user) db.session.add(user)