-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconftest.py
315 lines (228 loc) · 7.35 KB
/
conftest.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import json
from datetime import datetime
from datetime import timedelta
import pytest
from click.testing import CliRunner
from py42.response import Py42Response
from py42.sdk import SDKClient
from requests import HTTPError
from requests import Response
import code42cli.errors as error_tracker
from code42cli.config import ConfigAccessor
from code42cli.options import CLIState
from code42cli.profile import Code42Profile
TEST_ID = "TEST_ID"
@pytest.fixture(scope="session")
def runner():
return CliRunner()
@pytest.fixture(autouse=True)
def io_prevention(monkeypatch):
monkeypatch.setattr("logging.FileHandler._open", lambda *args, **kwargs: None)
@pytest.fixture
def file_event_namespace():
args = dict(
sdk=mock_42,
profile=create_mock_profile(),
incremental=None,
advanced_query=None,
begin=None,
end=None,
type=None,
c42_username=None,
actor=None,
md5=None,
sha256=None,
source=None,
file_name=None,
file_path=None,
process_owner=None,
tab_url=None,
include_non_exposure=None,
format=None,
output_file=None,
server=None,
protocol=None,
saved_search=None,
)
return args
@pytest.fixture
def alert_namespace():
args = dict(
sdk=mock_42,
profile=create_mock_profile(),
incremental=None,
advanced_query=None,
begin=None,
end=None,
severity=None,
state=None,
actor=None,
actor_contains=None,
exclude_actor=None,
exclude_actor_contains=None,
rule_name=None,
exclude_rule_name=None,
rule_id=None,
exclude_rule_id=None,
rule_type=None,
exclude_rule_type=None,
description=None,
format=None,
output_file=None,
server=None,
protocol=None,
)
return args
def create_profile_values_dict(
authority=None,
username=None,
ignore_ssl=False,
use_v2_file_events=False,
api_client_auth="False",
):
return {
ConfigAccessor.AUTHORITY_KEY: "example.com",
ConfigAccessor.USERNAME_KEY: "foo",
ConfigAccessor.IGNORE_SSL_ERRORS_KEY: "True",
ConfigAccessor.USE_V2_FILE_EVENTS_KEY: "False",
ConfigAccessor.API_CLIENT_AUTH_KEY: "False",
}
@pytest.fixture
def sdk(mocker):
return mocker.MagicMock(spec=SDKClient)
@pytest.fixture
def sdk_with_user(sdk):
sdk.users.get_by_username.return_value = {"users": [{"userUid": TEST_ID}]}
return sdk
@pytest.fixture
def sdk_without_user(sdk):
sdk.users.get_by_username.return_value = {"users": []}
return sdk
@pytest.fixture
def mock_42(mocker):
return mocker.patch("py42.sdk.from_local_account")
@pytest.fixture
def cli_state(mocker, sdk, profile):
mock_state = mocker.MagicMock(spec=CLIState)
mock_state._sdk = sdk
mock_state.profile = profile
mock_state.search_filters = []
mock_state.totp = None
mock_state.assume_yes = False
return mock_state
class MockSection:
def __init__(self, name=None, values_dict=None):
self.name = name
self.values_dict = values_dict or create_profile_values_dict()
def __getitem__(self, item):
return self.values_dict[item]
def __setitem__(self, key, value):
self.values_dict[key] = value
def get(self, item):
return self.values_dict.get(item)
def create_mock_profile(name="Test Profile Name"):
profile_section = MockSection(name)
profile = Code42Profile(profile_section)
return profile
def setup_mock_accessor(mock_accessor, name=None, values_dict=None):
profile_section = MockSection(name, values_dict)
mock_accessor.get_profile.return_value = profile_section
return mock_accessor
@pytest.fixture
def profile(mocker):
mock = mocker.MagicMock(spec=Code42Profile)
mock.name = "testcliprofile"
return mock
@pytest.fixture(autouse=True)
def mock_makedirs(mocker):
return mocker.patch("os.makedirs")
@pytest.fixture(autouse=True)
def mock_remove(mocker):
return mocker.patch("os.remove")
@pytest.fixture(autouse=True)
def mock_listdir(mocker):
return mocker.patch("os.listdir")
def func_keyword_args(
one=None, two=None, three=None, default="testdefault", nargstest=None
):
pass
def func_single_positional_arg(one):
pass
def func_single_positional_arg_many_optional_args(one, two=None, three=None, four=None):
pass
def func_positional_args(one, two, three):
pass
def func_mixed_args(one, two, three=None, four=None):
pass
def func_with_sdk(sdk, one, two, three=None, four=None):
pass
def func_single_positional_arg_with_sdk_and_profile(
sdk, profile, one, two=None, three=None, four=None
):
pass
def func_with_args(args):
pass
def convert_str_to_date(date_str):
return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ")
def get_test_date(days_ago=None, hours_ago=None, minutes_ago=None):
"""Note: only pass in one parameter to get the right test date... this is just a test func."""
now = datetime.utcnow()
if days_ago:
return now - timedelta(days=days_ago)
if hours_ago:
return now - timedelta(hours=hours_ago)
if minutes_ago:
return now - timedelta(minutes=minutes_ago)
def get_test_date_str(days_ago):
return get_test_date(days_ago).strftime("%Y-%m-%d")
begin_date_str = get_test_date_str(days_ago=89)
begin_date_str_with_time = f"{begin_date_str} 3:12:33"
begin_date_str_with_t_time = f"{begin_date_str}T3:12:33"
end_date_str = get_test_date_str(days_ago=10)
end_date_str_with_time = f"{end_date_str} 11:22:43"
begin_date_str = get_test_date_str(days_ago=89)
begin_date_with_time = [get_test_date_str(days_ago=89), "3:12:33"]
end_date_str = get_test_date_str(days_ago=10)
end_date_with_time = [get_test_date_str(days_ago=10), "11:22:43"]
class ErrorTrackerTestHelper:
def __enter__(self):
error_tracker.ERRORED = True
def __exit__(self, exc_type, exc_val, exc_tb):
error_tracker.ERRORED = False
TEST_FILE_PATH = "some/path"
@pytest.fixture
def mock_to_table(mocker):
return mocker.patch("code42cli.output_formats.to_table")
@pytest.fixture
def mock_to_csv(mocker):
return mocker.patch("code42cli.output_formats.to_csv")
@pytest.fixture
def mock_to_json(mocker):
return mocker.patch("code42cli.output_formats.to_json")
@pytest.fixture
def mock_to_formatted_json(mocker):
return mocker.patch("code42cli.output_formats.to_formatted_json")
@pytest.fixture
def mock_dataframe_to_json(mocker):
return mocker.patch("pandas.DataFrame.to_json")
@pytest.fixture
def mock_dataframe_to_csv(mocker):
return mocker.patch("pandas.DataFrame.to_csv")
@pytest.fixture
def mock_dataframe_to_string(mocker):
return mocker.patch("pandas.DataFrame.to_string")
def create_mock_response(mocker, data=None, status=200):
if isinstance(data, (dict, list)):
data = json.dumps(data)
elif not data:
data = ""
response = mocker.MagicMock(spec=Response)
response.text = data
response.status_code = status
response.encoding = None
response._content_consumed = ""
return Py42Response(response)
def create_mock_http_error(mocker, data=None, status=400):
mock_http_error = mocker.MagicMock(spec=HTTPError)
mock_http_error.response = create_mock_response(mocker, data=data, status=status)
return mock_http_error