Skip to content

Commit 940f536

Browse files
authored
Fix pylint errors in functions (#24)
1 parent 1c9f57e commit 940f536

File tree

15 files changed

+249
-163
lines changed

15 files changed

+249
-163
lines changed

.github/workflows/pylint.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ on:
44
push:
55
paths:
66
- '**.py'
7-
branches:
8-
- main
97
pull_request:
108
paths:
119
- '**.py'
12-
branches:
13-
- main
1410

1511
jobs:
1612
analyze:

functions/hello/main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
"""Main module for the hello function handler."""
2+
13
from crowdstrike.foundry.function import Function, Request, Response, APIError
24

3-
func = Function.instance()
5+
FUNC = Function.instance()
46

57

6-
# Handler hello
7-
@func.handler(method="POST", path="/hello")
8+
@FUNC.handler(method="POST", path="/hello")
89
def on_post(request: Request) -> Response:
10+
"""
11+
Handle POST requests to /hello endpoint.
12+
13+
Args:
14+
request: The incoming request object containing the request body.
15+
16+
Returns:
17+
Response: JSON response with greeting or error message.
18+
"""
919
#
1020
# Replace the following example code with your handler code
1121
#
@@ -29,4 +39,4 @@ def on_post(request: Request) -> Response:
2939

3040

3141
if __name__ == "__main__":
32-
func.run()
42+
FUNC.run()

functions/hello/test_main.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
1+
"""Test module for the hello function handler."""
2+
3+
import importlib
14
import unittest
25
from unittest.mock import patch
36

47
from 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

1421
class 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
def test_on_post_success(self):
25-
from main import on_post
33+
"""Test successful POST request with valid name in body."""
2634
request = Request()
2735
request.body = {
2836
"name": "Test User"
2937
}
3038

31-
response = on_post(request)
39+
response = main.on_post(request)
3240
self.assertEqual(response.code, 200)
3341
self.assertEqual(response.body["greeting"], "Hello Test User! It is nice to see you.")
3442

3543
def test_on_post_missing_name(self):
36-
from main import on_post
44+
"""Test POST request with missing name in body returns error."""
3745
request = Request()
3846

39-
response = on_post(request)
47+
response = main.on_post(request)
4048
self.assertEqual(response.code, 400)
4149
self.assertEqual(len(response.errors), 1)
4250
self.assertEqual(response.errors[0].message, "missing name from request body")

functions/host-details/main.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
"""Main module for the host-details function handler."""
2+
13
from crowdstrike.foundry.function import Function, Request, Response, APIError
24
# Import service collection you'd like to use
35
from falconpy import Hosts
46

5-
func = Function.instance()
7+
FUNC = Function.instance()
68

79

8-
@func.handler(method="POST", path="/host-details")
10+
@FUNC.handler(method="POST", path="/host-details")
911
def on_post(request: Request) -> Response:
12+
"""
13+
Handle POST requests to /host-details endpoint.
14+
15+
Args:
16+
request: The incoming request object containing the request body.
17+
18+
Returns:
19+
Response: JSON response with host details or error message.
20+
"""
1021
# Validate request
1122
if "host_id" not in request.body:
1223
return Response(
@@ -37,4 +48,4 @@ def on_post(request: Request) -> Response:
3748

3849

3950
if __name__ == "__main__":
40-
func.run()
51+
FUNC.run()

functions/host-details/test_main.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1+
"""Test module for the host-details function handler."""
2+
3+
import importlib
14
import unittest
25
from unittest.mock import patch, MagicMock
36

47
from 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

1421
class 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.Hosts")
2533
def test_on_post_success(self, mock_hosts_class):
26-
from main import on_post
27-
34+
"""Test successful POST request with valid host_id in body."""
2835
# Mock the Hosts instance and its response
2936
mock_hosts_instance = MagicMock()
3037
mock_hosts_class.return_value = mock_hosts_instance
@@ -44,27 +51,26 @@ def test_on_post_success(self, mock_hosts_class):
4451
"host_id": "test-host-123"
4552
}
4653

47-
response = on_post(request)
54+
response = main.on_post(request)
4855

4956
self.assertEqual(response.code, 200)
5057
self.assertIn("host_details", response.body)
5158
self.assertEqual(response.body["host_details"]["device_id"], "test-host-123")
5259
mock_hosts_instance.get_device_details.assert_called_once_with(ids="test-host-123")
5360

5461
def test_on_post_missing_host_id(self):
55-
from main import on_post
62+
"""Test POST request with missing host_id in body returns error."""
5663
request = Request()
5764

58-
response = on_post(request)
65+
response = main.on_post(request)
5966

6067
self.assertEqual(response.code, 400)
6168
self.assertEqual(len(response.errors), 1)
6269
self.assertEqual(response.errors[0].message, "missing host_id from request body")
6370

6471
@patch("main.Hosts")
6572
def test_on_post_api_error(self, mock_hosts_class):
66-
from main import on_post
67-
73+
"""Test POST request when API returns an error."""
6874
# Mock the Hosts instance to return an error
6975
mock_hosts_instance = MagicMock()
7076
mock_hosts_class.return_value = mock_hosts_instance
@@ -78,7 +84,7 @@ def test_on_post_api_error(self, mock_hosts_class):
7884
"host_id": "nonexistent-host"
7985
}
8086

81-
response = on_post(request)
87+
response = main.on_post(request)
8288

8389
self.assertEqual(response.code, 404)
8490
self.assertEqual(len(response.errors), 1)

functions/host-info/main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
"""Main module for the host-info function handler."""
2+
13
from logging import Logger
2-
from typing import Dict
4+
from typing import Dict, Optional
35

46
from crowdstrike.foundry.function import Function, Request, Response
57

68
from utils import validate_host_id, format_error_response
79

8-
func = Function.instance()
10+
FUNC = Function.instance()
11+
12+
13+
@FUNC.handler(method="POST", path="/host-info")
14+
def on_post(request: Request, _config: Optional[Dict[str, object]], logger: Logger) -> Response:
15+
"""
16+
Handle POST requests to /host-info endpoint.
917
18+
Args:
19+
request: The incoming request object containing the request body.
20+
_config: Configuration dictionary (unused).
21+
logger: Logger instance for logging.
1022
11-
@func.handler(method="POST", path="/host-info")
12-
def on_post(request: Request, config: Dict[str, object] | None, logger: Logger) -> Response:
23+
Returns:
24+
Response: JSON response with host info or error message.
25+
"""
1326
host_id = request.body.get("host_id")
1427

1528
logger.info(f"Host ID: {host_id}")
@@ -27,4 +40,4 @@ def on_post(request: Request, config: Dict[str, object] | None, logger: Logger)
2740

2841

2942
if __name__ == "__main__":
30-
func.run()
43+
FUNC.run()

functions/host-info/test_main.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1+
"""Test module for the host-info function handler."""
2+
3+
import importlib
14
import unittest
25
from unittest.mock import patch, MagicMock
36

47
from 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

1421
class 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")

functions/host-info/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Utility functions for host-info function handler."""
2+
13
import json
24
import re
35
from typing import Dict, Any, Optional

0 commit comments

Comments
 (0)