Skip to content

Commit b33c98a

Browse files
committed
feat: add parameter validations to decorator. add a unit test
1 parent 47b0721 commit b33c98a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

secure_logger/decorators.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# our stuff
88
from secure_logger.conf import settings
9+
from secure_logger.exceptions import SecureLoggerConfigurationError
910
from secure_logger.masked_dict import masked_dict2str
1011

1112

@@ -17,6 +18,14 @@ def secure_logger(
1718
):
1819
"""Top level decorator, for defining input parameters."""
1920

21+
logging_levels = list(settings.logging_levels)
22+
if log_level not in logging_levels:
23+
raise SecureLoggerConfigurationError(f"Invalid logging level: {log_level}. Valid values are: {logging_levels}")
24+
if indent < 0:
25+
raise SecureLoggerConfigurationError(f"Invalid indentation value: {indent}. Valid values are: 0 or greater.")
26+
if message == "":
27+
raise SecureLoggerConfigurationError(f"Invalid redaction message: {message}. Must be a non-empty string.")
28+
2029
def decorate(func):
2130
"""
2231
Decorate a Python a class, a class method, or a function.

secure_logger/tests/tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from secure_logger.conf import settings
1212
from secure_logger.decorators import secure_logger
13+
from secure_logger.exceptions import SecureLoggerConfigurationError
1314
from secure_logger.masked_dict import masked_dict, masked_dict2str
1415

1516

@@ -166,6 +167,26 @@ def test_class_method_with_custom_params(self):
166167

167168
self.assertEqual(cm.output[0][0:100], expected_output[0:100])
168169

170+
def test_method_with_illegal_decorator_input(self):
171+
"""Test class method with illegal decorator input."""
172+
173+
def create_mock_class():
174+
"""Create a mock class with illegal decorator input."""
175+
176+
class MockClass:
177+
"""Mock class with illegal decorator input."""
178+
179+
@secure_logger(indent=-1)
180+
def decorator_with_invalid_params(self):
181+
"""Test class input parameter as objects."""
182+
183+
return MockClass()
184+
185+
self.assertRaises(
186+
SecureLoggerConfigurationError,
187+
create_mock_class,
188+
)
189+
169190

170191
class TestClassDecorator(unittest.TestCase):
171192
"""Test class logging."""

0 commit comments

Comments
 (0)