Skip to content

Commit 6aa20e6

Browse files
ambvclaude
andcommitted
Add tests for public endpoints and logging utilities
Public: maintainers listing, memray status (healthy and with failures). Logging: string/dict/list sanitization, token masking, safe log context. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cb94426 commit 6aa20e6

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""Tests for logging utility functions."""
2+
3+
from app.logging_utils import (
4+
sanitize_string,
5+
sanitize_dict,
6+
sanitize_list,
7+
mask_token,
8+
create_safe_log_context,
9+
)
10+
11+
12+
def test_sanitize_string_redacts_hex_token():
13+
token = "a" * 64
14+
result = sanitize_string(f"Token is {token}")
15+
assert token not in result
16+
assert "REDACTED" in result
17+
18+
19+
def test_sanitize_string_redacts_password():
20+
result = sanitize_string('password="supersecret"')
21+
assert "supersecret" not in result
22+
assert "REDACTED" in result
23+
24+
25+
def test_sanitize_string_preserves_normal_text():
26+
text = "This is a normal log message"
27+
assert sanitize_string(text) == text
28+
29+
30+
def test_sanitize_string_handles_non_string():
31+
assert sanitize_string(42) == 42
32+
33+
34+
def test_sanitize_dict_redacts_sensitive_keys():
35+
data = {"token": "secret123", "name": "worker-1"}
36+
result = sanitize_dict(data)
37+
assert result["token"] == "***REDACTED***"
38+
assert result["name"] == "worker-1"
39+
40+
41+
def test_sanitize_dict_recursive():
42+
data = {"config": {"auth": {"password": "secret"}}}
43+
result = sanitize_dict(data)
44+
assert "secret" not in str(result)
45+
46+
47+
def test_sanitize_dict_handles_non_dict():
48+
assert sanitize_dict("not a dict") == "not a dict"
49+
50+
51+
def test_sanitize_list_handles_mixed():
52+
data = [{"token": "secret"}, "normal", 42]
53+
result = sanitize_list(data)
54+
assert result[0]["token"] == "***REDACTED***"
55+
assert result[1] == "normal"
56+
assert result[2] == 42
57+
58+
59+
def test_mask_token_normal():
60+
assert mask_token("abcdefghijklmnop") == "abcd...mnop"
61+
62+
63+
def test_mask_token_short():
64+
assert mask_token("short") == "***"
65+
66+
67+
def test_mask_token_empty():
68+
assert mask_token("") == "***"
69+
assert mask_token(None) == "***"
70+
71+
72+
def test_create_safe_log_context_redacts_keys():
73+
ctx = create_safe_log_context(token="secret", name="worker")
74+
assert ctx["token"] == "***REDACTED***"
75+
assert ctx["name"] == "worker"
76+
77+
78+
def test_create_safe_log_context_truncates_long_strings():
79+
long_value = "x" * 100
80+
ctx = create_safe_log_context(data=long_value)
81+
assert len(ctx["data"]) < 100
82+
assert ctx["data"].endswith("...")

backend/tests/test_public.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Tests for public API endpoints."""
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_maintainers_empty(client):
8+
response = await client.get("/api/maintainers")
9+
assert response.status_code == 200
10+
assert response.json() == []
11+
12+
13+
@pytest.mark.asyncio
14+
async def test_maintainers_with_admin(client, admin_user):
15+
response = await client.get("/api/maintainers")
16+
assert response.status_code == 200
17+
data = response.json()
18+
assert len(data) == 1
19+
assert data[0]["github_username"] == "test_admin"
20+
assert data[0]["is_active"] is True
21+
22+
23+
@pytest.mark.asyncio
24+
async def test_memray_status_healthy(client):
25+
response = await client.get("/api/memray-status")
26+
assert response.status_code == 200
27+
data = response.json()
28+
assert data["has_failures"] is False
29+
assert data["failure_count"] == 0
30+
assert data["affected_environments"] == []
31+
32+
33+
@pytest.mark.asyncio
34+
async def test_memray_status_with_failure(
35+
client, auth_headers, sample_binary, sample_environment
36+
):
37+
# Report a failure first
38+
await client.post(
39+
"/api/report-memray-failure",
40+
json={
41+
"commit_sha": "c" * 40,
42+
"commit_timestamp": "2025-06-16T10:00:00",
43+
"binary_id": "default",
44+
"environment_id": "linux-x86_64",
45+
"error_message": "memray install failed",
46+
},
47+
headers=auth_headers,
48+
)
49+
50+
response = await client.get("/api/memray-status")
51+
assert response.status_code == 200
52+
data = response.json()
53+
assert data["has_failures"] is True
54+
assert data["failure_count"] == 1
55+
assert data["affected_environments"][0]["binary_id"] == "default"

0 commit comments

Comments
 (0)