def test_encode_NaN(self): start = float('nan') serializable = SerializableTransform() result = transforms.transform(start, [serializable]) self.assertTrue(math.isnan(result))
def _assertScrubbed(self, params_to_scrub, start, expected, scrub_username=False, scrub_password=True, redact_char='-', skip_id_check=False): scrubber = ScrubUrlTransform(suffixes=[], params_to_scrub=params_to_scrub, scrub_username=scrub_username, scrub_password=scrub_password, redact_char=redact_char, randomize_len=False) result = transforms.transform(start, [scrubber]) """ print(start) print(result) print(expected) """ if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(expected), type(result)) self.assertIsInstance(result, string_types) self._compare_urls(expected, result)
def _assertSerialized(self, start, expected, safe_repr=True, safelist=None, skip_id_check=False): serializable = SerializableTransform(safe_repr=safe_repr, safelist_types=safelist) result = transforms.transform(start, serializable) """ #print start print result print expected """ if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(expected), type(result)) if isinstance(result, Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple): self.assertTupleEqual(result, expected) elif isinstance(result, (list, set)): self.assertListEqual(result, expected) else: self.assertEqual(result, expected)
def test_encode_Infinity(self): start = float('inf') serializable = SerializableTransform() result = transforms.transform(start, serializable) self.assertTrue(math.isinf(result))
def _assertScrubbed(self, suffixes, start, expected, redact_char='*', skip_id_check=False): scrubber = ScrubTransform(suffixes=suffixes, redact_char=redact_char, randomize_len=False) result = transforms.transform(start, [scrubber]) """ print start print result print expected """ if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(result), type(expected)) if isinstance(result, collections.Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple): self.assertTupleEqual(result, expected) elif isinstance(result, (list, set)): self.assertListEqual(result, expected) else: self.assertEqual(result, expected)
def test_encode_NaN(self): start = float('nan') serializable = SerializableTransform() result = transforms.transform(start, serializable) self.assertTrue(math.isnan(result))
def test_encode_Infinity(self): start = float('inf') serializable = SerializableTransform() result = transforms.transform(start, [serializable]) self.assertTrue(math.isinf(result))
def test_encode_with_bad_repr_doesnt_die(self): class CustomRepr(object): def __repr__(self): assert False start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<AssertionError.*CustomRepr.*>")
def test_encode_with_custom_repr_returns_object(self): class CustomRepr(object): def __repr__(self): return {'hi': 'there'} start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<class '.*CustomRepr'>")
def test_circular(self): ref = {'scrub': 'me', 'some': 'times'} obj = {'hello': 'world', 'password': ref} ref['circular'] = obj scrubber = ScrubTransform([]) result = transforms.transform(obj, scrubber) self.assertIsNot(result, obj) self.assertIsNot(result['password'], ref) self.assertIsNot(result['password']['circular'], obj) self.assertIs(result['password']['circular']['password'], result['password']['circular']['password']['circular']['password'])
def test_scrub_dict_val_isnt_string(self): # This link will *not* be scrubbed because the value isn't a string or bytes obj = { 'url': ['cory:[email protected]/asdf?password=secret&clear=text'] } scrubber = ScrubUrlTransform(suffixes=[('url',)], params_to_scrub=['password'], randomize_len=False) result = transforms.transform(obj, [scrubber]) expected = copy.deepcopy(obj) self.assertDictEqual(expected, result)
def test_scrub_dict_val_isnt_string(self): # This link will *not* be scrubbed because the value isn't a string or bytes obj = {'url': ['cory:[email protected]/asdf?password=secret&clear=text']} scrubber = ScrubUrlTransform(suffixes=[('url', )], params_to_scrub=['password'], randomize_len=False) result = transforms.transform(obj, [scrubber]) expected = copy.deepcopy(obj) self.assertDictEqual(expected, result)
def test_shorten_object(self): data = {'request': {'POST': {i: i for i in range(12)}}} keys = [ ('request', 'POST'), ('request', 'json'), ('body', 'request', 'POST'), ('body', 'request', 'json'), ] self.assertEqual(len(data['request']['POST']), 12) shortener = ShortenerTransform(keys=keys, **DEFAULT_LOCALS_SIZES) result = transforms.transform(data, shortener) self.assertEqual(type(result), dict) self.assertEqual(len(result['request']['POST']), 10)
def test_circular(self): ref = {'scrub': 'me', 'some': 'times'} obj = {'hello': 'world', 'password': ref} ref['circular'] = obj scrubber = ScrubTransform([]) result = transforms.transform(obj, [scrubber]) self.assertIsNot(result, obj) self.assertIsNot(result['password'], ref) self.assertIsNot(result['password']['circular'], obj) self.assertIs( result['password']['circular']['password'], result['password']['circular']['password']['circular']['password'])
def test_scrub_dict_val_isnt_string(self): url = 'cory:[email protected]/asdf?password=secret&clear=text' # Every string which is a URL should be scrubbed obj = {'url': [url]} scrubber = ScrubUrlTransform(suffixes=[('url', )], params_to_scrub=['password'], randomize_len=False) result = transforms.transform(obj, scrubber) expected = url.replace('secr3t', '------').replace('secret', '------') self._assertScrubbed(['password'], result['url'][0], expected)
def test_scrub_dict_val_isnt_string(self): url = 'cory:[email protected]/asdf?password=secret&clear=text' # Every string which is a URL should be scrubbed obj = { 'url': [url] } scrubber = ScrubUrlTransform(suffixes=[('url',)], params_to_scrub=['password'], randomize_len=False) result = transforms.transform(obj, scrubber) expected = url.replace('secr3t', '------').replace('secret', '------') self._assertScrubbed(['password'], result['url'][0], expected)
def test_encode_with_bad_str_doesnt_die(self): class UnStringableException(Exception): def __str__(self): raise Exception('asdf') class CustomRepr(object): def __repr__(self): raise UnStringableException() start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(safelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<UnStringableException.*Exception.*str.*>")
def test_encode_with_custom_repr_returns_bytes(self): class CustomRepr(object): def __repr__(self): return b'hello' start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) if python_major_version() < 3: self.assertEqual(result['custom'], b'hello') else: self.assertRegex(result['custom'], "<class '.*CustomRepr'>")
def test_encode_with_bad_str_doesnt_die(self): class UnStringableException(Exception): def __str__(self): raise Exception('asdf') class CustomRepr(object): def __repr__(self): raise UnStringableException() start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<UnStringableException.*Exception.*str.*>")
def test_scrub_dict_nested_key_match_with_circular_ref(self): # If a URL is a circular reference then let's make sure to # show the scrubbed, original URL url = 'cory:[email protected]/asdf?password=secret&clear=text' obj = {'url': [{'link': url}], 'link': [{'url': url}]} scrubber = ScrubUrlTransform(suffixes=[('url', ), ('link', )], params_to_scrub=['password'], randomize_len=False) result = transforms.transform(obj, scrubber) self.assertNotIn('secr3t', result['url'][0]['link']) self.assertNotIn('secret', result['url'][0]['link']) self.assertNotIn('secr3t', result['link'][0]['url']) self.assertNotIn('secret', result['link'][0]['url']) self.assertNotRegex(result['url'][0]['link'], r'^-+$') self.assertNotRegex(result['link'][0]['url'], r'^-+$')
def _assert_shortened(self, key, expected): shortener = ShortenerTransform(keys=[(key,)], **DEFAULT_LOCALS_SIZES) result = transforms.transform(self.data, shortener) # the repr output can vary between Python versions stripped_result_key = result[key].strip("'\"u") if key == 'dict': self.assertEqual(expected, stripped_result_key.count(':')) elif key == 'other': self.assertIn(expected, stripped_result_key) else: self.assertEqual(expected, stripped_result_key) # make sure nothing else was shortened result.pop(key) self.assertNotIn('...', str(result)) self.assertNotIn('...', str(self.data))
def test_scrub_dict_nested_key_match_with_circular_ref(self): # If a URL is a circular reference then let's make sure to # show the scrubbed, original URL url = 'cory:[email protected]/asdf?password=secret&clear=text' obj = { 'url': [{'link': url}], 'link': [{'url': url}] } scrubber = ScrubUrlTransform(suffixes=[('url',), ('link',)], params_to_scrub=['password'], randomize_len=False) result = transforms.transform(obj, [scrubber]) self.assertNotIn('secr3t', result['url'][0]['link']) self.assertNotIn('secret', result['url'][0]['link']) self.assertNotIn('secr3t', result['link'][0]['url']) self.assertNotIn('secret', result['link'][0]['url']) self.assertNotRegex(result['url'][0]['link'], r'^-+$') self.assertNotRegex(result['link'][0]['url'], r'^-+$')
def _assertScrubbed(self, start, expected, redact_char="*", skip_id_check=False): scrubber = ScrubRedactTransform(redact_char=redact_char, randomize_len=False) result = transforms.transform(start, scrubber) if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(result), type(expected)) if isinstance(result, collections.Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple): self.assertTupleEqual(result, expected) elif isinstance(result, list): self.assertListEqual(result, expected) elif isinstance(result, set): self.assertSetEqual(result, expected) else: self.assertEqual(result, expected)
def _assertScrubbed(self, suffixes, start, expected, redact_char='*', skip_id_check=False): scrubber = ScrubTransform(suffixes=suffixes, redact_char=redact_char, randomize_len=False) result = transforms.transform(start, scrubber) """ print start print result print expected """ if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(result), type(expected)) if isinstance(result, Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple): self.assertTupleEqual(result, expected) elif isinstance(result, (list, set)): self.assertListEqual(result, expected) else: self.assertEqual(result, expected)
def _assertSerialized(self, start, expected, safe_repr=True, whitelist=None, skip_id_check=False): serializable = SerializableTransform(safe_repr=safe_repr, whitelist_types=whitelist) result = transforms.transform(start, serializable) """ #print start print result print expected """ if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(expected), type(result)) if isinstance(result, Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple): self.assertTupleEqual(result, expected) elif isinstance(result, (list, set)): self.assertListEqual(result, expected) else: self.assertEqual(result, expected)
def _transform(self, obj, key=None): return transforms.transform(obj, self._transforms, key=key)
def _serialize_frame_data(self, data): return transforms.transform(data, (self._serialize_transform,))
def test_no_shorten(self): shortener = ShortenerTransform(**DEFAULT_LOCALS_SIZES) result = transforms.transform(self.data, shortener) self.assertEqual(self.data, result)
def _transform(obj, key=None): for transform in _transforms: obj = transforms.transform(obj, transform, key=key) return obj
def _serialize_frame_data(data): return transforms.transform(data, (_serialize_transform, ))
def _serialize_frame_data(data): for transform in (ScrubRedactTransform(), _serialize_transform): data = transforms.transform(data, transform) return data
def _transform(obj, key=None): return transforms.transform(obj, _transforms, key=key)
def _serialize_frame_data(data): return transforms.transform(data, (_serialize_transform,))