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 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 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")