-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconftest.py
More file actions
256 lines (208 loc) · 7.3 KB
/
Copy pathconftest.py
File metadata and controls
256 lines (208 loc) · 7.3 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
from collections.abc import Generator
from pathlib import Path
from typing import Any
import pytest
from pytest import TempPathFactory
from guard_core.models import SecurityConfig
from guard_core.sync.handlers.cloud_handler import cloud_handler
from guard_core.sync.handlers.ipban_handler import IPBanManager
from guard_core.sync.handlers.ipinfo_handler import IPInfoManager
from guard_core.sync.handlers.ratelimit_handler import rate_limit_handler
from guard_core.sync.handlers.redis_handler import RedisManager
from guard_core.sync.handlers.suspatterns_handler import sus_patterns_handler
IPINFO_TOKEN = os.getenv("IPINFO_TOKEN") or "test_token"
REDIS_URL = os.getenv("REDIS_URL") or "redis://localhost:6379"
REDIS_PREFIX = os.getenv("REDIS_PREFIX") or "test:guard_core:"
class MockState:
def __init__(self) -> None:
self._attrs: dict[str, Any] = {}
def __getattr__(self, name: str) -> Any:
if name == "_attrs":
return super().__getattribute__(name)
return self._attrs.get(name)
def __setattr__(self, name: str, value: Any) -> None:
if name == "_attrs":
super().__setattr__(name, value)
else:
self._attrs[name] = value
class SyncMockGuardRequest:
def __init__(
self,
path: str = "/",
method: str = "GET",
headers: dict[str, str] | None = None,
client_host: str | None = "127.0.0.1",
scheme: str = "https",
query_params: dict[str, str] | None = None,
body_content: bytes = b"",
scope: dict[str, Any] | None = None,
) -> None:
self._path = path
self._method = method
self._headers = headers or {}
self._client_host = client_host
self._scheme = scheme
self._query_params = query_params or {}
self._body = body_content
self._state = MockState()
self._scope = scope or {}
@property
def url_path(self) -> str:
return self._path
@property
def url_scheme(self) -> str:
return self._scheme
@property
def url_full(self) -> str:
return f"{self._scheme}://test{self._path}"
def url_replace_scheme(self, scheme: str) -> str:
return f"{scheme}://test{self._path}"
@property
def method(self) -> str:
return self._method
@property
def client_host(self) -> str | None:
return self._client_host
@property
def headers(self) -> dict[str, str]:
return self._headers
@property
def query_params(self) -> dict[str, str]:
return self._query_params
def body(self) -> bytes:
return self._body
@property
def state(self) -> MockState:
return self._state
@property
def scope(self) -> dict[str, Any]:
return self._scope
class MockGuardResponse:
def __init__(
self,
content: str = "",
status_code: int = 200,
headers: dict[str, str] | None = None,
) -> None:
self._status_code = status_code
self._headers: dict[str, str] = headers or {}
self._body = content.encode() if isinstance(content, str) else content
@property
def status_code(self) -> int:
return self._status_code
@property
def headers(self) -> dict[str, str]:
return self._headers
@property
def body(self) -> bytes:
return self._body
class MockGuardResponseFactory:
def create_response(self, content: str, status_code: int) -> MockGuardResponse:
return MockGuardResponse(content, status_code)
def create_redirect_response(self, url: str, status_code: int) -> MockGuardResponse:
return MockGuardResponse(f"Redirect to {url}", status_code, {"Location": url})
@pytest.fixture(autouse=True)
def reset_state() -> Generator[None, None]:
IPBanManager._instance = None
cloud_instance = cloud_handler._instance
if cloud_instance:
from guard_core.sync.handlers.cloud_ip_stores import InMemoryCloudIpStore
cloud_instance.ip_ranges = {"AWS": set(), "GCP": set(), "Azure": set()}
cloud_instance.redis_handler = None
cloud_instance.agent_handler = None
cloud_instance._store = InMemoryCloudIpStore()
if IPInfoManager._instance:
if IPInfoManager._instance.reader:
IPInfoManager._instance.reader.close()
IPInfoManager._instance.agent_handler = None
IPInfoManager._instance = None
yield
spm = type(sus_patterns_handler)
spm._instance = sus_patterns_handler
spm._config = None
sus_patterns_handler.patterns = [p[0] for p in spm._pattern_definitions]
sus_patterns_handler.custom_patterns = set()
sus_patterns_handler.compiled_custom_patterns = set()
IPBanManager._instance = None
@pytest.fixture
def security_config() -> SecurityConfig:
return SecurityConfig(
enable_redis=False,
whitelist=["127.0.0.1"],
blacklist=["192.168.1.1"],
blocked_user_agents=[r"badbot"],
auto_ban_threshold=3,
auto_ban_duration=300,
custom_log_file="test_log.log",
custom_error_responses={
403: "Custom Forbidden",
429: "Custom Too Many Requests",
},
enable_cors=True,
cors_allow_origins=["https://example.com"],
cors_allow_methods=["GET", "POST"],
cors_allow_headers=["*"],
cors_allow_credentials=True,
cors_expose_headers=["X-Custom-Header"],
cors_max_age=600,
)
@pytest.fixture(scope="session")
def ipinfo_db_path(tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("ipinfo_data") / "country_asn.mmdb"
@pytest.fixture
def security_config_redis(ipinfo_db_path: Path) -> SecurityConfig:
return SecurityConfig(
redis_url=REDIS_URL,
redis_prefix=REDIS_PREFIX,
whitelist=["127.0.0.1"],
blacklist=["192.168.1.1"],
blocked_user_agents=[r"badbot"],
auto_ban_threshold=3,
auto_ban_duration=300,
custom_log_file="test_log.log",
custom_error_responses={
403: "Custom Forbidden",
429: "Custom Too Many Requests",
},
enable_cors=True,
cors_allow_origins=["https://example.com"],
cors_allow_methods=["GET", "POST"],
cors_allow_headers=["*"],
cors_allow_credentials=True,
cors_expose_headers=["X-Custom-Header"],
cors_max_age=600,
)
@pytest.fixture(autouse=True)
def redis_cleanup() -> Generator[None, None]:
config = SecurityConfig(
redis_url=REDIS_URL,
redis_prefix=REDIS_PREFIX,
)
redis_handler = RedisManager(config)
redis_handler.initialize()
try:
redis_handler.delete_pattern(f"{REDIS_PREFIX}*")
except Exception:
pass
finally:
redis_handler.close()
yield
redis_handler = RedisManager(config)
redis_handler.initialize()
try:
redis_handler.delete_pattern(f"{REDIS_PREFIX}*")
except Exception:
pass
finally:
redis_handler.close()
@pytest.fixture(autouse=True)
def reset_rate_limiter() -> Generator[None, None]:
config = SecurityConfig(enable_redis=False)
rate_limit = rate_limit_handler(config)
rate_limit.reset()
yield
@pytest.fixture
def clean_rate_limiter() -> None:
from guard_core.sync.handlers.ratelimit_handler import RateLimitManager
RateLimitManager._instance = None