Repository: oppia/oppia
File: core/controllers/base_test.py (Python)

Code before:
        self.mock_request = webapp2.Request.blank('/')
        self.mock_request.environ['REQUEST_METHOD'] = 'POST'
        self.mock_response = webapp2.Response()

        self.logged_warnings: List[str] = []
        self.logged_exceptions: List[str] = []

        def mock_logging_warning(msg: str) -> None:
            self.logged_warnings.append(msg)

        def mock_logging_exception(msg: str) -> None:
            self.logged_exceptions.append(msg)

        self.mock_logging_warning = mock_logging_warning
        self.mock_logging_exception = mock_logging_exception

        self.handler: base.BaseHandler[Dict[str, str], Dict[str, str]] = base.BaseHandler(self.mock_request, self.mock_response)

    def test_handle_not_logged_in_exception_logs_warning(self) -> None:
        """Ensures NotLoggedInException logs a warning with the correct format."""
        with self.swap(logging, 'warning', self.mock_logging_warning):
            self.handler.handle_exception(
                self.handler.NotLoggedInException('Unauthenticated user'),
                False
            )
        expected_log_message = (
            '\n\nStack Trace: \nNoneType: None\n\n'
            'Type Exception: NotLoggedInException\n'
            'Error Message: Unauthenticated user\n'
            'URL requested: %s\n'
            'Request method: POST\n'
            'Handler class name: BaseHandler\n\n'
        ) % (self.handler.request.uri,)
        self.assertIn(
            expected_log_message,
            self.logged_warnings,
            msg='NotLoggedInException message not match'
        )

    def test_not_found_exception_logs_warning(self) -> None:
        """Ensures NotFoundException logs a warning with the correct format."""
        with self.swap(logging, 'warning', self.mock_logging_warning):
            self.handler.handle_exception(
                self.handler.NotFoundException('Invalid URL requested'),
                False
            )
        expected_log_message = (
            '\n\nStack Trace: \nNoneType: None\n\n'
            'Type Exception: NotFoundException\n'
            'Error Message: Invalid URL requested\n'
            'URL requested: %s\n'

Review comment (suggestion):
For readability, how about using triple-quoted strings here and below? See e.g. how SAMPLE_EXPORTED_DICT in exp_services_test.py is handled.

Code after:
        self.mock_request = webapp2.Request.blank('/')
        self.mock_request.environ['REQUEST_METHOD'] = 'POST'
        self.mock_response = webapp2.Response()

        self.logged_warnings: List[str] = []
        self.logged_exceptions: List[str] = []

        def mock_logging_warning(msg: str) -> None:
            self.logged_warnings.append(msg)

        def mock_logging_exception(msg: str) -> None:
            self.logged_exceptions.append(msg)

        self.mock_logging_warning = mock_logging_warning
        self.mock_logging_exception = mock_logging_exception

        self.handler: base.BaseHandler[Dict[str, str], Dict[str, str]] = base.BaseHandler(
            self.mock_request, self.mock_response
        )

    def test_handle_not_logged_in_exception_logs_warning(self) -> None:
        """Ensures NotLoggedInException logs a warning with the correct format."""
        with self.swap(logging, 'warning', self.mock_logging_warning):
            self.handler.handle_exception(
                self.handler.NotLoggedInException('Unauthenticated user'),
                False
            )
        expected_log_message = f"""

NotLoggedInException: Unauthenticated user

Stack Trace: 
NoneType: None

URL requested: {self.handler.request.uri}
Request method: POST
Handler class name: BaseHandler
"""
        self.assertIn(
            expected_log_message,
            self.logged_warnings,
            msg='NotLoggedInException message not match'
        )

    def test_not_found_exception_logs_warning(self) -> None:
        """Ensures NotFoundException logs a warning with the correct format."""
        with self.swap(logging, 'warning', self.mock_logging_warning):
            self.handler.handle_exception(
                self.handler.NotFoundException('Invalid URL requested'),
                False
            )