Skip to content

Commit 04aa665

Browse files
committed
test: Data Classes str()
Add tests for DictWrapper str() functionality, including Data Classes with: - no properties - single property - blacklisted property - recursive properties (Data Classes inside Data Classes) - exceptions reading properties - exceptions when transforming a list with DictWrapper subclasses inside
1 parent 5dc4fb8 commit 04aa665

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

tests/functional/test_data_classes.py

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

128128

129+
def test_dict_wrapper_str_no_property():
130+
class DataClassSample(DictWrapper):
131+
attribute = None
132+
133+
def function(self):
134+
pass
135+
136+
event_source = DataClassSample({})
137+
assert event_source._properties() == ["raw_event"]
138+
assert event_source._str_helper() == {"raw_event": "[SENSITIVE]"}
139+
assert event_source.__str__() == "{'raw_event': '[SENSITIVE]'}"
140+
assert str(event_source) == "{'raw_event': '[SENSITIVE]'}"
141+
142+
143+
def test_dict_wrapper_str_single_property():
144+
class DataClassSample(DictWrapper):
145+
attribute = None
146+
147+
def function(self):
148+
pass
149+
150+
@property
151+
def data_property(self):
152+
return "value"
153+
154+
event_source = DataClassSample({})
155+
assert event_source._properties() == ["data_property", "raw_event"]
156+
assert event_source._str_helper() == {"data_property": "value", "raw_event": "[SENSITIVE]"}
157+
assert event_source.__str__() == "{'data_property': 'value', 'raw_event': '[SENSITIVE]'}"
158+
assert str(event_source) == "{'data_property': 'value', 'raw_event': '[SENSITIVE]'}"
159+
160+
161+
def test_dict_wrapper_str_property_exception():
162+
class DataClassSample(DictWrapper):
163+
attribute = None
164+
165+
def function(self):
166+
pass
167+
168+
@property
169+
def data_property(self):
170+
raise Exception()
171+
172+
event_source = DataClassSample({})
173+
assert event_source._properties() == ["data_property", "raw_event"]
174+
assert event_source._str_helper() == {
175+
"data_property": "[EXCEPTION <class 'Exception'>]",
176+
"raw_event": "[SENSITIVE]",
177+
}
178+
assert (
179+
event_source.__str__() == "{'data_property': \"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}"
180+
)
181+
assert str(event_source) == "{'data_property': \"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}"
182+
183+
184+
def test_dict_wrapper_str_property_list_exception():
185+
class BrokenDataClass(DictWrapper):
186+
@property
187+
def broken_data_property(self):
188+
raise Exception()
189+
190+
class DataClassSample(DictWrapper):
191+
attribute = None
192+
193+
def function(self):
194+
pass
195+
196+
@property
197+
def data_property(self):
198+
return ["string", 0, 0.0, BrokenDataClass({})]
199+
200+
event_source = DataClassSample({})
201+
assert event_source._properties() == ["data_property", "raw_event"]
202+
assert event_source._str_helper() == {
203+
"data_property": [
204+
"string",
205+
0,
206+
0.0,
207+
{"broken_data_property": "[EXCEPTION <class 'Exception'>]", "raw_event": "[SENSITIVE]"},
208+
],
209+
"raw_event": "[SENSITIVE]",
210+
}
211+
event_str = (
212+
"{'data_property': ['string', 0, 0.0, {'broken_data_property': "
213+
+ "\"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}], 'raw_event': '[SENSITIVE]'}"
214+
)
215+
assert event_source.__str__() == event_str
216+
assert str(event_source) == event_str
217+
218+
219+
def test_dict_wrapper_str_recursive_property():
220+
class DataClassTerminal(DictWrapper):
221+
attribute = None
222+
223+
def function(self):
224+
pass
225+
226+
@property
227+
def terminal_property(self):
228+
return "end-recursion"
229+
230+
class DataClassRecursive(DictWrapper):
231+
attribute = None
232+
233+
def function(self):
234+
pass
235+
236+
@property
237+
def data_property(self):
238+
return DataClassTerminal({})
239+
240+
event_source = DataClassRecursive({})
241+
assert event_source._properties() == ["data_property", "raw_event"]
242+
assert event_source._str_helper() == {
243+
"data_property": {"raw_event": "[SENSITIVE]", "terminal_property": "end-recursion"},
244+
"raw_event": "[SENSITIVE]",
245+
}
246+
assert (
247+
event_source.__str__()
248+
== "{'data_property': {'raw_event': '[SENSITIVE]', 'terminal_property': 'end-recursion'},"
249+
+ " 'raw_event': '[SENSITIVE]'}"
250+
)
251+
assert (
252+
str(event_source)
253+
== "{'data_property': {'raw_event': '[SENSITIVE]', 'terminal_property': 'end-recursion'},"
254+
+ " 'raw_event': '[SENSITIVE]'}"
255+
)
256+
257+
258+
def test_dict_wrapper_str_sensitive_properties_property():
259+
class DataClassSample(DictWrapper):
260+
attribute = None
261+
262+
def function(self):
263+
pass
264+
265+
_str_sensitive_properties = ["data_property"]
266+
267+
@property
268+
def data_property(self):
269+
return "value"
270+
271+
event_source = DataClassSample({})
272+
assert event_source._properties() == ["data_property", "raw_event"]
273+
assert event_source._str_helper() == {"data_property": "[SENSITIVE]", "raw_event": "[SENSITIVE]"}
274+
assert event_source.__str__() == "{'data_property': '[SENSITIVE]', 'raw_event': '[SENSITIVE]'}"
275+
assert str(event_source) == "{'data_property': '[SENSITIVE]', 'raw_event': '[SENSITIVE]'}"
276+
277+
129278
def test_cloud_watch_dashboard_event():
130279
event = CloudWatchDashboardCustomWidgetEvent(load_event("cloudWatchDashboardEvent.json"))
131280
assert event.describe is False

0 commit comments

Comments
 (0)