def test_given_anonymize_with_encrypt_then_text_returned_with_encrypted_content():
    unencrypted_text = "My name is "
    expected_encrypted_text = "Chloë"
    text = unencrypted_text + expected_encrypted_text
    start_index = 11
    end_index = 16
    key = "WmZq4t7w!z%C&F)J"
    analyzer_results = [RecognizerResult("PERSON", start_index, end_index, 0.8)]
    anonymizers_config = {"PERSON": OperatorConfig("encrypt", {"key": key})}

    actual_anonymize_result = (
        AnonymizerEngine().anonymize(text, analyzer_results, anonymizers_config)
    )

    assert len(actual_anonymize_result.items) == 1
    anonymized_entities = [
        AnonymizerResult.from_operator_result(actual_anonymize_result.items[0])
    ]
    engine = DeanonymizeEngine()
    decryption = engine.deanonymize(
        actual_anonymize_result.text, anonymized_entities,
        {"PERSON": OperatorConfig(Decrypt.NAME, {"key": key})}
    )
    assert decryption.text == "My name is Chloë"
    assert len(decryption.items) == 1
    assert decryption.items[0].text == "Chloë"
    assert decryption.items[0].end == 16
    assert decryption.items[0].start == 11
    assert decryption.items[0].entity_type == "PERSON"
Пример #2
0
def test_given_valid_json_then_we_parse_it_to_encrypt_result():
    encrypt_result = AnonymizerResult.from_json({
        "start": 0,
        "end": 10,
        "entity_type": "PERSON",
    })
    assert encrypt_result.end == 10
    assert encrypt_result.start == 0
    assert encrypt_result.entity_type == "PERSON"
def test_given_short_key_then_we_fail():
    text = "My name is S184CMt9Drj7QaKQ21JTrpYzghnboTF9pn/neN8JME0="
    encryption_results = [
        AnonymizerResult(
            start=11,
            end=55,
            entity_type="PERSON"
        ),
    ]
    engine = DeanonymizeEngine()
    expected_result = "Invalid input, key must be of length 128, 192 or 256 bits"
    with pytest.raises(InvalidParamException,
                       match=expected_result):
        engine.deanonymize(
            text, encryption_results,
            {"PERSON": OperatorConfig(Decrypt.NAME, {"key": "1234"})}
        )
def test_given_operator_decrypt_with_valid_params_then_decrypt_text_successfully():
    text = "My name is S184CMt9Drj7QaKQ21JTrpYzghnboTF9pn/neN8JME0="
    encryption_results = [
        AnonymizerResult(
            start=11,
            end=55,
            entity_type="PERSON"
        ),
    ]
    engine = DeanonymizeEngine()
    decryption = engine.deanonymize(
        text, encryption_results,
        {"DEFAULT": OperatorConfig(Decrypt.NAME, {"key": "WmZq4t7w!z%C&F)J"})}
    )
    assert decryption.text == "My name is Chloë"
    assert len(decryption.items) == 1
    assert decryption.items[0].text == "Chloë"
    assert decryption.items[0].end == 16
    assert decryption.items[0].start == 11
    assert decryption.items[0].entity_type == "PERSON"
Пример #5
0
    def deanonymize_entities_from_json(json: Dict) -> List['AnonymizerResult']:
        """
        Create DecryptEntity list.

        :param json:
        {
            "text": text,
            "encrypt_results": [{
                "start": 0,
                "end": 10,
                "key": "1111111111111111",
                "entity_type":"PHONE_NUMBER"
            }],
        }
        :return: List[AnonymizerResult]
        """
        items = []
        decrypt_entity = json.get("anonymizer_results")
        if decrypt_entity:
            for result in decrypt_entity:
                items.append(AnonymizerResult.from_json(result))
        return items
Пример #6
0
def test_given_anonymized_entity_then_we_parse_it_to_encrypt_result():
    anonyimzer_result = AnonymizerResult.from_operator_result(
        OperatorResult("bla", "replace", 9, 10, "PHONE_NUMBER"))
    assert anonyimzer_result.end == 10
    assert anonyimzer_result.start == 9
    assert anonyimzer_result.entity_type == "PHONE_NUMBER"
Пример #7
0
def test_given_invalid_json_then_we_fail_to_parse(json, expected_result):
    with pytest.raises(InvalidParamException, match=expected_result):
        AnonymizerResult.from_json(json)