From 49167a58a9005234d00bec29e0fee0de62f5b205 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Sep 2023 17:19:46 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(tests):=20update=20endpoint?= =?UTF-8?q?=20path=20from=20"/api/v1/user"=20to=20"/api/v1/users"=20for=20?= =?UTF-8?q?consistency=20and=20clarity=20=E2=9C=A8=20feat(tests):=20add=20?= =?UTF-8?q?assertion=20for=20response=20status=20code=20201=20after=20crea?= =?UTF-8?q?ting=20a=20user=20=F0=9F=90=9B=20fix(tests):=20update=20endpoin?= =?UTF-8?q?t=20path=20from=20"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/us?= =?UTF-8?q?ers/{user=5Fid}"=20for=20consistency=20and=20clarity=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(tests):=20update=20endpoint=20path=20from=20?= =?UTF-8?q?"/api/v1/user"=20to=20"/api/v1/users/whoami"=20to=20fetch=20the?= =?UTF-8?q?=20updated=20user=20=F0=9F=90=9B=20fix(tests):=20update=20endpo?= =?UTF-8?q?int=20path=20from=20"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/?= =?UTF-8?q?users/{user=5Fid}"=20for=20consistency=20and=20clarity=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(tests):=20update=20endpoint=20path=20from=20?= =?UTF-8?q?"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/users/{user=5Fid}"?= =?UTF-8?q?=20for=20consistency=20and=20clarity=20=E2=9C=A8=20feat(tests):?= =?UTF-8?q?=20add=20assertion=20for=20response=20status=20code=20401=20and?= =?UTF-8?q?=20detail=20message=20after=20fetching=20the=20updated=20user?= =?UTF-8?q?=20=E2=9C=A8=20feat(tests):=20add=20assertion=20for=20response?= =?UTF-8?q?=20status=20code=20200=20after=20deleting=20a=20user=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(tests):=20update=20endpoint=20path=20from=20?= =?UTF-8?q?"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/users/{user=5Fid}"?= =?UTF-8?q?=20for=20consistency=20and=20clarity=20=E2=9C=A8=20feat(tests):?= =?UTF-8?q?=20add=20assertion=20for=20response=20status=20code=20200=20and?= =?UTF-8?q?=20detail=20message=20after=20deleting=20a=20user=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(tests):=20update=20endpoint=20path=20from=20?= =?UTF-8?q?"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/users/{user=5Fid}"?= =?UTF-8?q?=20for=20consistency=20and=20clarity=20=F0=9F=90=9B=20fix(tests?= =?UTF-8?q?):=20update=20endpoint=20path=20from=20"/api/v1/user/{user=5Fid?= =?UTF-8?q?}"=20to=20"/api/v1/users/{user=5Fid}"=20for=20consistency=20and?= =?UTF-8?q?=20clarity=20=E2=9C=A8=20feat(tests):=20add=20assertion=20for?= =?UTF-8?q?=20response=20status=20code=20304=20after=20patching=20user=20d?= =?UTF-8?q?ata=20=E2=9C=A8=20feat(tests):=20add=20assertion=20for=20respon?= =?UTF-8?q?se=20status=20code=20200=20after=20resetting=20user=20password?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(tests):=20update=20endpoint=20path=20from?= =?UTF-8?q?=20"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/users/{user=5Fid}?= =?UTF-8?q?"=20for=20consistency=20and=20clarity=20=E2=9C=A8=20feat(tests)?= =?UTF-8?q?:=20add=20assertion=20for=20response=20status=20code=20422=20an?= =?UTF-8?q?d=20detail=20message=20after=20patching=20user=20data=20with=20?= =?UTF-8?q?wrong=20id=20=F0=9F=90=9B=20fix(tests):=20update=20endpoint=20p?= =?UTF-8?q?ath=20from=20"/api/v1/user/{user=5Fid}"=20to=20"/api/v1/users/{?= =?UTF-8?q?user=5Fid}"=20for=20consistency=20and=20clarity=20=E2=9C=A8=20f?= =?UTF-8?q?eat(tests):=20add=20assertion=20for=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/conftest.py | 3 ++- tests/test_user.py | 52 +++++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1d1fb9ac7..95aba4462 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -166,7 +166,8 @@ def test_user(client): username="testuser", password="testpassword", ) - response = client.post("/api/v1/user", json=user_data.dict()) + response = client.post("/api/v1/users", json=user_data.dict()) + assert response.status_code == 201 return response.json() diff --git a/tests/test_user.py b/tests/test_user.py index bc617e127..4d99c5c9c 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -78,24 +78,27 @@ def test_deactivated_user_cannot_access(client, deactivated_user, logged_in_head assert response.json()["detail"] == "The user doesn't have enough privileges" -def test_data_consistency_after_update(client, active_user, logged_in_headers): +def test_data_consistency_after_update( + client, active_user, logged_in_headers, super_user_headers +): user_id = active_user.id - update_data = UserUpdate(username="newname") + update_data = UserUpdate(is_active=False) response = client.patch( - f"/api/v1/user/{user_id}", json=update_data.dict(), headers=logged_in_headers + f"/api/v1/users/{user_id}", json=update_data.dict(), headers=super_user_headers ) - assert response.status_code == 200 + assert response.status_code == 200, response.json() # Fetch the updated user from the database - response = client.get("/api/v1/user", headers=logged_in_headers) - assert response.json()["username"] == "newname", response.json() + response = client.get("/api/v1/users/whoami", headers=logged_in_headers) + assert response.status_code == 401, response.json() + assert response.json()["detail"] == "Could not validate credentials" def test_data_consistency_after_delete(client, test_user, super_user_headers): - user_id = test_user["id"] - response = client.delete(f"/api/v1/user/{user_id}", headers=super_user_headers) - assert response.status_code == 200 + user_id = test_user.get("id") + response = client.delete(f"/api/v1/users/{user_id}", headers=super_user_headers) + assert response.status_code == 200, response.json() # Attempt to fetch the deleted user from the database response = client.get("/api/v1/users", headers=super_user_headers) @@ -157,7 +160,21 @@ def test_patch_user(client, active_user, logged_in_headers): ) response = client.patch( - f"/api/v1/user/{user_id}", json=update_data.dict(), headers=logged_in_headers + f"/api/v1/users/{user_id}", json=update_data.dict(), headers=logged_in_headers + ) + assert response.status_code == 304, response.json() + + +def test_patch_reset_password(client, active_user, logged_in_headers): + user_id = active_user.id + update_data = UserUpdate( + password="newpassword", + ) + + response = client.patch( + f"/api/v1/users/{user_id}/reset-password", + json=update_data.dict(), + headers=logged_in_headers, ) assert response.status_code == 200, response.json() @@ -169,7 +186,7 @@ def test_patch_user_wrong_id(client, active_user, logged_in_headers): ) response = client.patch( - f"/api/v1/user/{user_id}", json=update_data.dict(), headers=logged_in_headers + f"/api/v1/users/{user_id}", json=update_data.dict(), headers=logged_in_headers ) assert response.status_code == 422, response.json() assert response.json() == { @@ -185,14 +202,14 @@ def test_patch_user_wrong_id(client, active_user, logged_in_headers): def test_delete_user(client, test_user, super_user_headers): user_id = test_user["id"] - response = client.delete(f"/api/v1/user/{user_id}", headers=super_user_headers) + response = client.delete(f"/api/v1/users/{user_id}", headers=super_user_headers) assert response.status_code == 200 assert response.json() == {"detail": "User deleted"} def test_delete_user_wrong_id(client, test_user, super_user_headers): user_id = "wrong_id" - response = client.delete(f"/api/v1/user/{user_id}", headers=super_user_headers) + response = client.delete(f"/api/v1/users/{user_id}", headers=super_user_headers) assert response.status_code == 422 assert response.json() == { "detail": [ @@ -207,13 +224,6 @@ def test_delete_user_wrong_id(client, test_user, super_user_headers): def test_normal_user_cant_delete_user(client, test_user, logged_in_headers): user_id = test_user["id"] - response = client.delete(f"/api/v1/user/{user_id}", headers=logged_in_headers) + response = client.delete(f"/api/v1/users/{user_id}", headers=logged_in_headers) assert response.status_code == 400 assert response.json() == {"detail": "The user doesn't have enough privileges"} - - -# If you still want to test the superuser endpoint -def test_add_super_user_for_testing_purposes_delete_me_before_merge_into_dev(client): - response = client.post("/api/v1/super_user") - assert response.status_code == 200 - assert response.json()["username"] == "superuser"