def test_date_shift(): test_date = str(datetime.date(2006, 6, 15)) date_config = DateShiftConfig( secret= "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", lower_range_days=-10, upper_range_days=45, labels=["date"], ) date_config_tweak = DateShiftConfig( secret= "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", lower_range_days=-10, upper_range_days=45, labels=["date"], tweak=FieldRef("test_id"), ) date2_config = DateShiftConfig( secret= "1628AED2A6A809CBF7158F7F036D6F059D8D54FC6A942B7E15F4F3CEF4380AA4", lower_range_days=-10, upper_range_days=45, labels=["date"], ) date2_config_tweak = DateShiftConfig( secret= "1628AED2A6A809CBF7158F7F036D6F059D8D54FC6A942B7E15F4F3CEF4380AA4", lower_range_days=-10, upper_range_days=45, labels=["date"], ) xf = factory(date_config) encode = xf.transform_field("date", test_date, None) decode = xf.restore_field("date", encode["date"], None) xf = factory(date2_config) encode2 = xf.transform_field("date", test_date, None) decode2 = xf.restore_field("date", encode2["date"], None) xf = factory(date_config_tweak) xf.field_ref_dict["tweak"] = FieldRef("user_id", 10, 17) encode_t = xf.transform_field("date", test_date, None) decode_t = xf.restore_field("date", encode_t["date"], None) xf = factory(date2_config_tweak) xf.field_ref_dict["tweak"] = FieldRef("user_id", 10, 17) encode2_t = xf.transform_field("date", test_date, None) decode2_t = xf.restore_field("date", encode2_t["date"], None) assert decode_t["date"] == test_date assert decode2_t["date"] == test_date assert decode["date"] == test_date assert decode2["date"] == test_date assert encode_t["date"] != test_date assert encode2_t["date"] != test_date assert encode["date"] != test_date assert encode2["date"] != test_date assert encode_t != encode2_t and encode_t != encode and encode_t != encode2 assert encode2_t != encode and encode2_t != encode2 assert encode != encode2
def test_fpe_string(): mask_last_name = StringMask(mask_after=' ') mask_first_name = StringMask(mask_until=' ') fpe_string_config = FpeStringConfig(secret="2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", radix=62, mask=[mask_last_name]) xf = factory(fpe_string_config) record = xf.transform_field("person_name", "John Doe", None) assert record == {'person_name': 'John BDy'} record = xf._restore_field('person_name', record['person_name'], None) assert record == {'person_name': 'John Doe'} fpe_string_config = FpeStringConfig(secret="2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", radix=62, mask=[mask_first_name]) xf = factory(fpe_string_config) record = xf.transform_field("person_name", "John Doe", None) assert record == {'person_name': 'Uugx Doe'} record = xf._restore_field('person_name', record['person_name'], None) assert record == {'person_name': 'John Doe'} fpe_string_config = FpeStringConfig(secret="2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", radix=62, mask=[mask_first_name, mask_last_name]) xf = factory(fpe_string_config) record = xf.transform_field("person_name", "John Doe", None) assert record == {'person_name': 'Uugx BDy'} record = xf._restore_field('person_name', record['person_name'], None) assert record == {'person_name': 'John Doe'}
def test_type_mismatch(): bucket_list = [Bucket("a", "l", "a-l"), Bucket("m", "s", "m-s")] xf = factory( BucketConfig(buckets=bucket_list, labels=["person_name"], upper_outlier_label="t-z")) assert (None, 123) == xf.transform_entity("person_name", 123)
def test_fake_constant(): test_values_list = [ (["tr-TR"], "name", "John Doe", "Jane Doe", "Erensoy Karadeniz"), (["tr-TR"], "first_name", "John", "Frank", "Fatih"), (["tr-TR"], "email", "*****@*****.**", "*****@*****.**", "*****@*****.**"), (["en-US"], "email", "*****@*****.**", "*****@*****.**", "*****@*****.**"), (["en-US"], "first_name", "John", "Frank", "James"), (["en-US"], "last_name", "Johnson", "Smith", "Huang"), (["en-US"], "name", "John Doe", "Jane Doe", "Eduardo Walter"), (["es-MX"], "email", "*****@*****.**", "*****@*****.**", "*****@*****.**"), (["es-MX"], "first_name", "John", "Frank", "Catalina"), (["es-MX"], "last_name", "Johnson", "Smith", "Tovar"), (["es-MX"], "name", "John Doe", "Jane Doe", "Elvia Rosario Almonte Luna"), ] for test_values in test_values_list: fake_config = FakeConstantConfig(locales=test_values[0], seed=12345, fake_method=test_values[1]) xf = factory(fake_config) encode1 = xf.transform_field(test_values[1], test_values[2], None) encode2 = xf.transform_field(test_values[1], test_values[2], None) encode3 = xf.transform_field(test_values[1], test_values[3], None) assert encode1 == {test_values[1]: test_values[4]} assert encode2 == encode1 != encode3
def test_faker_locales(): locales = ["tr-TR", "es-MX", "en-US", "de-DE"] fake_config = FakeConstantConfig(locales=locales, seed=12345, fake_method="name") xf = factory(fake_config) name_set = set() while len(name_set) < len(locales): record = xf.transform_field("person_name", "John Doe", None) name_set.add(record["person_name"])
def test_redact_with_char(): mask_last_name = StringMask(mask_after=' ') mask_first_name = StringMask(mask_until=' ') redact_with_char_config = RedactWithCharConfig(labels=['ip_address'], mask=[mask_last_name]) xf = factory(redact_with_char_config) record = xf.transform_field("person_name", "John Doe", None) assert record == {'person_name': 'John XXX'} redact_with_char_config = RedactWithCharConfig(labels=['ip_address'], mask=[mask_first_name]) xf = factory(redact_with_char_config) record = xf.transform_field("person_name", "John Doe", None) assert record == {'person_name': 'XXXX Doe'} redact_with_char_config = RedactWithCharConfig( labels=['ip_address'], mask=[mask_first_name, mask_last_name]) xf = factory(redact_with_char_config) record = xf.transform_field("person_name", "John Doe", None) assert record == {'person_name': 'XXXX XXX'}
def test_string_bucket(): bucket_list = [Bucket("a", "l", "a-l"), Bucket("m", "s", "m-s")] xf = factory( BucketConfig(buckets=bucket_list, labels=["person_name"], upper_outlier_label="t-z")) _, check = xf.transform_entity("person_name", "myers") assert check == "m-s" _, check = xf.transform_entity("person_name", "ehrath") assert check == "a-l"
def test_faker_locales_seed_deterministc(): name_set = set() locales = ["tr-TR", "es-MX"] for i in range(10): fake_config = FakeConstantConfig(locales=locales, seed=12345, fake_method="name") xf = factory(fake_config) record = xf.transform_field("person_name", "John Doe", None) name_set.add(record["person_name"]) assert len(name_set) == 1
def __init__(self, *, input: str, xforms: Union[List[TransformerConfig], TransformerConfig] = None, output: str = None): self.input_field_matcher = re.compile(fnmatch.translate(input)) self.input_field = input self.output_field = output if xforms: transform_configs = list(_flatten(xforms)) self.transformations = [ factory(config) for config in transform_configs ]
def __init__(self, config: ConditionalConfig): super().__init__(config=config) self.conditional_value = config.conditional_value if isinstance(config.regex, Pattern): self.regex = config.regex else: self.regex = re.compile(config.regex) if config.true_xform is not None: self.true_xform = factory(config.true_xform) if isinstance(self.true_xform, RestoreTransformer): self._true_xform_restore_field = self.true_xform.restore_field self._true_xform_restore_entity = self.true_xform.restore_entity else: self._true_xform_restore_field = self.true_xform.transform_field self._true_xform_restore_entity = self.true_xform.transform_entity if config.false_xform is not None: self.false_xform = factory(config.false_xform) if isinstance(self.false_xform, RestoreTransformer): self._false_xform_restore_field = self.false_xform.restore_field self._false_xform_restore_entity = self.false_xform.restore_entity else: self._false_xform_restore_field = self.false_xform.transform_field self._false_xform_restore_entity = self.false_xform.transform_entity
def test_faker_no_method(): with pytest.raises(ValueError): fake_config = FakeConstantConfig(seed=12345) factory(fake_config)
def test_faker_valid_label(): with pytest.raises(ValueError): fake_config = FakeConstantConfig(seed=12345, labels=VALID_LABELS[0]) factory(fake_config)
def test_faker_invalid_label(): with pytest.raises(ValueError): fake_config = FakeConstantConfig(seed=12345, labels=["fail_me"]) factory(fake_config)