Skip to content

Commit 32e4d24

Browse files
committed
fix tests 2
1 parent 227eb35 commit 32e4d24

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

backend/app/api/routes/auth.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ async def login(
6565
)
6666

6767
settings = get_settings()
68-
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
69-
access_token = security_service.create_access_token(
70-
data={"sub": user.username}, expires_delta=access_token_expires
71-
)
7268

7369
logger.info(
7470
"Login successful",
@@ -80,6 +76,14 @@ async def login(
8076
},
8177
)
8278

79+
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
80+
access_token = security_service.create_access_token(
81+
data={"sub": user.username}, expires_delta=access_token_expires
82+
)
83+
84+
session_id = security_service.get_session_id_from_request(request)
85+
csrf_token = security_service.generate_csrf_token(session_id)
86+
8387
# Set httpOnly cookie for secure token storage
8488
response.set_cookie(
8589
key="access_token",
@@ -91,10 +95,6 @@ async def login(
9195
path="/",
9296
)
9397

94-
# Generate CSRF token for the session
95-
session_id = security_service.get_session_id_from_request(request)
96-
csrf_token = security_service.generate_csrf_token(session_id)
97-
9898
return {"message": "Login successful", "username": user.username, "csrf_token": csrf_token}
9999

100100

backend/tests/integration/test_auth_api.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
2+
import httpx
33
import pytest
44
from app.schemas.user import UserCreate
55
from httpx import AsyncClient
@@ -117,12 +117,21 @@ async def test_verify_token_valid(self) -> None:
117117
@pytest.mark.asyncio
118118
async def test_verify_token_invalid_token(self) -> None:
119119
"""Verify an invalid/malformed token fails."""
120-
# Clear cookies to simulate invalid token
121-
response = await self.client.get("/api/v1/verify-token", cookies={})
122-
assert response.status_code == 401
120+
async with httpx.AsyncClient(
121+
base_url="https://localhost:443",
122+
verify=False,
123+
timeout=30.0
124+
) as new_client:
125+
response = await new_client.get("/api/v1/verify-token")
126+
assert response.status_code == 401
123127

124128
@pytest.mark.asyncio
125129
async def test_verify_token_no_token(self) -> None:
126130
"""Verify request fails without token."""
127-
response = await self.client.get("/api/v1/verify-token", cookies={}) # No cookies
128-
assert response.status_code == 401
131+
async with httpx.AsyncClient(
132+
base_url="https://localhost:443",
133+
verify=False,
134+
timeout=30.0
135+
) as new_client:
136+
response = await new_client.get("/api/v1/verify-token")
137+
assert response.status_code == 401

backend/tests/integration/test_execution_api.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import asyncio
22
import time
3-
3+
import httpx
44
import pytest
55
from app.schemas.user import UserCreate
66
from httpx import AsyncClient, HTTPStatusError
77
from motor.motor_asyncio import AsyncIOMotorDatabase
88

9-
# Define polling parameters
109
POLL_INTERVAL = 2 # seconds
1110
EXECUTION_TIMEOUT = 120 # seconds
1211

@@ -146,16 +145,28 @@ async def test_k8s_resource_limits(self) -> None:
146145
@pytest.mark.asyncio
147146
async def test_execute_endpoint_without_auth(self) -> None:
148147
"""Test accessing execute endpoint without authentication (should succeed)."""
149-
execution_request = {"script": "print('no auth test should pass')"}
150-
response = await self.client.post("/api/v1/execute", json=execution_request, cookies={}) # No cookies
151-
# Expect 200 OK because the endpoint is public
152-
assert response.status_code == 200
153-
assert "execution_id" in response.json()
154-
assert "status" in response.json()
148+
async with httpx.AsyncClient(
149+
base_url="https://localhost:443",
150+
verify=False,
151+
timeout=30.0
152+
) as new_client:
153+
execution_request = {"script": "print('no auth test should pass')"}
154+
response = await new_client.post("/api/v1/execute", json=execution_request)
155+
# Expect 200 OK because the endpoint is public
156+
assert response.status_code == 200
157+
assert "execution_id" in response.json()
158+
assert "status" in response.json()
155159

156160
@pytest.mark.asyncio
157161
async def test_result_endpoint_without_auth(self) -> None:
158-
non_existent_id = "nonexistent-public-id-999"
159-
response = await self.client.get(f"/api/v1/result/{non_existent_id}", cookies={}) # No cookies
160-
# Expect 404 Not Found because the ID doesn't exist, *not* 401 because the endpoint is public
161-
assert response.status_code == 404
162+
# Create a new client without cookies to simulate no auth
163+
import httpx
164+
async with httpx.AsyncClient(
165+
base_url="https://localhost:443",
166+
verify=False,
167+
timeout=30.0
168+
) as new_client:
169+
non_existent_id = "nonexistent-public-id-999"
170+
response = await new_client.get(f"/api/v1/result/{non_existent_id}")
171+
# Expect 404 Not Found because the ID doesn't exist, *not* 401 because the endpoint is public
172+
assert response.status_code == 404

backend/tests/integration/test_saved_scripts_api.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
2+
import httpx
33
import pytest
44
from app.schemas.user import UserCreate
55
from httpx import AsyncClient
@@ -132,13 +132,17 @@ async def test_list_scripts_empty(self) -> None:
132132
@pytest.mark.asyncio
133133
async def test_scripts_endpoints_without_auth(self) -> None:
134134
"""Test accessing scripts endpoints without authentication."""
135-
script_data = {"name": "No Auth", "script": "print('no')"}
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={})
135+
async with httpx.AsyncClient(
136+
base_url="https://localhost:443",
137+
verify=False,
138+
timeout=30.0
139+
) as new_client:
140+
script_data = {"name": "No Auth", "script": "print('no')"}
141+
response_post = await new_client.post("/api/v1/scripts", json=script_data)
142+
response_get_list = await new_client.get("/api/v1/scripts")
143+
response_get_one = await new_client.get("/api/v1/scripts/some-id")
144+
response_put = await new_client.put("/api/v1/scripts/some-id", json=script_data)
145+
response_delete = await new_client.delete("/api/v1/scripts/some-id")
142146

143147
assert response_post.status_code == 401
144148
assert response_get_list.status_code == 401

0 commit comments

Comments
 (0)