'Test that login can be performed with IUsernamePassword credentials and
that when the connection is dropped the avatar is logged out.'

def test_loginLogout(self):

    self.portal.registerChecker(checkers.InMemoryUsernamePasswordDatabaseDontUse(user='pass'))
    creds = credentials.UsernamePassword('user', 'pass')
    mind = 'BRAINS!'
    loginCompleted = Deferred()
    d = self.clientFactory.login(creds, mind)
    def cbLogin(perspective):
        self.assertTrue(self.realm.lastPerspective.loggedIn)
        self.assertIsInstance(perspective, pb.RemoteReference)
        return loginCompleted
    def cbDisconnect(ignored):
        self.clientFactory.disconnect()
        self.completeClientLostConnection()
    d.addCallback(cbLogin)
    d.addCallback(cbDisconnect)
    def cbLogout(ignored):
        self.assertTrue(self.realm.lastPerspective.loggedOut)
    d.addCallback(cbLogout)
    self.establishClientAndServer()
    self.pump.flush()
    gc.collect()
    self.pump.flush()
    loginCompleted.callback(None)
    return d
