From a953631d19f41661c79cbb5b7a3ae236a547a07d Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 31 Dec 2022 14:55:24 +0100 Subject: [PATCH] Feat: post and put redirect to get method --- backend/api/main.py | 24 ++++++++++++++---------- tests/e2e/test_api_students.py | 9 +++++++-- tests/e2e/test_api_tribes.py | 15 +++++++-------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/backend/api/main.py b/backend/api/main.py index 27b6dd0..86927b4 100644 --- a/backend/api/main.py +++ b/backend/api/main.py @@ -1,7 +1,7 @@ import sqlite3 from fastapi import FastAPI, status -from fastapi.responses import JSONResponse, Response +from fastapi.responses import JSONResponse, RedirectResponse, Response from backend.adapters.sqlite import create_db from backend.api.model import StudentModel, TribeModel @@ -35,7 +35,7 @@ student_repo = StudentSQLiteRepository(conn) app = FastAPI() -@app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel) +@app.post("/tribes", response_class=RedirectResponse, status_code=status.HTTP_302_FOUND) async def post_tribe(item: TribeModel): try: tribe = services.add_tribe( @@ -47,10 +47,12 @@ async def post_tribe(item: TribeModel): content=f"The tribe {item.name} already exists", ) - return tribe.to_dict() + return f"/tribes/{tribe.name}" -@app.put("/tribes/{name}", status_code=status.HTTP_200_OK, response_model=TribeModel) +@app.put( + "/tribes/{name}", response_class=RedirectResponse, status_code=status.HTTP_302_FOUND +) async def put_tribe(name: str, item: TribeModel): try: tribe = services.update_tribe( @@ -62,7 +64,7 @@ async def put_tribe(name: str, item: TribeModel): content=f"The tribe {name} does not exists", ) - return tribe.to_dict() + return f"/tribes/{tribe.name}" @app.delete("/tribes/{name}") @@ -94,7 +96,9 @@ async def get_tribe(name: str): return tribe.to_dict() -@app.post("/students", status_code=status.HTTP_201_CREATED, response_model=StudentModel) +@app.post( + "/students", response_class=RedirectResponse, status_code=status.HTTP_302_FOUND +) async def post_student(item: StudentModel): if item.id is not None: return JSONResponse( @@ -116,7 +120,7 @@ async def post_student(item: StudentModel): content=f"The tribe {item.tribe_name} does not exists. You can't add a student in it.", ) - return student.to_dict() + return f"/students/{student.id}" @app.get("/students/{id}", status_code=status.HTTP_200_OK, response_model=StudentModel) @@ -136,8 +140,8 @@ async def list_students(): @app.put( "/students/{student_id}", - status_code=status.HTTP_200_OK, - response_model=StudentModel, + response_class=RedirectResponse, + status_code=status.HTTP_302_FOUND, ) async def put_student(student_id, item: StudentModel): if student_id != item.id: @@ -166,7 +170,7 @@ async def put_student(student_id, item: StudentModel): content=f"The student {item.name} ({item.id=}) does not exists. You can't modify it.", ) - return student.to_dict() + return f"/students/{student.id}" @app.delete( diff --git a/tests/e2e/test_api_students.py b/tests/e2e/test_api_students.py index 5069286..619acdf 100644 --- a/tests/e2e/test_api_students.py +++ b/tests/e2e/test_api_students.py @@ -15,7 +15,10 @@ def test_api_post_student(): data = {"name": "zart", "tribe_name": tribe.name} r = requests.post(f"{url}/students", json=data) - assert r.status_code == 201 + post_request = r.history[0] + assert post_request.status_code == 302 + + assert r.status_code == 200 assert r.json()["name"] == "zart" assert r.json()["tribe_name"] == tribe.name assert r.json()["id"] @@ -72,6 +75,9 @@ def test_api_put_student(): r2 = requests.put(f"{url}/students/{student['id']}", json=student) + post_request = r2.history[0] + assert post_request.status_code == 302 + assert r2.status_code == 200 assert r2.json()["name"] == "Choupinou" assert r2.json()["tribe_name"] == tribe.name @@ -89,7 +95,6 @@ def test_api_delete_student(): 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}") diff --git a/tests/e2e/test_api_tribes.py b/tests/e2e/test_api_tribes.py index b1b1604..78b447e 100644 --- a/tests/e2e/test_api_tribes.py +++ b/tests/e2e/test_api_tribes.py @@ -13,7 +13,10 @@ def test_api_post_tribe(): url = config.get_api_url() r = requests.post(f"{url}/tribes", json=data) - assert r.status_code == 201 + post_request = r.history[0] + assert post_request.status_code == 302 + + assert r.status_code == 200 assert r.json() == { "assessments": [], "level": "2nd", @@ -42,11 +45,13 @@ def test_api_put_tribe(): url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) - assert r.status_code == 201 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") @@ -72,8 +77,6 @@ def test_api_delete_tribe(): url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) - assert r.status_code == 201 - r = requests.delete(f"{url}/tribes/{tribe.name}") assert r.status_code == 204 @@ -89,8 +92,6 @@ def test_api_delete_tribe_doesnt_exists(): url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) - assert r.status_code == 201 - r = requests.delete(f"{url}/tribes/notexisting") assert r.status_code == 409 @@ -107,8 +108,6 @@ def test_api_post_list_tribe(): url = config.get_api_url() r = requests.post(f"{url}/tribes", json=tribe.to_dict()) - assert r.status_code == 201 - r = requests.get(f"{url}/tribes") assert r.json() == [ {