-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_main.py
More file actions
90 lines (68 loc) · 2.71 KB
/
test_main.py
File metadata and controls
90 lines (68 loc) · 2.71 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
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.Hosts")
def test_on_post_success(self, mock_hosts_class):
from main import on_post
# Mock the Hosts instance and its response
mock_hosts_instance = MagicMock()
mock_hosts_class.return_value = mock_hosts_instance
mock_hosts_instance.get_device_details.return_value = {
"status_code": 200,
"body": {
"resources": [{
"device_id": "test-host-123",
"hostname": "test-host",
"platform_name": "Windows"
}]
}
}
request = Request()
request.body = {
"host_id": "test-host-123"
}
response = on_post(request)
self.assertEqual(response.code, 200)
self.assertIn("host_details", response.body)
self.assertEqual(response.body["host_details"]["device_id"], "test-host-123")
mock_hosts_instance.get_device_details.assert_called_once_with(ids="test-host-123")
def test_on_post_missing_host_id(self):
from main import on_post
request = Request()
response = on_post(request)
self.assertEqual(response.code, 400)
self.assertEqual(len(response.errors), 1)
self.assertEqual(response.errors[0].message, "missing host_id from request body")
@patch("main.Hosts")
def test_on_post_api_error(self, mock_hosts_class):
from main import on_post
# Mock the Hosts instance to return an error
mock_hosts_instance = MagicMock()
mock_hosts_class.return_value = mock_hosts_instance
mock_hosts_instance.get_device_details.return_value = {
"status_code": 404,
"body": {"errors": [{"message": "Host not found"}]}
}
request = Request()
request.body = {
"host_id": "nonexistent-host"
}
response = on_post(request)
self.assertEqual(response.code, 404)
self.assertEqual(len(response.errors), 1)
self.assertIn("Error retrieving host:", response.errors[0].message)
mock_hosts_instance.get_device_details.assert_called_once_with(ids="nonexistent-host")
if __name__ == "__main__":
unittest.main()