Skip to content

Commit 9c904f0

Browse files
committed
test: Data Classes str()
Add tests for DictWrapper str() functionality, including Data Classes with: - no properties - single property - sensitive property - recursive properties (Data Classes inside Data Classes) - exceptions reading properties - exceptions when transforming a list with DictWrapper subclasses inside
1 parent d9d1bc7 commit 9c904f0

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

tests/functional/test_data_classes.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,174 @@ class DataClassSample(DictWrapper):
126126
assert event_source.items() == data.items()
127127

128128

129+
def test_dict_wrapper_str_no_property():
130+
"""
131+
Checks that the _properties function returns
132+
only the "raw_event", and the resulting string
133+
notes it as sensitive.
134+
"""
135+
136+
class DataClassSample(DictWrapper):
137+
attribute = None
138+
139+
def function(self):
140+
pass
141+
142+
event_source = DataClassSample({})
143+
assert event_source._properties() == ["raw_event"]
144+
assert str(event_source) == "{'raw_event': '[SENSITIVE]'}"
145+
146+
147+
def test_dict_wrapper_str_single_property():
148+
"""
149+
Checks that the _properties function returns
150+
the defined property "data_property", and
151+
resulting string includes the property value.
152+
"""
153+
154+
class DataClassSample(DictWrapper):
155+
attribute = None
156+
157+
def function(self):
158+
pass
159+
160+
@property
161+
def data_property(self):
162+
return "value"
163+
164+
event_source = DataClassSample({})
165+
assert event_source._properties() == ["data_property", "raw_event"]
166+
assert event_source._str_helper() == {"data_property": "value", "raw_event": "[SENSITIVE]"}
167+
assert str(event_source) == "{'data_property': 'value', 'raw_event': '[SENSITIVE]'}"
168+
169+
170+
def test_dict_wrapper_str_property_exception():
171+
"""
172+
Check the recursive _str_helper function handles
173+
exceptions that may occur when accessing properties
174+
"""
175+
176+
class DataClassSample(DictWrapper):
177+
attribute = None
178+
179+
def function(self):
180+
pass
181+
182+
@property
183+
def data_property(self):
184+
raise Exception()
185+
186+
event_source = DataClassSample({})
187+
assert event_source._str_helper() == {
188+
"data_property": "[EXCEPTION <class 'Exception'>]",
189+
"raw_event": "[SENSITIVE]",
190+
}
191+
assert str(event_source) == "{'data_property': \"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}"
192+
193+
194+
def test_dict_wrapper_str_property_list_exception():
195+
"""
196+
Check that _str_helper properly handles exceptions
197+
that occur when recursively working through items
198+
in a list property.
199+
"""
200+
201+
class BrokenDataClass(DictWrapper):
202+
@property
203+
def broken_data_property(self):
204+
raise Exception()
205+
206+
class DataClassSample(DictWrapper):
207+
attribute = None
208+
209+
def function(self):
210+
pass
211+
212+
@property
213+
def data_property(self):
214+
return ["string", 0, 0.0, BrokenDataClass({})]
215+
216+
event_source = DataClassSample({})
217+
assert event_source._properties() == ["data_property", "raw_event"]
218+
assert event_source._str_helper() == {
219+
"data_property": [
220+
"string",
221+
0,
222+
0.0,
223+
{"broken_data_property": "[EXCEPTION <class 'Exception'>]", "raw_event": "[SENSITIVE]"},
224+
],
225+
"raw_event": "[SENSITIVE]",
226+
}
227+
event_str = (
228+
"{'data_property': ['string', 0, 0.0, {'broken_data_property': "
229+
+ "\"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}], 'raw_event': '[SENSITIVE]'}"
230+
)
231+
assert str(event_source) == event_str
232+
233+
234+
def test_dict_wrapper_str_recursive_property():
235+
"""
236+
Check that the _str_helper function recursively
237+
handles Data Classes within Data Classes
238+
"""
239+
240+
class DataClassTerminal(DictWrapper):
241+
attribute = None
242+
243+
def function(self):
244+
pass
245+
246+
@property
247+
def terminal_property(self):
248+
return "end-recursion"
249+
250+
class DataClassRecursive(DictWrapper):
251+
attribute = None
252+
253+
def function(self):
254+
pass
255+
256+
@property
257+
def data_property(self):
258+
return DataClassTerminal({})
259+
260+
event_source = DataClassRecursive({})
261+
assert event_source._properties() == ["data_property", "raw_event"]
262+
assert event_source._str_helper() == {
263+
"data_property": {"raw_event": "[SENSITIVE]", "terminal_property": "end-recursion"},
264+
"raw_event": "[SENSITIVE]",
265+
}
266+
assert (
267+
str(event_source)
268+
== "{'data_property': {'raw_event': '[SENSITIVE]', 'terminal_property': 'end-recursion'},"
269+
+ " 'raw_event': '[SENSITIVE]'}"
270+
)
271+
272+
273+
def test_dict_wrapper_sensitive_properties_property():
274+
"""
275+
Checks that the _str_helper function correctly
276+
handles _sensitive_properties
277+
"""
278+
279+
class DataClassSample(DictWrapper):
280+
attribute = None
281+
282+
def function(self):
283+
pass
284+
285+
_sensitive_properties = ["data_property"]
286+
287+
@property
288+
def data_property(self):
289+
return "value"
290+
291+
event_source = DataClassSample({})
292+
assert event_source._properties() == ["data_property", "raw_event"]
293+
assert event_source._str_helper() == {"data_property": "[SENSITIVE]", "raw_event": "[SENSITIVE]"}
294+
assert str(event_source) == "{'data_property': '[SENSITIVE]', 'raw_event': '[SENSITIVE]'}"
295+
296+
129297
def test_cloud_watch_dashboard_event():
130298
event = CloudWatchDashboardCustomWidgetEvent(load_event("cloudWatchDashboardEvent.json"))
131299
assert event.describe is False

0 commit comments

Comments
 (0)