diff --git a/ki/actions/seed.py b/ki/actions/seed.py index 9edf8e9..08052cf 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -88,71 +88,73 @@ def seed_user(auth_id, def seed(dev: bool): - seed_contacttypes() + with app.app_context(): + seed_contacttypes() - skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv" + skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv" - app.logger.info("importing skills") + app.logger.info("importing skills") - with open(skill_seed_file_path) as skills_file: - skills_csv_reader = csv.DictReader(skills_file) + with open(skill_seed_file_path) as skills_file: + skills_csv_reader = csv.DictReader(skills_file) - for skill in skills_csv_reader: - id = int(skill["id"]) - db_skill = db.session.get(Skill, id) + for skill in skills_csv_reader: + id = int(skill["id"]) + db_skill = db.session.get(Skill, id) - if db_skill is None: - db.session.add(Skill(id=int(skill["id"]), name=skill["name"])) + if db_skill is None: + db.session.add(Skill(id=int(skill["id"]), name=skill["name"])) - app.logger.info("importing languages") + app.logger.info("importing languages") - iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv" + iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv" - with open(iso_seed_file_path) as iso_file: - iso_csv_reader = csv.DictReader(iso_file) + with open(iso_seed_file_path) as iso_file: + iso_csv_reader = csv.DictReader(iso_file) - for iso in iso_csv_reader: - id = iso["639-1"] - db_language = db.session.get(Language, id) + for iso in iso_csv_reader: + id = iso["639-1"] + db_language = db.session.get(Language, id) - if db_language is None: - db.session.add(Language(id=iso["639-1"], name=iso["Sprache"])) + if db_language is None: + db.session.add(Language(id=iso["639-1"], name=iso["Sprache"])) - if dev: - seed_user("klaus", visible=False) + if dev: + seed_user("klaus", visible=False) - for i in range(1, 20): - seed_user(f"babsi{i}") + for i in range(1, 20): + seed_user(f"babsi{i}") - seed_user("peter", - nickname="peternichtlustig", - visible=False, - pronouns="Herr Dr. Dr.", - volunteerwork="Gartenverein", - availability_status=True, - availability_hours_per_week=42, - availability_text="Immer", - freetext="Ich mag Kaffee", - skills=[(3, 3), (1, 5)], - searchtopics=[3, 1], - languages=[("de", 5), ("fr", 3)], - address=("Peter Nichtlustig", "Waldweg", "23i", "Hinterhaus", "13337", "Bielefeld", "Deutschland"), - contacts=[(4, "@peter:wtf-eg.de"), (1, "peter@wtf-eg.de")]) + seed_user("peter", + nickname="peternichtlustig", + visible=False, + pronouns="Herr Dr. Dr.", + volunteerwork="Gartenverein", + availability_status=True, + availability_hours_per_week=42, + availability_text="Immer", + freetext="Ich mag Kaffee", + skills=[(3, 3), (1, 5)], + searchtopics=[3, 1], + languages=[("de", 5), ("fr", 3)], + address=("Peter Nichtlustig", "Waldweg", "23i", "Hinterhaus", "13337", "Bielefeld", + "Deutschland"), + contacts=[(4, "@peter:wtf-eg.de"), (1, "peter@wtf-eg.de")]) - seed_user("dirtydieter", - volunteerwork="Müll sammeln", - availability_status=True, - availability_hours_per_week=24, - availability_text="Nur Nachts!", - freetext="1001010010111!!!", - skills=[(1, 5)], - address=("Friedrich Witzig", "", "", "", "", "", "")) + seed_user("dirtydieter", + volunteerwork="Müll sammeln", + availability_status=True, + availability_hours_per_week=24, + availability_text="Nur Nachts!", + freetext="1001010010111!!!", + skills=[(1, 5)], + address=("Friedrich Witzig", "", "", "", "", "", "")) - all_skills = [(skill.id, 3) for skill in Skill.query.all()] - seed_user("jutta", languages=[("fr", 5)], skills=all_skills) + all_skills = [(skill.id, 3) for skill in Skill.query.all()] + seed_user("jutta", languages=[("fr", 5)], skills=all_skills) - seed_user("giesela", skills=[(9, 3), (10, 5)]) - seed_user("bertha", visible=False, skills=[(11, 3), (10, 5)]) - seed_user("monique", languages=[("fr", 4)]) + seed_user("giesela", skills=[(9, 3), (10, 5)]) + seed_user("bertha", visible=False, skills=[(11, 3), (10, 5)]) + seed_user("monique", languages=[("fr", 4)]) - db.session.commit() + db.session.commit() diff --git a/ki/test/ApiTest.py b/ki/test/ApiTest.py index 72e9873..5995010 100644 --- a/ki/test/ApiTest.py +++ b/ki/test/ApiTest.py @@ -26,13 +26,14 @@ class ApiTest(unittest.TestCase): config = migrate.get_config() command.upgrade(config, "head") - seed(True) - max_skill = Skill.query.order_by(Skill.id.desc()).first() - self.max_skill_id = max_skill.id + seed(True) + max_skill = Skill.query.order_by(Skill.id.desc()).first() + self.max_skill_id = max_skill.id def tearDown(self): - db.drop_all() - db.engine.dispose() + with app.app_context(): + db.drop_all() + db.engine.dispose() def login(self, username, password): login_data = {"username": username, "password": password} diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index 61f247d..cf564a9 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -20,11 +20,12 @@ class TestProfileEndpoint(ApiTest): self.assertEqual(login_response.status_code, 200) self.assertIn("token", login_response.json) - 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"]}) + 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"]}) self.assertEqual(response.status_code, 403) @@ -103,11 +104,12 @@ class TestProfileEndpoint(ApiTest): "level": 2 }] } - 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}) + 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}) self.assertEqual(response.status_code, 200) with app.app_context(): @@ -185,8 +187,9 @@ class TestProfileEndpoint(ApiTest): def test_get_visible_proifle(self): token = self.login("peter", "geheim")["token"] - babsi = User.query.filter(User.auth_id == "babsi1").first() - response = self.client.get(f"/users/{babsi.id}/profile", headers={"Authorization": f"Bearer {token}"}) + 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) @@ -197,10 +200,11 @@ class TestProfileEndpoint(ApiTest): self.assertEqual(login_response.status_code, 200) self.assertIn("token", login_response.json) - 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 + 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 self.assertEqual(response.status_code, 200) self.assertDictEqual( response.json, {