2022-12-28 06:47:35 +00:00
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from backend import config
|
2022-12-30 13:41:23 +00:00
|
|
|
from tests.model.fakes import build_student, build_tribes
|
2022-12-28 06:47:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("restart_api")
|
|
|
|
@pytest.mark.usefixtures("clean_db")
|
|
|
|
def test_api_post_student():
|
|
|
|
url = config.get_api_url()
|
|
|
|
tribe = build_tribes(1)[0]
|
|
|
|
requests.post(f"{url}/tribes", json=tribe.to_dict())
|
|
|
|
|
|
|
|
data = {"name": "zart", "tribe_name": tribe.name}
|
|
|
|
r = requests.post(f"{url}/students", json=data)
|
|
|
|
|
|
|
|
assert r.status_code == 201
|
|
|
|
assert r.json()["name"] == "zart"
|
|
|
|
assert r.json()["tribe_name"] == tribe.name
|
|
|
|
assert r.json()["id"]
|
2022-12-29 06:23:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("restart_api")
|
|
|
|
@pytest.mark.usefixtures("clean_db")
|
2022-12-29 06:37:49 +00:00
|
|
|
def test_api_post_student_with_id():
|
2022-12-29 06:23:02 +00:00
|
|
|
url = config.get_api_url()
|
|
|
|
tribe = build_tribes(1)[0]
|
|
|
|
requests.post(f"{url}/tribes", json=tribe.to_dict())
|
|
|
|
|
2022-12-29 06:37:49 +00:00
|
|
|
data = {"id": "1234", "name": "zart", "tribe_name": tribe.name}
|
|
|
|
requests.post(f"{url}/students", json=data)
|
2022-12-29 06:23:02 +00:00
|
|
|
r = requests.post(f"{url}/students", json=data)
|
|
|
|
|
2022-12-29 06:37:49 +00:00
|
|
|
assert r.status_code == 409
|
|
|
|
assert (
|
|
|
|
r.json()
|
|
|
|
== f"You can't post a student with an id. It is already registrered. Use PUT to modify it."
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("restart_api")
|
|
|
|
@pytest.mark.usefixtures("clean_db")
|
|
|
|
def test_api_post_student_in_non_existant_tribe():
|
|
|
|
url = config.get_api_url()
|
|
|
|
tribe = build_tribes(1)[0]
|
|
|
|
requests.post(f"{url}/tribes", json=tribe.to_dict())
|
|
|
|
|
|
|
|
data = {"name": "zart", "tribe_name": tribe.name + "_"}
|
|
|
|
requests.post(f"{url}/students", json=data)
|
|
|
|
r = requests.post(f"{url}/students", json=data)
|
|
|
|
|
|
|
|
assert r.status_code == 409
|
|
|
|
assert (
|
|
|
|
r.json()
|
|
|
|
== f"The tribe {tribe.name+'_'} does not exists. You can't add a student in it."
|
|
|
|
)
|
2022-12-29 16:47:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("restart_api")
|
|
|
|
@pytest.mark.usefixtures("clean_db")
|
|
|
|
def test_api_put_student():
|
|
|
|
url = config.get_api_url()
|
|
|
|
tribe = build_tribes(1)[0]
|
|
|
|
requests.post(f"{url}/tribes", json=tribe.to_dict())
|
|
|
|
|
|
|
|
data = {"name": "zart", "tribe_name": tribe.name}
|
|
|
|
r = requests.post(f"{url}/students", json=data)
|
|
|
|
|
|
|
|
student = r.json()
|
|
|
|
student["name"] = "Choupinou"
|
|
|
|
|
|
|
|
r2 = requests.put(f"{url}/students/{student['id']}", json=student)
|
|
|
|
|
|
|
|
assert r2.status_code == 200
|
|
|
|
assert r2.json()["name"] == "Choupinou"
|
|
|
|
assert r2.json()["tribe_name"] == tribe.name
|
|
|
|
assert r2.json()["id"] == r.json()["id"]
|
2022-12-30 13:41:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("restart_api")
|
|
|
|
@pytest.mark.usefixtures("clean_db")
|
|
|
|
def test_api_delete_student():
|
|
|
|
url = config.get_api_url()
|
|
|
|
tribe = build_tribes(1)[0]
|
|
|
|
requests.post(f"{url}/tribes", json=tribe.to_dict())
|
|
|
|
|
|
|
|
student = build_student([tribe], 1)[0]
|
|
|
|
r = requests.post(
|
|
|
|
f"{url}/students", json={"name": student.name, "tribe_name": student.tribe.name}
|
|
|
|
)
|
|
|
|
assert r.status_code == 201
|
|
|
|
student_id = r.json()["id"]
|
|
|
|
|
|
|
|
r = requests.delete(f"{url}/students/{student_id}")
|
|
|
|
assert r.status_code == 204
|
|
|
|
|
|
|
|
r = requests.get(f"{url}/students/")
|
|
|
|
assert r.json() == []
|