from alembic import command import unittest import json from app import app, db, migrate from ki.actions import seed from ki.models import 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() with app.app_context(): config = migrate.get_config() command.upgrade(config, "head") seed(True) def tearDown(self): db.drop_all() db.engine.dispose() def test_create_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) 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) with app.app_context(): user = User.query.filter(User.id == 1).first() profile = user.profile self.assertEqual("Herr Dr. Dr.", profile.pronouns) self.assertEqual("ja", profile.volunteerwork) self.assertEqual("Hallo", profile.freetext) 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={ "Authorization": "Bearer " + login_response.json["token"] }) self.assertEqual(response.status_code, 200) self.assertEqual( response.json, { "profile": { "freetext": "Ich mag Kaffee", "nickname": "peternichtlustig", "pronouns": "Herr Dr. Dr.", "volunteerwork": "Gartenverein" } }) if __name__ == "main": unittest.main()