def test_unexpectedLoginFailure(self):
        """
        If the portal raises an exception other than
        L{UnauthorizedLogin} or L{UnhandledCredentials}, the server
        responds with a C{BAD} response and the exception is logged.
        """

        class UnexpectedException(Exception):
            """
            An unexpected exception.
            """

        class FailingChecker:
            """
            A credentials checker whose L{requestAvatarId} method
            raises L{UnexpectedException}.
            """
            credentialInterfaces = (IUsernameHashedPassword,
                                    IUsernamePassword)

            def requestAvatarId(self, credentials):
                raise UnexpectedException("Unexpected error.")

        realm = TestRealm()
        portal = Portal(realm)
        portal.registerChecker(FailingChecker())
        self.server.portal = portal

        self.server.challengers[b'LOGIN'] = loginCred = imap4.LOGINCredentials

        verifyClass(IChallengeResponse, loginCred)

        cAuth = imap4.LOGINAuthenticator(b'testuser')
        self.client.registerAuthenticator(cAuth)

        def auth():
            return self.client.authenticate(b'secret')

        def assertUnexpectedExceptionLogged():
            self.assertTrue(self.flushLoggedErrors(UnexpectedException))

        d1 = self.connected.addCallback(strip(auth))
        d1.addErrback(self.assertClientFailureMessage,
                      b"Server error: login failed unexpectedly")
        d1.addCallback(strip(assertUnexpectedExceptionLogged))
        d1.addCallbacks(self._cbStopClient, self._ebGeneral)
        d = defer.gatherResults([self.loopback(), d1])
        return d

        If the portal raises an exception other than
        L{UnauthorizedLogin} or L{UnhandledCredentials}, the server
        responds with a C{BAD} response and the exception is logged.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/mail/test/test_imap.py