Skip to content

Commit cb94426

Browse files
ambvclaude
andcommitted
Add tests for token authentication
Covers Bearer and Token header formats, invalid tokens, missing tokens, and deactivated token rejection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3ae11f1 commit cb94426

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

backend/tests/test_auth.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Tests for token authentication."""
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_valid_bearer_token(client, auth_headers, sample_binary, sample_environment):
8+
"""A valid Bearer token should authenticate successfully."""
9+
response = await client.get("/api/binaries")
10+
assert response.status_code == 200
11+
12+
13+
@pytest.mark.asyncio
14+
async def test_upload_with_invalid_token(client, sample_binary, sample_environment):
15+
"""An invalid token should be rejected."""
16+
headers = {"Authorization": "Bearer invalid_token_value"}
17+
response = await client.post(
18+
"/api/upload-run",
19+
json={"metadata": {}, "benchmark_results": [], "binary_id": "x", "environment_id": "y"},
20+
headers=headers,
21+
)
22+
assert response.status_code in (401, 403)
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_upload_with_no_token(client):
27+
"""Missing token should be rejected."""
28+
response = await client.post(
29+
"/api/upload-run",
30+
json={"metadata": {}, "benchmark_results": [], "binary_id": "x", "environment_id": "y"},
31+
)
32+
assert response.status_code in (401, 403)
33+
34+
35+
@pytest.mark.asyncio
36+
async def test_token_format_bearer(client, auth_token, sample_binary, sample_environment):
37+
"""'Bearer <token>' format should work."""
38+
raw_token, _ = auth_token
39+
headers = {"Authorization": f"Bearer {raw_token}"}
40+
# Use upload endpoint since it requires auth
41+
response = await client.post(
42+
"/api/report-memray-failure",
43+
json={
44+
"commit_sha": "d" * 40,
45+
"commit_timestamp": "2025-06-16T10:00:00",
46+
"binary_id": "default",
47+
"environment_id": "linux-x86_64",
48+
"error_message": "test",
49+
},
50+
headers=headers,
51+
)
52+
assert response.status_code == 200
53+
54+
55+
@pytest.mark.asyncio
56+
async def test_token_format_token_prefix(client, auth_token, sample_binary, sample_environment):
57+
"""'Token <token>' format should also work."""
58+
raw_token, _ = auth_token
59+
headers = {"Authorization": f"Token {raw_token}"}
60+
response = await client.post(
61+
"/api/report-memray-failure",
62+
json={
63+
"commit_sha": "e" * 40,
64+
"commit_timestamp": "2025-06-16T10:00:00",
65+
"binary_id": "default",
66+
"environment_id": "linux-x86_64",
67+
"error_message": "test",
68+
},
69+
headers=headers,
70+
)
71+
assert response.status_code == 200
72+
73+
74+
@pytest.mark.asyncio
75+
async def test_inactive_token_rejected(client, db_session, auth_token, sample_binary, sample_environment):
76+
"""A deactivated token should be rejected."""
77+
raw_token, token_model = auth_token
78+
token_model.is_active = False
79+
await db_session.commit()
80+
81+
headers = {"Authorization": f"Bearer {raw_token}"}
82+
response = await client.post(
83+
"/api/report-memray-failure",
84+
json={
85+
"commit_sha": "f" * 40,
86+
"commit_timestamp": "2025-06-16T10:00:00",
87+
"binary_id": "default",
88+
"environment_id": "linux-x86_64",
89+
"error_message": "test",
90+
},
91+
headers=headers,
92+
)
93+
assert response.status_code in (401, 403)

0 commit comments

Comments
 (0)