ki-backend/ki/test/test_profile_endpoint.py

304 lines
11 KiB
Python
Raw Permalink Normal View History

2021-07-05 19:37:05 +02:00
# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
2021-06-20 19:25:27 +02:00
import unittest
import json
2021-06-27 14:25:44 +02:00
from app import app
2021-06-26 10:51:39 +02:00
from ki.models import User
2021-06-27 14:25:44 +02:00
from ki.test.ApiTest import ApiTest
2021-06-20 19:25:27 +02:00
2021-06-27 14:25:44 +02:00
class TestProfileEndpoint(ApiTest):
2021-06-26 12:16:14 +02:00
maxDiff = None
2021-06-27 14:25:44 +02:00
def test_update_profile_unauthorised(self):
login_data = {"username": "klaus", "password": "jutta"}
login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
2021-06-20 19:25:27 +02:00
2021-06-27 14:25:44 +02:00
self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json)
2021-06-20 19:25:27 +02:00
2024-09-13 15:38:56 +02:00
with app.app_context():
babsi = User.query.filter(User.auth_id == "babsi1").first()
response = self.client.post(f"/users/{babsi.id}/profile",
data=json.dumps({}),
content_type="application/json",
headers={"Authorization": "Bearer " + login_response.json["token"]})
2021-06-20 19:25:27 +02:00
2021-06-27 14:25:44 +02:00
self.assertEqual(response.status_code, 403)
2021-06-20 20:13:19 +02:00
2021-06-26 12:16:14 +02:00
def test_update_profile(self):
2021-06-27 14:25:44 +02:00
token = self.login("peter", "geheim")["token"]
2021-06-20 20:13:19 +02:00
data = {
2021-11-22 20:19:23 +01:00
"nickname": "Hebbert",
2021-06-26 12:16:14 +02:00
"pronouns": "Monsieur",
2021-06-20 20:13:19 +02:00
"volunteerwork": "ja",
"availability_status": False,
"availability_text": "Nie",
"availability_hours_per_week": "23",
2021-06-20 20:13:19 +02:00
"freetext": "Hallo",
2021-07-02 16:26:36 +02:00
"visible": True,
2021-06-27 12:20:36 +02:00
"address": {
"name": "Peeeda",
"street": "Bachstraße",
"house_number": "42x",
"additional": "oben",
"postcode": "23232",
"city": "Travemünde",
"country": "Deutschland"
2021-06-27 13:07:54 +02:00
},
"contacts": [{
"id": 1,
"contacttype": {
2021-07-02 16:49:14 +02:00
"id": 4,
2021-06-27 13:07:54 +02:00
"name": "Matrix"
},
"content": "@peeda:wtf-eg.de"
}, {
"contacttype": {
"name": "Rohrpost"
},
"content": "Ausgang 2"
2021-06-27 13:38:16 +02:00
}],
"skills": [{
"id": 1,
"skill": {
"id": 3,
"name": "Python"
},
"level": 4
}, {
"skill": {
"name": "Tschunkproduktion"
},
"level": 5
2021-06-27 13:51:07 +02:00
}],
2021-07-05 20:39:49 +02:00
"searchtopics": [{
"profile_id": 1,
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
}
}, {
"profile_id": 1,
"skill": {
"name": "Assembler"
}
}],
2021-06-27 13:51:07 +02:00
"languages": [{
"id": 1,
"language": {
"id": "de",
"name": "Deutsch"
},
"level": 4
}, {
"language": {
"id": "es",
"name": "Spanisch"
},
"level": 2
2021-06-27 13:07:54 +02:00
}]
2021-06-20 20:13:19 +02:00
}
2024-09-13 15:38:56 +02:00
with app.app_context():
peter = User.query.filter(User.auth_id == "peter").first()
response = self.client.post(f"/users/{peter.id}/profile",
data=json.dumps(data),
content_type="application/json",
headers={"Authorization": "Bearer " + token})
2021-06-20 20:13:19 +02:00
self.assertEqual(response.status_code, 200)
2021-06-20 20:21:49 +02:00
with app.app_context():
user = User.query.filter(User.id == peter.id).first()
2021-06-20 20:21:49 +02:00
profile = user.profile
2021-11-22 20:19:23 +01:00
self.assertEqual("Hebbert", profile.nickname)
2021-06-26 12:16:14 +02:00
self.assertEqual("Monsieur", profile.pronouns)
2021-06-20 20:21:49 +02:00
self.assertEqual("ja", profile.volunteerwork)
self.assertEqual(False, profile.availability_status)
self.assertEqual("Nie", profile.availability_text)
self.assertEqual(23, profile.availability_hours_per_week)
2021-06-20 20:21:49 +02:00
self.assertEqual("Hallo", profile.freetext)
2021-07-02 16:26:36 +02:00
self.assertTrue(profile.visible)
2021-06-20 20:13:19 +02:00
2021-06-27 12:20:36 +02:00
address = profile.address
self.assertEqual(address.name, "Peeeda")
2021-06-27 13:07:54 +02:00
self.assertEqual(address.street, "Bachstraße")
self.assertEqual(address.house_number, "42x")
self.assertEqual(address.additional, "oben")
self.assertEqual(address.postcode, "23232")
self.assertEqual(address.city, "Travemünde")
self.assertEqual(address.country, "Deutschland")
contacts = profile.contacts
self.assertEqual(len(contacts), 2)
first_contact = contacts[0]
self.assertEqual(first_contact.contacttype.name, "Matrix")
self.assertEqual(first_contact.content, "@peeda:wtf-eg.de")
second_contact = contacts[1]
self.assertEqual(second_contact.contacttype.name, "Rohrpost")
self.assertEqual(second_contact.content, "Ausgang 2")
2021-06-27 12:20:36 +02:00
2021-06-27 13:51:07 +02:00
skills = profile.skills
self.assertEqual(len(skills), 2)
first_skill = skills[0]
self.assertEqual(first_skill.skill.id, 3)
self.assertEqual(first_skill.skill.name, "Python")
self.assertEqual(first_skill.level, 4)
second_skill = skills[1]
2021-07-26 19:56:36 +02:00
self.assertEqual(second_skill.skill.id, self.max_skill_id + 1)
2021-06-27 13:51:07 +02:00
self.assertEqual(second_skill.skill.name, "Tschunkproduktion")
self.assertEqual(second_skill.level, 5)
2021-07-05 20:39:49 +02:00
searchtopics = profile.searchtopics
self.assertEqual(len(searchtopics), 2)
first_searchtopic = searchtopics[0]
self.assertEqual(first_searchtopic.skill.id, 3)
self.assertEqual(first_searchtopic.skill.name, "Python")
second_searchtopic = searchtopics[1]
2021-07-26 19:56:36 +02:00
self.assertEqual(second_searchtopic.skill.id, self.max_skill_id + 2)
2021-07-05 20:39:49 +02:00
self.assertEqual(second_searchtopic.skill.name, "Assembler")
2021-06-27 13:51:07 +02:00
languages = profile.languages
self.assertEqual(len(languages), 2)
first_language = languages[0]
self.assertEqual(first_language.language_id, "de")
self.assertEqual(first_language.level, 4)
second_language = languages[1]
self.assertEqual(second_language.language_id, "es")
self.assertEqual(second_language.level, 2)
2021-07-02 16:33:48 +02:00
def test_get_profile_unauthorised(self):
response = self.client.get("/users/1/profile")
self.assertEqual(response.status_code, 401)
def test_get_visible_proifle(self):
token = self.login("peter", "geheim")["token"]
2024-09-13 15:38:56 +02:00
with app.app_context():
babsi = User.query.filter(User.auth_id == "babsi1").first()
response = self.client.get(f"/users/{babsi.id}/profile", headers={"Authorization": f"Bearer {token}"})
self.assertEqual(response.status_code, 200)
def test_get_own_profile(self):
2021-06-20 19:25:27 +02:00
login_data = {"username": "peter", "password": "geheim"}
2021-06-27 13:38:16 +02:00
login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
2021-06-20 19:25:27 +02:00
self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json)
2024-09-13 15:38:56 +02:00
with app.app_context():
peter = User.query.filter(User.auth_id == "peter").first()
response = self.client.get(f"/users/{peter.id}/profile",
headers={"Authorization": "Bearer " + login_response.json["token"]})
profile_id = peter.profile.id
2021-06-20 19:25:27 +02:00
self.assertEqual(response.status_code, 200)
2021-06-26 12:16:14 +02:00
self.assertDictEqual(
2021-06-20 19:25:27 +02:00
response.json, {
"profile": {
"user_id": peter.id,
2021-06-26 10:51:39 +02:00
"nickname": "peternichtlustig",
"pronouns": "Herr Dr. Dr.",
"availability_status": True,
"availability_hours_per_week": 42,
"availability_text": "Immer",
2021-06-26 12:16:14 +02:00
"freetext": "Ich mag Kaffee",
"volunteerwork": "Gartenverein",
2021-07-02 16:26:36 +02:00
"visible": False,
2021-06-26 12:16:14 +02:00
"address": {
"additional": "Hinterhaus",
"city": "Bielefeld",
"country": "Deutschland",
"house_number": "23i",
"id": 1,
"name": "Peter Nichtlustig",
"postcode": "13337",
"profile_id": profile_id,
2021-06-26 12:16:14 +02:00
"street": "Waldweg"
},
"contacts": [{
"id": 1,
"profile_id": profile_id,
2021-06-26 12:16:14 +02:00
"contacttype": {
2021-07-02 16:49:14 +02:00
"id": 4,
2021-06-26 12:16:14 +02:00
"name": "Matrix"
},
"content": "@peter:wtf-eg.de"
2021-06-27 13:07:54 +02:00
}, {
"id": 2,
"profile_id": profile_id,
2021-06-27 13:07:54 +02:00
"contacttype": {
2021-07-02 16:49:14 +02:00
"id": 1,
2021-06-27 13:07:54 +02:00
"name": "E-Mail"
},
"content": "peter@wtf-eg.de"
2021-06-26 12:16:14 +02:00
}],
"skills": [{
"profile_id": profile_id,
2021-06-27 13:38:16 +02:00
"skill": {
"id": 1,
"name": "PHP",
"icon_url": "/skills/1/icon"
},
"level": 5
}, {
"profile_id": profile_id,
2021-06-26 12:16:14 +02:00
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
},
2021-06-27 13:38:16 +02:00
"level": 3
2021-06-26 12:16:14 +02:00
}],
2021-07-05 20:39:49 +02:00
"searchtopics": [{
"profile_id": profile_id,
2021-07-05 20:39:49 +02:00
"skill": {
"id": 1,
"name": "PHP",
"icon_url": "/skills/1/icon"
}
}, {
"profile_id": profile_id,
2021-07-05 20:39:49 +02:00
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
}
}],
2021-06-26 12:16:14 +02:00
"languages": [{
"profile_id": profile_id,
2021-06-26 12:16:14 +02:00
"language": {
"id": "de",
"name": "Deutsch",
"icon_url": "/languages/de/icon"
},
"level": 5
}, {
"profile_id": profile_id,
2021-06-26 12:16:14 +02:00
"language": {
"id": "fr",
"name": "Französisch",
"icon_url": "/languages/fr/icon"
},
"level": 3
}]
2021-06-20 19:25:27 +02:00
}
})
if __name__ == "main":
unittest.main()