Esempio n. 1
0
    def test_add_refresh(self) -> None:
        """Test adding refresh header."""
        response = Response()

        self.assertEqual(None, response.headers.get("refresh"))

        ResponseGenerator.add_refresh(response, 15)

        self.assertEqual("15", response.headers.get("refresh"))
Esempio n. 2
0
    def test_serve_from_filesystem_passes_verify_file_path_errors(
        self,
        mocked_verify_file_path: MagicMock,
    ) -> None:
        """Test that exceptions from verify_file_path are propagated."""
        mocked_verify_file_path.side_effect = NotFoundException(
            "Expected Error Message")

        with self.assertRaisesRegex(NotFoundException,
                                    "Expected Error Message"):
            ResponseGenerator.serve_from_filesystem("foo.txt")
Esempio n. 3
0
    def test_from_exception_for_not_found_exception(self) -> None:
        """Test from_exception for NotFoundException."""
        message = "There's nothing here!"

        response = ResponseGenerator.from_exception(NotFoundException(message))

        self.assertEqual(404, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 4
0
    def test_from_exception_for_exception(self) -> None:
        """Test from_exception for Exception."""
        message = "Something crashed!"

        response = ResponseGenerator.from_exception(Exception(message))

        self.assertEqual(500, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 5
0
    def test_from_exception_for_internal_exception(self) -> None:
        """Test from_exception for InternalException."""
        message = "Domain code crashed!"

        response = ResponseGenerator.from_exception(InternalException(message))

        self.assertEqual(500, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 6
0
    def test_from_exception_for_access_denied_exception(self) -> None:
        """Test from_exception for AccessDeniedException."""
        message = "You can't enter here!"

        response = ResponseGenerator.from_exception(
            AccessDeniedException(message))

        self.assertEqual(403, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 7
0
    def test_from_exception_for_client_error_exception(self) -> None:
        """Test from_exception for ClientErrorException."""
        message = "Request is invalid!"

        response = ResponseGenerator.from_exception(
            ClientErrorException(message))

        self.assertEqual(400, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 8
0
def handle_api_call(subpath: str) -> Any:
    """Handle API access."""
    try:
        parameters = build_parameters(subpath, request)
        response = router.handle(parameters)
        if isinstance(response, WebResponse):
            return response
        return jsonify(response.data)
    except Exception as err:
        if isinstance(err, InternalException):
            log.critical(err)
        return ResponseGenerator.from_exception(err)
Esempio n. 9
0
    def get_output(data: dict) -> Response:
        """Get config file for requested Workload."""
        workload_id = RequestDataProcessor.get_string_value(data, "workload_id")
        workdir = Workdir()
        workload_data = workdir.get_workload_data(workload_id)
        log_path = workload_data.get("log_path")

        if not log_path:
            raise NotFoundException(f"Unable to find output log for {workload_id}")

        try:
            response = ResponseGenerator.serve_from_filesystem(
                path=log_path,
                mimetype="text/plain",
            )
        except Exception as e:
            response = ResponseGenerator.from_exception(e)

        return ResponseGenerator.add_refresh(
            response=response,
            refresh_time=3,
        )
Esempio n. 10
0
    def get_code_template(data: dict) -> Response:
        """Get code template file for requested Workload."""
        workload_id = RequestDataProcessor.get_string_value(data, "workload_id")
        workdir = Workdir()
        workload_data = workdir.get_workload_data(workload_id)
        code_template_path = workload_data.get("code_template_path")

        if not code_template_path:
            raise NotFoundException(f"Unable to find code template file for {workload_id}")

        return ResponseGenerator.serve_from_filesystem(
            path=code_template_path,
            mimetype="text/x-python",
        )
Esempio n. 11
0
    def test_serve_from_filesystem(
        self,
        mocked_verify_file_path: MagicMock,
        mocked_send_file: MagicMock,
    ) -> None:
        """Test serve_from_filesystem."""
        expected_value = Response("expected response text")

        mocked_verify_file_path.return_value = None
        mocked_send_file.return_value = expected_value

        actual = ResponseGenerator.serve_from_filesystem("foo.txt", "mimetype")

        self.assertEqual(expected_value, actual)
        mocked_verify_file_path.assert_called_once_with("foo.txt")
        mocked_send_file.assert_called_once_with(
            "foo.txt",
            mimetype="mimetype",
            as_attachment=False,
            cache_timeout=0,
        )