def test_given_request_deanonymizers_return_list(): engine = DeanonymizeEngine() expected_list = ["decrypt"] anon_list = engine.get_deanonymizers() assert anon_list == expected_list
class Server: """Flask server for anonymizer.""" def __init__(self): fileConfig(Path(Path(__file__).parent, LOGGING_CONF_FILE)) self.logger = logging.getLogger("presidio-anonymizer") self.logger.setLevel(os.environ.get("LOG_LEVEL", self.logger.level)) self.app = Flask(__name__) self.logger.info("Starting anonymizer engine") self.anonymizer = AnonymizerEngine() self.deanonymize = DeanonymizeEngine() self.logger.info(WELCOME_MESSAGE) @self.app.route("/health") def health() -> str: """Return basic health probe result.""" return "Presidio Anonymizer service is up" @self.app.route("/anonymize", methods=["POST"]) 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")) if AppEntitiesConvertor.check_custom_operator(anonymizers_config): raise BadRequest("Custom type anonymizer is not supported") 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") @self.app.route("/deanonymize", methods=["POST"]) 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") @self.app.route("/anonymizers", methods=["GET"]) def anonymizers(): """Return a list of supported anonymizers.""" return jsonify(self.anonymizer.get_anonymizers()) @self.app.route("/deanonymizers", methods=["GET"]) def deanonymizers(): """Return a list of supported deanonymizers.""" return jsonify(self.deanonymize.get_deanonymizers()) @self.app.errorhandler(InvalidParamException) def invalid_param(err): self.logger.warning( f"Request failed with parameter validation error: {err.err_msg}" ) return jsonify(error=err.err_msg), 422 @self.app.errorhandler(HTTPException) def http_exception(e): return jsonify(error=e.description), e.code @self.app.errorhandler(Exception) def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500