ki-backend/ki/test/test_profile_endpoint.py

135 lines
4.8 KiB
Python
Raw Normal View History

2021-06-20 19:25:27 +02:00
from alembic import command
import unittest
import json
from app import app, db, migrate
2021-06-26 10:51:39 +02:00
from ki.actions import seed
from ki.models import User
2021-06-20 19:25:27 +02:00
class TestProfileEndpoint(unittest.TestCase):
2021-06-26 12:16:14 +02:00
maxDiff = None
2021-06-20 19:25:27 +02:00
def setUp(self):
app.debug = True
2021-06-20 20:13:19 +02:00
app.config["TESTING"] = True
2021-06-20 19:25:27 +02:00
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
self.client = app.test_client()
with app.app_context():
config = migrate.get_config()
command.upgrade(config, "head")
2021-06-26 10:51:39 +02:00
seed(True)
2021-06-20 19:25:27 +02:00
2021-06-20 20:13:19 +02:00
def tearDown(self):
2021-06-22 17:35:27 +02:00
db.drop_all()
2021-06-20 20:13:19 +02:00
db.engine.dispose()
2021-06-26 12:16:14 +02:00
def test_update_profile(self):
2021-06-20 20:13:19 +02:00
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 = {
2021-06-26 12:16:14 +02:00
"pronouns": "Monsieur",
2021-06-20 20:13:19 +02:00
"volunteerwork": "ja",
"freetext": "Hallo",
}
response = self.client.post("/users/1/profile",
data=json.dumps(data),
content_type="application/json",
headers={
2021-06-26 12:16:14 +02:00
"Authorization": "Bearer " +
2021-06-20 20:13:19 +02:00
login_response.json["token"]
})
self.assertEqual(response.status_code, 200)
2021-06-20 20:21:49 +02:00
with app.app_context():
user = User.query.filter(User.id == 1).first()
profile = user.profile
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("Hallo", profile.freetext)
2021-06-20 20:13:19 +02:00
2021-06-20 19:25:27 +02:00
def test_get_profile(self):
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)
response = self.client.get("/users/1/profile",
headers={
2021-06-26 12:16:14 +02:00
"Authorization": "Bearer " +
login_response.json["token"]
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": {
2021-06-26 12:16:14 +02:00
"user_id": 1,
2021-06-26 10:51:39 +02:00
"nickname": "peternichtlustig",
"pronouns": "Herr Dr. Dr.",
2021-06-26 12:16:14 +02:00
"freetext": "Ich mag Kaffee",
"volunteerwork": "Gartenverein",
"address": {
"additional": "Hinterhaus",
"city": "Bielefeld",
"country": "Deutschland",
"house_number": "23i",
"id": 1,
"name": "Peter Nichtlustig",
"postcode": "13337",
"profile_id": 1,
"street": "Waldweg"
},
"contacts": [{
"id": 1,
"profile_id": 1,
"contacttype": {
"id": 1,
"name": "Matrix"
},
"content": "@peter:wtf-eg.de"
}],
"skills": [{
"profile_id": 1,
"skill": {
"id": 3,
"name": "Python",
"icon_url": "/skills/3/icon"
},
"level": 5
}],
"languages": [{
"profile_id": 1,
"language": {
"id": "de",
"name": "Deutsch",
"icon_url": "/languages/de/icon"
},
"level": 5
}, {
"profile_id": 1,
"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()