-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_main.py
More file actions
196 lines (139 loc) · 6.7 KB
/
test_main.py
File metadata and controls
196 lines (139 loc) · 6.7 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
import unittest
from unittest.mock import patch, MagicMock
from crowdstrike.foundry.function import Request
def mock_handler(*args, **kwargs):
def identity(func):
return func
return identity
class FnTestCase(unittest.TestCase):
def setUp(self):
patcher = patch("crowdstrike.foundry.function.Function.handler", new=mock_handler)
self.addCleanup(patcher.stop)
self.handler_patch = patcher.start()
import importlib
import main
importlib.reload(main)
@patch("main.validate_host_id")
@patch("main.format_error_response")
def test_on_post_success(self, mock_format_error, mock_validate_host_id):
from main import on_post
# Mock validation to return True for valid host ID
mock_validate_host_id.return_value = True
# Create mock logger
mock_logger = MagicMock()
request = Request()
request.body = {
"host_id": "valid-host-123"
}
response = on_post(request, config=None, logger=mock_logger)
self.assertEqual(response.code, 200)
self.assertEqual(response.body["host"], "valid-host-123")
# Verify validation was called twice (once for logging, once for condition)
self.assertEqual(mock_validate_host_id.call_count, 2)
mock_validate_host_id.assert_called_with("valid-host-123")
# Verify logger was called
self.assertEqual(mock_logger.info.call_count, 2)
mock_logger.info.assert_any_call("Host ID: valid-host-123")
mock_logger.info.assert_any_call("Is valid? True")
# Verify format_error_response was not called
mock_format_error.assert_not_called()
@patch("main.validate_host_id")
@patch("main.format_error_response")
def test_on_post_invalid_host_id(self, mock_format_error, mock_validate_host_id):
from main import on_post
# Mock validation to return False for invalid host ID
mock_validate_host_id.return_value = False
# Mock error response formatting
mock_format_error.return_value = [{"code": 400, "message": "Invalid host ID format"}]
# Create mock logger
mock_logger = MagicMock()
request = Request()
request.body = {
"host_id": "invalid-host"
}
response = on_post(request, config=None, logger=mock_logger)
# Should return error response (default code is likely 400)
self.assertEqual(response.errors, [{"code": 400, "message": "Invalid host ID format"}])
# Verify validation was called twice (once for logging, once for condition)
self.assertEqual(mock_validate_host_id.call_count, 2)
mock_validate_host_id.assert_called_with("invalid-host")
# Verify error formatting was called
mock_format_error.assert_called_once_with("Invalid host ID format")
# Verify logger was called
self.assertEqual(mock_logger.info.call_count, 2)
mock_logger.info.assert_any_call("Host ID: invalid-host")
mock_logger.info.assert_any_call("Is valid? False")
@patch("main.validate_host_id")
@patch("main.format_error_response")
def test_on_post_missing_host_id(self, mock_format_error, mock_validate_host_id):
from main import on_post
# Mock validation to return False for None host ID
mock_validate_host_id.return_value = False
# Mock error response formatting
mock_format_error.return_value = [{"code": 400, "message": "Invalid host ID format"}]
# Create mock logger
mock_logger = MagicMock()
request = Request()
request.body = {} # No host_id provided
response = on_post(request, config=None, logger=mock_logger)
# Should return error response
self.assertEqual(response.errors, [{"code": 400, "message": "Invalid host ID format"}])
# Verify validation was called twice (once for logging, once for condition)
self.assertEqual(mock_validate_host_id.call_count, 2)
mock_validate_host_id.assert_called_with(None)
# Verify error formatting was called
mock_format_error.assert_called_once_with("Invalid host ID format")
# Verify logger was called with None
self.assertEqual(mock_logger.info.call_count, 2)
mock_logger.info.assert_any_call("Host ID: None")
mock_logger.info.assert_any_call("Is valid? False")
@patch("main.validate_host_id")
@patch("main.format_error_response")
def test_on_post_empty_host_id(self, mock_format_error, mock_validate_host_id):
from main import on_post
# Mock validation to return False for empty string
mock_validate_host_id.return_value = False
# Mock error response formatting
mock_format_error.return_value = [{"code": 400, "message": "Invalid host ID format"}]
# Create mock logger
mock_logger = MagicMock()
request = Request()
request.body = {
"host_id": ""
}
response = on_post(request, config=None, logger=mock_logger)
# Should return error response
self.assertEqual(response.errors, [{"code": 400, "message": "Invalid host ID format"}])
# Verify validation was called twice (once for logging, once for condition)
self.assertEqual(mock_validate_host_id.call_count, 2)
mock_validate_host_id.assert_called_with("")
# Verify error formatting was called
mock_format_error.assert_called_once_with("Invalid host ID format")
# Verify logger was called with empty string
self.assertEqual(mock_logger.info.call_count, 2)
mock_logger.info.assert_any_call("Host ID: ")
mock_logger.info.assert_any_call("Is valid? False")
@patch("main.validate_host_id")
@patch("main.format_error_response")
def test_on_post_with_config(self, mock_format_error, mock_validate_host_id):
from main import on_post
# Mock validation to return True
mock_validate_host_id.return_value = True
# Create mock logger
mock_logger = MagicMock()
# Test with config parameter
config = {"some_setting": "value"}
request = Request()
request.body = {
"host_id": "test-host-456"
}
response = on_post(request, config=config, logger=mock_logger)
self.assertEqual(response.code, 200)
self.assertEqual(response.body["host"], "test-host-456")
# Verify validation was called twice (once for logging, once for condition)
self.assertEqual(mock_validate_host_id.call_count, 2)
mock_validate_host_id.assert_called_with("test-host-456")
# Verify logger was called
self.assertEqual(mock_logger.info.call_count, 2)
if __name__ == "__main__":
unittest.main()