🐛 fix(tests): update endpoint path from "/api/v1/user" to "/api/v1/users" for consistency and clarity
✨ feat(tests): add assertion for response status code 201 after creating a user 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity 🐛 fix(tests): update endpoint path from "/api/v1/user" to "/api/v1/users/whoami" to fetch the updated user 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity ✨ feat(tests): add assertion for response status code 401 and detail message after fetching the updated user ✨ feat(tests): add assertion for response status code 200 after deleting a user 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity ✨ feat(tests): add assertion for response status code 200 and detail message after deleting a user 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity ✨ feat(tests): add assertion for response status code 304 after patching user data ✨ feat(tests): add assertion for response status code 200 after resetting user password 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity ✨ feat(tests): add assertion for response status code 422 and detail message after patching user data with wrong id 🐛 fix(tests): update endpoint path from "/api/v1/user/{user_id}" to "/api/v1/users/{user_id}" for consistency and clarity ✨ feat(tests): add assertion for response
This commit is contained in:
parent
a7db386277
commit
49167a58a9
2 changed files with 33 additions and 22 deletions
|
|
@ -166,7 +166,8 @@ def test_user(client):
|
||||||
username="testuser",
|
username="testuser",
|
||||||
password="testpassword",
|
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()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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"
|
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
|
user_id = active_user.id
|
||||||
update_data = UserUpdate(username="newname")
|
update_data = UserUpdate(is_active=False)
|
||||||
|
|
||||||
response = client.patch(
|
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
|
# Fetch the updated user from the database
|
||||||
response = client.get("/api/v1/user", headers=logged_in_headers)
|
response = client.get("/api/v1/users/whoami", headers=logged_in_headers)
|
||||||
assert response.json()["username"] == "newname", response.json()
|
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):
|
def test_data_consistency_after_delete(client, test_user, super_user_headers):
|
||||||
user_id = test_user["id"]
|
user_id = test_user.get("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.status_code == 200, response.json()
|
||||||
|
|
||||||
# Attempt to fetch the deleted user from the database
|
# Attempt to fetch the deleted user from the database
|
||||||
response = client.get("/api/v1/users", headers=super_user_headers)
|
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(
|
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()
|
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(
|
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.status_code == 422, response.json()
|
||||||
assert 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):
|
def test_delete_user(client, test_user, super_user_headers):
|
||||||
user_id = test_user["id"]
|
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.status_code == 200
|
||||||
assert response.json() == {"detail": "User deleted"}
|
assert response.json() == {"detail": "User deleted"}
|
||||||
|
|
||||||
|
|
||||||
def test_delete_user_wrong_id(client, test_user, super_user_headers):
|
def test_delete_user_wrong_id(client, test_user, super_user_headers):
|
||||||
user_id = "wrong_id"
|
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.status_code == 422
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"detail": [
|
"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):
|
def test_normal_user_cant_delete_user(client, test_user, logged_in_headers):
|
||||||
user_id = test_user["id"]
|
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.status_code == 400
|
||||||
assert response.json() == {"detail": "The user doesn't have enough privileges"}
|
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"
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue