Skip to content

Commit

Permalink
test(template): Fixed broken template authentication tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Achronus committed Sep 27, 2024
1 parent 87b21b0 commit 9d21a12
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions zentra_api/cli/template/fastapi/project/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


ROUTE_PREFIX = "/api/auth"
TOKEN_PREFIX = f"{ROUTE_PREFIX}/token"


class TestGetDB:
Expand Down Expand Up @@ -67,7 +68,7 @@ def test_login_user(client: TestClient):
"password": "testpassword",
}
client.post(f"{ROUTE_PREFIX}/register", json=user_data)
response = client.post(f"{ROUTE_PREFIX}/token", data=user_data)
response = client.post(f"{TOKEN_PREFIX}", data=user_data)
assert response.status_code == 202
assert "access_token" in response.json()

Expand All @@ -77,7 +78,7 @@ def test_login_invalid_user(client: TestClient):
"username": "invaliduser",
"password": "invalidpassword",
}
response = client.post(f"{ROUTE_PREFIX}/token", data=user_data)
response = client.post(f"{TOKEN_PREFIX}", data=user_data)
assert response.status_code == 401

@staticmethod
Expand All @@ -87,7 +88,7 @@ def test_get_user(client: TestClient):
"password": "testpassword",
}
client.post(f"{ROUTE_PREFIX}/register", json=user_data)
response = client.post(f"{ROUTE_PREFIX}/token", data=user_data)
response = client.post(f"{TOKEN_PREFIX}", data=user_data)
access_token = response.json()["access_token"]

headers = {"Authorization": f"Bearer {access_token}"}
Expand All @@ -105,7 +106,7 @@ def test_inactive_user(client: TestClient):
response = client.post(f"{ROUTE_PREFIX}/register", json=user_data)
assert response.status_code == 201

response = client.post(f"{ROUTE_PREFIX}/token", data=user_data)
response = client.post(f"{TOKEN_PREFIX}", data=user_data)
access_token = response.json()["access_token"]

headers = {"Authorization": f"Bearer {access_token}"}
Expand All @@ -126,7 +127,7 @@ def test_invalid_password(client: TestClient):
json={"username": "testuser4", "password": "password"},
)
response = client.post(
f"{ROUTE_PREFIX}/token",
f"{TOKEN_PREFIX}",
data={"username": "testuser4", "password": "wrong"},
)
assert response.status_code == 401
Expand All @@ -139,17 +140,15 @@ def test_verify_token(client: TestClient):
"password": "testpassword",
}
client.post(f"{ROUTE_PREFIX}/register", json=user_data)
response = client.post(f"{ROUTE_PREFIX}/token", data=user_data)
response = client.post(f"{TOKEN_PREFIX}", data=user_data)
access_token = response.json()["access_token"]

headers = {"Authorization": f"Bearer {access_token}"}
response = client.post(
f"{ROUTE_PREFIX}/verify-token/{access_token}", headers=headers
)
response = client.post(f"{TOKEN_PREFIX}/verify/{access_token}", headers=headers)
assert response.status_code == 200
assert response.json()["message"] == "Token is valid."

@staticmethod
def test_verify_invalid_token(client: TestClient):
response = client.post(f"{ROUTE_PREFIX}/verify-token/invalidtoken")
response = client.post(f"{TOKEN_PREFIX}/verify/invalidtoken")
assert response.status_code == 401

0 comments on commit 9d21a12

Please sign in to comment.