1+ """Test module for the host-info function handler."""
2+
3+ import importlib
14import unittest
25from unittest .mock import patch , MagicMock
36
47from crowdstrike .foundry .function import Request
58
9+ import main
10+
11+
12+ def mock_handler (* _args , ** _kwargs ):
13+ """Mock handler decorator for testing."""
614
7- def mock_handler (* args , ** kwargs ):
815 def identity (func ):
916 return func
1017
1118 return identity
1219
1320
1421class FnTestCase (unittest .TestCase ):
22+ """Test case class for function handler tests."""
23+
1524 def setUp (self ):
25+ """Set up test fixtures before each test method."""
1626 patcher = patch ("crowdstrike.foundry.function.Function.handler" , new = mock_handler )
1727 self .addCleanup (patcher .stop )
1828 self .handler_patch = patcher .start ()
1929
20- import importlib
21- import main
2230 importlib .reload (main )
2331
2432 @patch ("main.validate_host_id" )
2533 @patch ("main.format_error_response" )
2634 def test_on_post_success (self , mock_format_error , mock_validate_host_id ):
27- from main import on_post
28-
35+ """Test successful POST request with valid host_id in body."""
2936 # Mock validation to return True for valid host ID
3037 mock_validate_host_id .return_value = True
3138
@@ -37,7 +44,7 @@ def test_on_post_success(self, mock_format_error, mock_validate_host_id):
3744 "host_id" : "valid-host-123"
3845 }
3946
40- response = on_post (request , config = None , logger = mock_logger )
47+ response = main . on_post (request , _config = None , logger = mock_logger )
4148
4249 self .assertEqual (response .code , 200 )
4350 self .assertEqual (response .body ["host" ], "valid-host-123" )
@@ -57,8 +64,7 @@ def test_on_post_success(self, mock_format_error, mock_validate_host_id):
5764 @patch ("main.validate_host_id" )
5865 @patch ("main.format_error_response" )
5966 def test_on_post_invalid_host_id (self , mock_format_error , mock_validate_host_id ):
60- from main import on_post
61-
67+ """Test POST request with invalid host_id returns error."""
6268 # Mock validation to return False for invalid host ID
6369 mock_validate_host_id .return_value = False
6470
@@ -73,7 +79,7 @@ def test_on_post_invalid_host_id(self, mock_format_error, mock_validate_host_id)
7379 "host_id" : "invalid-host"
7480 }
7581
76- response = on_post (request , config = None , logger = mock_logger )
82+ response = main . on_post (request , _config = None , logger = mock_logger )
7783
7884 # Should return error response (default code is likely 400)
7985 self .assertEqual (response .errors , [{"code" : 400 , "message" : "Invalid host ID format" }])
@@ -93,8 +99,7 @@ def test_on_post_invalid_host_id(self, mock_format_error, mock_validate_host_id)
9399 @patch ("main.validate_host_id" )
94100 @patch ("main.format_error_response" )
95101 def test_on_post_missing_host_id (self , mock_format_error , mock_validate_host_id ):
96- from main import on_post
97-
102+ """Test POST request with missing host_id returns error."""
98103 # Mock validation to return False for None host ID
99104 mock_validate_host_id .return_value = False
100105
@@ -107,7 +112,7 @@ def test_on_post_missing_host_id(self, mock_format_error, mock_validate_host_id)
107112 request = Request ()
108113 request .body = {} # No host_id provided
109114
110- response = on_post (request , config = None , logger = mock_logger )
115+ response = main . on_post (request , _config = None , logger = mock_logger )
111116
112117 # Should return error response
113118 self .assertEqual (response .errors , [{"code" : 400 , "message" : "Invalid host ID format" }])
@@ -127,8 +132,7 @@ def test_on_post_missing_host_id(self, mock_format_error, mock_validate_host_id)
127132 @patch ("main.validate_host_id" )
128133 @patch ("main.format_error_response" )
129134 def test_on_post_empty_host_id (self , mock_format_error , mock_validate_host_id ):
130- from main import on_post
131-
135+ """Test POST request with empty host_id returns error."""
132136 # Mock validation to return False for empty string
133137 mock_validate_host_id .return_value = False
134138
@@ -143,7 +147,7 @@ def test_on_post_empty_host_id(self, mock_format_error, mock_validate_host_id):
143147 "host_id" : ""
144148 }
145149
146- response = on_post (request , config = None , logger = mock_logger )
150+ response = main . on_post (request , _config = None , logger = mock_logger )
147151
148152 # Should return error response
149153 self .assertEqual (response .errors , [{"code" : 400 , "message" : "Invalid host ID format" }])
@@ -162,9 +166,8 @@ def test_on_post_empty_host_id(self, mock_format_error, mock_validate_host_id):
162166
163167 @patch ("main.validate_host_id" )
164168 @patch ("main.format_error_response" )
165- def test_on_post_with_config (self , mock_format_error , mock_validate_host_id ):
166- from main import on_post
167-
169+ def test_on_post_with_config (self , _mock_format_error , mock_validate_host_id ):
170+ """Test POST request with config parameter."""
168171 # Mock validation to return True
169172 mock_validate_host_id .return_value = True
170173
@@ -179,7 +182,7 @@ def test_on_post_with_config(self, mock_format_error, mock_validate_host_id):
179182 "host_id" : "test-host-456"
180183 }
181184
182- response = on_post (request , config = config , logger = mock_logger )
185+ response = main . on_post (request , _config = config , logger = mock_logger )
183186
184187 self .assertEqual (response .code , 200 )
185188 self .assertEqual (response .body ["host" ], "test-host-456" )
0 commit comments