Skip to content

Commit 227eb35

Browse files
committed
fixed tests (access_token -> csrf_token)
1 parent c2ed952 commit 227eb35

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

backend/tests/integration/test_auth_api.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ async def setup(self, client: AsyncClient, db: AsyncIOMotorDatabase) -> None:
3737
pytest.fail(f"Auth setup: Login failed: {login_response.status_code} {login_response.text}")
3838

3939
login_data = login_response.json()
40-
assert "access_token" in login_data
41-
self.token = login_data["access_token"]
42-
self.headers = {"Authorization": f"Bearer {self.token}"}
40+
assert "csrf_token" in login_data
41+
assert "message" in login_data
42+
self.csrf_token = login_data["csrf_token"]
43+
self.headers = {"X-CSRF-Token": self.csrf_token}
4344

4445
@pytest.mark.asyncio
4546
async def test_login_success(self) -> None:
@@ -49,7 +50,10 @@ async def test_login_success(self) -> None:
4950
data={"username": self.test_username, "password": self.test_password},
5051
)
5152
assert login_response.status_code == 200
52-
assert "access_token" in login_response.json()
53+
data = login_response.json()
54+
assert "csrf_token" in data
55+
assert "message" in data
56+
assert data["message"] == "Login successful"
5357

5458
@pytest.mark.asyncio
5559
async def test_login_wrong_password(self) -> None:
@@ -103,20 +107,22 @@ async def test_register_duplicate_user(self) -> None:
103107
@pytest.mark.asyncio
104108
async def test_verify_token_valid(self) -> None:
105109
"""Verify a valid token (using token from setup)."""
106-
response = await self.client.get("/api/v1/verify-token", headers=self.headers)
110+
response = await self.client.get("/api/v1/verify-token")
107111
assert response.status_code == 200
108112
data = response.json()
109-
assert data == {"valid": True, "username": self.test_username}
113+
assert data["valid"] == True
114+
assert data["username"] == self.test_username
115+
assert "csrf_token" in data
110116

111117
@pytest.mark.asyncio
112118
async def test_verify_token_invalid_token(self) -> None:
113119
"""Verify an invalid/malformed token fails."""
114-
invalid_headers = {"Authorization": "Bearer invalidtoken"}
115-
response = await self.client.get("/api/v1/verify-token", headers=invalid_headers)
120+
# Clear cookies to simulate invalid token
121+
response = await self.client.get("/api/v1/verify-token", cookies={})
116122
assert response.status_code == 401
117123

118124
@pytest.mark.asyncio
119125
async def test_verify_token_no_token(self) -> None:
120126
"""Verify request fails without token."""
121-
response = await self.client.get("/api/v1/verify-token") # No headers
127+
response = await self.client.get("/api/v1/verify-token", cookies={}) # No cookies
122128
assert response.status_code == 401

backend/tests/integration/test_execution_api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ async def setup(self, client: AsyncClient, db: AsyncIOMotorDatabase) -> None:
4040
if login_response.status_code != 200:
4141
pytest.fail(f"Exec setup: Login failed: {login_response.status_code} {login_response.text}")
4242
login_data = login_response.json()
43-
assert "access_token" in login_data
44-
self.token = login_data["access_token"]
45-
self.headers = {"Authorization": f"Bearer {self.token}"}
43+
assert "csrf_token" in login_data
44+
assert "message" in login_data
45+
self.csrf_token = login_data["csrf_token"]
46+
self.headers = {"X-CSRF-Token": self.csrf_token}
4647

4748
@pytest.mark.asyncio
4849
async def test_execute_script_success_workflow(self) -> None:
@@ -146,7 +147,7 @@ async def test_k8s_resource_limits(self) -> None:
146147
async def test_execute_endpoint_without_auth(self) -> None:
147148
"""Test accessing execute endpoint without authentication (should succeed)."""
148149
execution_request = {"script": "print('no auth test should pass')"}
149-
response = await self.client.post("/api/v1/execute", json=execution_request) # No headers
150+
response = await self.client.post("/api/v1/execute", json=execution_request, cookies={}) # No cookies
150151
# Expect 200 OK because the endpoint is public
151152
assert response.status_code == 200
152153
assert "execution_id" in response.json()
@@ -155,6 +156,6 @@ async def test_execute_endpoint_without_auth(self) -> None:
155156
@pytest.mark.asyncio
156157
async def test_result_endpoint_without_auth(self) -> None:
157158
non_existent_id = "nonexistent-public-id-999"
158-
response = await self.client.get(f"/api/v1/result/{non_existent_id}") # No headers
159+
response = await self.client.get(f"/api/v1/result/{non_existent_id}", cookies={}) # No cookies
159160
# Expect 404 Not Found because the ID doesn't exist, *not* 401 because the endpoint is public
160161
assert response.status_code == 404

backend/tests/integration/test_saved_scripts_api.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ async def setup(self, client: AsyncClient, db: AsyncIOMotorDatabase) -> None:
3535
if login_response.status_code != 200:
3636
pytest.fail(f"Scripts setup: Login failed: {login_response.status_code} {login_response.text}")
3737
login_data = login_response.json()
38-
assert "access_token" in login_data
39-
self.token = login_data["access_token"]
40-
self.headers = {"Authorization": f"Bearer {self.token}"}
38+
assert "csrf_token" in login_data
39+
assert "message" in login_data
40+
self.csrf_token = login_data["csrf_token"]
41+
self.headers = {"X-CSRF-Token": self.csrf_token}
4142

4243
@pytest.mark.asyncio
4344
async def test_saved_scripts_crud_workflow(self) -> None:
@@ -132,11 +133,12 @@ async def test_list_scripts_empty(self) -> None:
132133
async def test_scripts_endpoints_without_auth(self) -> None:
133134
"""Test accessing scripts endpoints without authentication."""
134135
script_data = {"name": "No Auth", "script": "print('no')"}
135-
response_post = await self.client.post("/api/v1/scripts", json=script_data)
136-
response_get_list = await self.client.get("/api/v1/scripts")
137-
response_get_one = await self.client.get("/api/v1/scripts/some-id")
138-
response_put = await self.client.put("/api/v1/scripts/some-id", json=script_data)
139-
response_delete = await self.client.delete("/api/v1/scripts/some-id")
136+
# Use empty cookies to simulate no authentication
137+
response_post = await self.client.post("/api/v1/scripts", json=script_data, cookies={})
138+
response_get_list = await self.client.get("/api/v1/scripts", cookies={})
139+
response_get_one = await self.client.get("/api/v1/scripts/some-id", cookies={})
140+
response_put = await self.client.put("/api/v1/scripts/some-id", json=script_data, cookies={})
141+
response_delete = await self.client.delete("/api/v1/scripts/some-id", cookies={})
140142

141143
assert response_post.status_code == 401
142144
assert response_get_list.status_code == 401

0 commit comments

Comments
 (0)