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

Code before:
        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'
            'Request method: POST\n'

Review comment (suggestion):
Actually a question about this. How come the stack trace is None? Can you simulate a test with a non-empty stack trace, e.g. by firing a request to the controller? Look at some of the other tests in this file which use `get_json` etc.

Code after:
        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
            )
        expected_log_message = f"""