def test_given_no_custom_operator_then_expected_result_returned(): result = False content = get_content() anonymizers_config = AppEntitiesConvertor.operators_config_from_json( content.get("anonymizers")) assert AppEntitiesConvertor.check_custom_operator( anonymizers_config) == result
def test_given_invalid_json_then_we_fail_to_convert(): data = { "text": "THIS IS MY TEXT", "anonymizer_results": [{ "start": 0, "end": 5, "key": "1111111111111111", }], } with pytest.raises(InvalidParamException, match="Invalid input, result must contain entity_type"): AppEntitiesConvertor.deanonymize_entities_from_json(data)
def deanonymize() -> Response: content = request.get_json() if not content: raise BadRequest("Invalid request json") text = content.get("text") deanonymize_entities = AppEntitiesConvertor.deanonymize_entities_from_json( content) deanonymize_config = AppEntitiesConvertor.operators_config_from_json( content.get("deanonymizers")) deanonymized_response = self.deanonymize.deanonymize( text=text, entities=deanonymize_entities, operators=deanonymize_config) return Response(deanonymized_response.to_json(), mimetype="application/json")
def test_given_custom_operator_then_expected_result_returned(): result = True anonymizers = { "DEFAULT": { "type": "replace", "new_value": "ANONYMIZED" }, "PHONE_NUMBER": { "type": "custom", "lambda": "lambda x: x[::-1]" } } anonymizers_config = AppEntitiesConvertor.operators_config_from_json( anonymizers) assert AppEntitiesConvertor.check_custom_operator( anonymizers_config) == result
def test_given_anonymizers_json_then_we_create_properties_properly( anonymizer_json, result ): anonymizers_config = AppEntitiesConvertor.operators_config_from_json( anonymizer_json.get("anonymizers") ) assert anonymizers_config == result
def anonymize() -> Response: content = request.get_json() if not content: raise BadRequest("Invalid request json") anonymizers_config = AppEntitiesConvertor.operators_config_from_json( content.get("anonymizers")) analyzer_results = AppEntitiesConvertor.analyzer_results_from_json( content.get("analyzer_results")) anoymizer_result = self.anonymizer.anonymize( text=content.get("text"), analyzer_results=analyzer_results, operators=anonymizers_config, ) return Response(anoymizer_result.to_json(), mimetype="application/json")
def test_given_empty_analyzer_results_then_list_created_successfully(): analyzer_results = AppEntitiesConvertor.analyzer_results_from_json([]) assert len(analyzer_results) == len([]) for result_a in analyzer_results: same_result_in_content = __find_element([], result_a.entity_type) assert same_result_in_content assert result_a.score == same_result_in_content.get("score") assert result_a.start == same_result_in_content.get("start") assert result_a.end == same_result_in_content.get("end")
def test_given_valid_json_then_we_convert_it_to_decrypt_entities_list(): data = { "text": "THIS IS MY TEXT", "anonymizer_results": [{ "start": 0, "end": 5, "entity_type": "PHONE" }], } decrypted_entities = AppEntitiesConvertor.deanonymize_entities_from_json(data) assert len(decrypted_entities) == 1 assert decrypted_entities[0].start == 0 assert decrypted_entities[0].end == 5 assert decrypted_entities[0].entity_type == "PHONE"
def test_given_valid_json_then_anonymizers_config_list_created_successfully(): content = get_content() anonymizers_config = AppEntitiesConvertor.operators_config_from_json( content.get("anonymizers")) assert len(anonymizers_config) == 2 phone_number_anonymizer = anonymizers_config.get("PHONE_NUMBER") assert phone_number_anonymizer.params == { "masking_char": "*", "chars_to_mask": 4, "from_end": True, } assert phone_number_anonymizer.operator_name == "mask" default_anonymizer = anonymizers_config.get("DEFAULT") assert default_anonymizer.params == {"new_value": "ANONYMIZED"} assert default_anonymizer.operator_name == "replace"
def test_given_invalid_json_for_analyzer_result_then_we_fail( request_json, result_text): with pytest.raises(InvalidParamException) as e: AppEntitiesConvertor.analyzer_results_from_json(request_json) assert result_text == e.value.err_msg
def test_given_anonymize_called_with_multiple_scenarios_then_expected_results_returned( analyzer_json, result): analyzer_results = AppEntitiesConvertor.analyzer_results_from_json( analyzer_json) assert analyzer_results == result