def test_translate_number_decimal_negative(self): """ Test metod `translate_number` on decimal positive number. """ number = Decimal("-3.14") encrypted = translate_number(self.numeric_encryption_key, number) assert_not_equal(number, encrypted) assert_equal(type(number), type(encrypted)) decrypted = translate_number(self.numeric_encryption_key, encrypted, encrypt=False) assert_equal(number, decrypted) assert_equal(type(number), type(decrypted))
def test_translate_number_whole_negative(self): """ Test metod `translate_number` on whole negative number. """ number = -42 encrypted = translate_number(self.numeric_encryption_key, number) assert_not_equal(number, encrypted) assert_equal(type(number), type(encrypted)) decrypted = translate_number(self.numeric_encryption_key, encrypted, encrypt=False) assert_equal(number, decrypted) assert_equal(type(number), type(decrypted))
def anonymize_json_value( self, value: Union[list, dict, bool, None, str, int, float], encryption_key: str, anonymize: bool = True ) -> Union[list, dict, bool, None, str, int, float]: if value is None: return None elif type(value) is str: return translate_text(encryption_key, value, anonymize, JSON_SAFE_CHARS) # type: ignore elif type(value) is int: return translate_number(encryption_key, value, anonymize) # type: ignore elif type(value) is float: # We cannot safely anonymize floats return value elif type(value) is dict: return { key: self.anonymize_json_value(item, encryption_key, anonymize) for key, item in value.items() } # type: ignore elif type(value) is list: return [ self.anonymize_json_value(item, encryption_key, anonymize) for item in value ] # type: ignore elif type(value) is bool and self.get_numeric_encryption_key( encryption_key) % 2 == 0: return not value return value
def test_translate_number_whole_positive(self): """ Test metod `translate_number` on whole positive number. """ number = 42 encrypted = translate_number(self.numeric_encryption_key, number) self.assertNotEqual(number, encrypted) self.assertEqual(type(number), type(encrypted)) decrypted = translate_number(self.numeric_encryption_key, encrypted, encrypt=False) self.assertEqual(number, decrypted) self.assertEqual(type(number), type(decrypted))
def get_decrypted_value(self, value, encryption_key: str): return translate_number(str( self.get_numeric_encryption_key(encryption_key)), value, encrypt=False)