Esempio n. 1
0
def test_given_encrypt_called_then_decrypt_returns_the_original_encrypted_text(
):
    text_for_encryption = "Lorem Ipsum is a Software Engineer"
    key = "1111111111111111"
    anonymize_request = {
        "text":
        text_for_encryption,
        "anonymizers": {
            "DEFAULT": {
                "type": "encrypt",
                "key": key
            },
            "TITLE": {
                "type": "encrypt",
                "key": "2222222222222222"
            }
        },
        "analyzer_results": [{
            "start": 0,
            "end": 12,
            "score": 0.8,
            "entity_type": "NAME",
        }, {
            "start": 18,
            "end": len(text_for_encryption),
            "score": 0.8,
            "entity_type": "TITLE",
        }],
    }
    _, anonymize_response_content = anonymize(json.dumps(anonymize_request))
    encrypted_text = json.loads(anonymize_response_content)["text"]

    decrypt_request = {
        "text":
        encrypted_text,
        "deanonymizers": {
            "DEFAULT": {
                "type": "decrypt",
                "key": "1111111111111111"
            },
            "TITLE": {
                "type": "decrypt",
                "key": "2222222222222222"
            }
        },
        "anonymizer_results": [{
            "start": 0,
            "end": 44,
            "entity_type": "NAME",
        }, {
            "start": 50,
            "end": 114,
            "entity_type": "TITLE",
        }],
    }

    _, decrypted_text_response = deanonymize(json.dumps(decrypt_request))

    decrypted_text = json.loads(decrypted_text_response)["text"]
    assert decrypted_text == text_for_encryption
Esempio n. 2
0
def test_given_decrypt_called_with_invalid_key_then_invalid_input_response_returned(
):
    text = "e6HnOMnIxbd4a8Qea44LshQDnjvxwzBIaAz + YqHNnMW2mC5r3AWoay8Spsoajyyy"
    request_body = {
        "text":
        text,
        "deanonymizers": {
            "NUMBER": {
                "type": "decrypt",
                "key": "invalidkey"
            }
        },
        "anonymizer_results": [{
            "start": 0,
            "end": len(text),
            "entity_type": "NUMBER"
        }],
    }

    response_status, response_content = deanonymize(json.dumps(request_body))

    expected_response = """
    {
        "error": "Invalid input, key must be of length 128, 192 or 256 bits"
    }
    """

    assert response_status == 422
    assert equal_json_strings(expected_response, response_content)
Esempio n. 3
0
def test_given_decrypt_called_with_encrypted_text_then_decrypted_text_returned(
):
    text = "e6HnOMnIxbd4a8Qea44LshQDnjvxwzBIaAz+YqHNnMW2mC5r3AWoay8Spsoajyyy"
    request_body = {
        "text":
        text,
        "deanonymizers": {
            "NUMBER": {
                "type": "decrypt",
                "key": "1111111111111111"
            }
        },
        "anonymizer_results": [{
            "start": 0,
            "end": len(text),
            "entity_type": "NUMBER"
        }],
    }

    response_status, response_content = deanonymize(json.dumps(request_body))

    expected_response = """{"text": "text_for_encryption", "items": [{"start": 0, "end": 19, "operator":"decrypt", "text": "text_for_encryption","entity_type":"NUMBER"}]}"""

    assert response_status == 200
    assert equal_json_strings(expected_response, response_content)
Esempio n. 4
0
def test_given_decrypt_called_with_missing_key_then_invalid_input_response_returned(
):
    request_body = """
    {
        "text": "e6HnOMnIxbd4a8Qea44LshQDnjvxwzBIaAz+YqHNnMW2mC5r3AWoay8Spsoajyyy"
    }
    """

    response_status, response_content = deanonymize(request_body)

    expected_response = """{"text": "e6HnOMnIxbd4a8Qea44LshQDnjvxwzBIaAz+YqHNnMW2mC5r3AWoay8Spsoajyyy", "items": []}"""
    assert response_status == 200
    assert equal_json_strings(expected_response, response_content)
Esempio n. 5
0
def test_given_decrypt_called_with_missing_payload_then_bad_request_response_returned(
):
    request_body = """
    { }
    """

    response_status, response_content = deanonymize(request_body)

    expected_response = """
    {
        "error": "Invalid request json"
    }
    """

    assert response_status == 400
    assert equal_json_strings(expected_response, response_content)
Esempio n. 6
0
def test_given_decrypt_called_with_missing_text_then_invalid_input_response_returned(
):
    request_body = """
    {
        "key": "1111111111111111"
    }
    """

    response_status, response_content = deanonymize(request_body)

    expected_response = """
    {
        "error": "Invalid input, text can not be empty"
    }
    """

    assert response_status == 422
    assert equal_json_strings(expected_response, response_content)