-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_auth.py
More file actions
100 lines (85 loc) · 3.39 KB
/
Copy pathtest_auth.py
File metadata and controls
100 lines (85 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Tests for token authentication."""
async def test_valid_bearer_token(client, auth_headers, sample_binary, sample_environment):
"""A valid Bearer token should authenticate successfully on a protected endpoint."""
payload = {
"commit_sha": "a" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "test",
}
# Without auth → rejected
response = await client.post("/api/report-memray-failure", json=payload)
assert response.status_code in (401, 403)
# With auth → accepted
response = await client.post(
"/api/report-memray-failure", json=payload, headers=auth_headers
)
assert response.status_code == 200
async def test_upload_with_invalid_token(client, sample_binary, sample_environment):
"""An invalid token should be rejected."""
headers = {"Authorization": "Bearer invalid_token_value"}
response = await client.post(
"/api/upload-run",
json={"metadata": {}, "benchmark_results": [], "binary_id": "x", "environment_id": "y"},
headers=headers,
)
assert response.status_code in (401, 403)
async def test_upload_with_no_token(client):
"""Missing token should be rejected."""
response = await client.post(
"/api/upload-run",
json={"metadata": {}, "benchmark_results": [], "binary_id": "x", "environment_id": "y"},
)
assert response.status_code in (401, 403)
async def test_token_format_bearer(client, auth_token, sample_binary, sample_environment):
"""'Bearer <token>' format should work."""
raw_token, _ = auth_token
headers = {"Authorization": f"Bearer {raw_token}"}
# Use upload endpoint since it requires auth
response = await client.post(
"/api/report-memray-failure",
json={
"commit_sha": "d" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "test",
},
headers=headers,
)
assert response.status_code == 200
async def test_token_format_token_prefix(client, auth_token, sample_binary, sample_environment):
"""'Token <token>' format should also work."""
raw_token, _ = auth_token
headers = {"Authorization": f"Token {raw_token}"}
response = await client.post(
"/api/report-memray-failure",
json={
"commit_sha": "e" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "test",
},
headers=headers,
)
assert response.status_code == 200
async def test_inactive_token_rejected(client, db_session, auth_token, sample_binary, sample_environment):
"""A deactivated token should be rejected."""
raw_token, token_model = auth_token
token_model.is_active = False
await db_session.commit()
headers = {"Authorization": f"Bearer {raw_token}"}
response = await client.post(
"/api/report-memray-failure",
json={
"commit_sha": "f" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "test",
},
headers=headers,
)
assert response.status_code in (401, 403)