import pytest import requests from backend import config from tests.model.fakes import build_tribes @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_post_tribe(): data = {"name": "tribe", "level": "2nd"} url = config.get_api_url() r = requests.post(f"{url}/tribes", json=data) post_request = r.history[0] assert post_request.status_code == 302 assert r.status_code == 200 assert r.json() == { "assessments": [], "level": "2nd", "name": "tribe", "students": [], } @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_post_tribe_already_exists(): data = {"name": "Pioupiou", "level": "2nd"} url = config.get_api_url() requests.post(f"{url}/tribes", json=data) r = requests.post(f"{url}/tribes", json=data) assert r.status_code == 409 assert r.json() == f"The tribe {data['name']} already exists" @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_put_tribe(): tribe = build_tribes(1)[0] url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) mod_tribe = tribe mod_tribe.level = "other level" r = requests.put(f"{url}/tribes/{tribe.name}", json=mod_tribe.to_dict()) post_request = r.history[0] assert post_request.status_code == 302 assert r.status_code == 200 r = requests.get(f"{url}/tribes") assert [t["name"] for t in r.json()] == [mod_tribe.name] assert [t["level"] for t in r.json()] == [mod_tribe.level] @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_put_tribe_doesnt_exists(): tribe = build_tribes(1)[0] url = config.get_api_url() r = requests.put(f"{url}/tribes/{tribe.name}", json=tribe.to_dict()) assert r.status_code == 409 @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_delete_tribe(): tribe = build_tribes(1)[0] url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) r = requests.delete(f"{url}/tribes/{tribe.name}") assert r.status_code == 204 r = requests.get(f"{url}/tribes") assert r.json() == [] @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_delete_tribe_doesnt_exists(): tribe = build_tribes(1)[0] url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) r = requests.delete(f"{url}/tribes/notexisting") assert r.status_code == 409 r = requests.get(f"{url}/tribes") assert [t["name"] for t in r.json()] == [tribe.name] assert [t["level"] for t in r.json()] == [tribe.level] @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_post_list_tribe(): tribe = build_tribes(1)[0] url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) r = requests.get(f"{url}/tribes") assert r.json() == [ { "assessments": [], "level": tribe.level, "name": tribe.name, "students": [], } ]