-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
193 lines (160 loc) · 5.41 KB
/
Copy pathconftest.py
File metadata and controls
193 lines (160 loc) · 5.41 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Global pytest fixtures and configuration for Sentinel tests.
This module provides:
- Mock LLM providers (Anthropic, OpenAI)
- Centralized test configuration
- Shared database fixtures
- Environment variable setup
All tests automatically use these fixtures and configuration.
"""
import json
import os
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
@pytest.fixture(scope="session", autouse=True)
def setup_test_environment() -> None:
"""Configure test environment variables globally."""
os.environ["SENTINEL_ENV"] = "test"
os.environ["LOG_LEVEL"] = "WARNING"
os.environ["ANTHROPIC_API_KEY"] = "test-key-sentinel"
os.environ["OPENAI_API_KEY"] = "test-key-sentinel"
@pytest.fixture
def mock_llm_response() -> dict[str, Any]:
"""Provide a mock LLM reasoning response."""
return {
"reasoning": "Entity shows high confidence pattern match.",
"score": 0.92,
"confidence": 0.88,
"cache_hit": False,
"tokens_used": 145,
}
@pytest.fixture
def mock_anthropic_client() -> MagicMock:
"""Create a mock Anthropic client that doesn't make real API calls."""
def mock_message_create(
model: str,
max_tokens: int,
system: str | None = None,
messages: list[dict[str, str]] | None = None,
**kwargs: Any,
) -> MagicMock:
"""Mock message creation with structured output support."""
response = MagicMock()
response.content = [
MagicMock(
type="text",
text=json.dumps(
{
"reasoning": "Test reasoning from Claude",
"score": 0.85,
"confidence": 0.80,
}
),
)
]
response.usage.input_tokens = 100
response.usage.output_tokens = 50
return response
client = MagicMock()
client.messages.create = MagicMock(side_effect=mock_message_create)
return client
@pytest.fixture
def mock_openai_client() -> MagicMock:
"""Create a mock OpenAI client that doesn't make real API calls."""
def mock_completion_create(
model: str,
messages: list[dict[str, str]] | None = None,
response_format: dict[str, str] | None = None,
max_tokens: int | None = None,
**kwargs: Any,
) -> MagicMock:
"""Mock completion creation."""
response = MagicMock()
response.choices = [
MagicMock(
message=MagicMock(
content=json.dumps(
{
"reasoning": "Test reasoning from GPT",
"score": 0.78,
"confidence": 0.75,
}
)
)
)
]
response.usage.prompt_tokens = 120
response.usage.completion_tokens = 60
return response
client = MagicMock()
client.chat.completions.create = MagicMock(side_effect=mock_completion_create)
return client
@pytest.fixture
def patch_llm_providers(mock_anthropic_client: MagicMock, mock_openai_client: MagicMock) -> Any:
"""Patch both LLM providers for all tests that need them."""
with (
patch("sentinel.llm.Anthropic", return_value=mock_anthropic_client),
patch("sentinel.llm.OpenAI", return_value=mock_openai_client),
):
yield
@pytest.fixture
def tmp_sqlite_db(tmp_path: Any) -> str:
"""Create a temporary SQLite database for testing.
Args:
tmp_path: pytest's temporary directory fixture
Returns:
Path to the test database file
"""
return str(tmp_path / "test_incidents.db")
@pytest.fixture(autouse=True)
def reset_logging() -> None:
"""Reset logging after each test."""
import logging
# Get all loggers and reset
for logger_name in logging.Logger.manager.loggerDict:
logger = logging.getLogger(logger_name)
logger.handlers.clear()
yield
# Clear handlers after test
for logger_name in logging.Logger.manager.loggerDict:
logger = logging.getLogger(logger_name)
logger.handlers.clear()
@pytest.fixture
def sample_incident_event() -> dict[str, Any]:
"""Provide sample incident event data."""
return {
"entity": "192.168.1.100",
"stage": "initial-compromise",
"confidence": 0.85,
"timestamp": "2024-01-15T10:30:00Z",
"detection_method": "behavioral-analysis",
"raw_data": {
"process_name": "svchost.exe",
"parent_process": "explorer.exe",
"command_line": "svchost.exe -k netsvcs",
},
}
@pytest.fixture
def sample_network_finding() -> dict[str, Any]:
"""Provide sample network detection data."""
return {
"source_ip": "192.168.1.50",
"destination_ip": "203.0.113.42",
"ja3_hash": "fake_ja3_hash_known_bad",
"certificate_issuer": "Untrusted CA",
"dns_query": "suspicious.malware.com",
"port": 443,
"protocol": "TLS",
}
@pytest.fixture
def sample_behavioral_profile() -> dict[str, Any]:
"""Provide sample behavioral profile for drift detection."""
return {
"entity": "workstation-42",
"time_window": "1h",
"process_count": 150,
"unique_processes": 45,
"network_connections": 32,
"failed_authentications": 0,
"anomaly_score": 0.12,
}