def setUp(self):
     yield super(CredentialsManagementTestCase, self).setUp()
     self._called = False
     self.proxy = FakeSSOProxy()
     self.cm = CredentialsManagement(self.proxy)
class CredentialsManagementTestCase(TestCase):
    """Tests for CredentialsManagement."""

    timeout = 5
    app_name = APP_NAME

    @defer.inlineCallbacks
    def setUp(self):
        yield super(CredentialsManagementTestCase, self).setUp()
        self._called = False
        self.proxy = FakeSSOProxy()
        self.cm = CredentialsManagement(self.proxy)

    def _set_called(self, *args, **kwargs):
        """Helper to keep track calls."""
        self._called = (args, kwargs)

    def assert_callback_called(self, expected):
        """Test that _called helper holds 'expected'."""
        self.assertEqual(self._called, expected)

    def test_find_credentials(self):
        """Test the find_credentials method."""
        d = defer.Deferred()
        ok = lambda: d.callback("ok")
        error = lambda *args: d.errback(args)
        self.cm.find_credentials(reply_handler=ok, error_handler=error)
        return d

    def test_clear_credentials(self):
        """Test the clear_credentials method."""
        d = defer.Deferred()
        ok = lambda: d.callback("ok")
        error = lambda *args: d.errback(args)
        self.cm.clear_credentials(reply_handler=ok, error_handler=error)
        return d

    def test_store_credentials(self):
        """Test the store_credentials method."""
        d = defer.Deferred()
        ok = lambda: d.callback("ok")
        error = lambda *args: d.errback(args)
        self.cm.store_credentials(TEST_CREDENTIALS, reply_handler=ok,
                                  error_handler=error)
        return d

    def test_register(self):
        """Test the register method."""
        d = defer.Deferred()
        ok = lambda: d.callback("ok")
        error = lambda *args: d.errback(args)
        self.cm.register({}, reply_handler=ok, error_handler=error)
        return d

    def test_login(self):
        """Test the login method."""
        d = defer.Deferred()
        ok = lambda: d.callback("ok")
        error = lambda *args: d.errback(args)
        self.cm.login({}, reply_handler=ok, error_handler=error)
        return d

    def test_login_email_password(self):
        """Test the login_email_password method."""
        d = defer.Deferred()
        ok = lambda: d.callback("ok")
        error = lambda *args: d.errback(args)
        self.cm.login_email_password({'email': 'foo', 'password': '******'},
                                     reply_handler=ok, error_handler=error)
        return d

    def test_register_to_credentials_found(self):
        """Test the register_to_credentials_found method."""
        signal = self.cm.register_to_credentials_found(self._set_called)
        signal(self.app_name, TEST_CREDENTIALS)
        self.assert_callback_called(((TEST_CREDENTIALS,), {}))

    def test_register_to_credentials_not_found(self):
        """Test the register_to_credentials_not_found method."""
        signal = self.cm.register_to_credentials_not_found(self._set_called)
        signal(self.app_name)
        self.assert_callback_called(((), {}))

    def test_register_to_credentials_stored(self):
        """Test the register_to_credentials_stored method."""
        signal = self.cm.register_to_credentials_stored(self._set_called)
        signal(self.app_name)
        self.assert_callback_called(((), {}))

    def test_register_to_credentials_cleared(self):
        """Test the register_to_credentials_cleared method."""
        signal = self.cm.register_to_credentials_cleared(self._set_called)
        signal(self.app_name)
        self.assert_callback_called(((), {}))

    def test_register_to_credentials_error(self):
        """Test the register_to_credentials_error method."""
        signal = self.cm.register_to_credentials_error(self._set_called)
        signal(self.app_name)
        self.assert_callback_called(((), {}))

    def test_register_to_authorization_denied(self):
        """Test the register_to_authorization_denied method."""
        signal = self.cm.register_to_authorization_denied(self._set_called)
        signal(self.app_name, TEST_ERROR_DICT)
        self.assert_callback_called(((TEST_ERROR_DICT,), {}))

    def _verify_not_called_twice(self, signal_name, *args):
        """Test that the callback is not called twice."""
        d = defer.Deferred()

        def signal_handler(*args):
            """Fake the behaviour of CredentialsManagementTool."""
            d.callback(args[0] if len(args) > 0 else None)

        register = getattr(self.cm, "register_to_" + signal_name)
        signal = register(signal_handler)
        proxy_cb = getattr(self.proxy, "on_" + signal_name)
        proxy_cb(*args)
        if getattr(signal, "remove", False):
            signal.remove()
        proxy_cb(*args)

    def test_not_called_twice_credentials_stored(self):
        """Test that on_credentials_stored is not called twice."""
        self._verify_not_called_twice("credentials_stored")

    def test_not_called_twice_credentials_cleared(self):
        """Test that on_credentials_cleared is not called twice."""
        self._verify_not_called_twice("credentials_cleared")

    def test_not_called_twice_credentials_found(self):
        """Test that on_credentials_found is not called twice."""
        self._verify_not_called_twice("credentials_found", self.app_name,
                                      TEST_CREDENTIALS)

    def test_not_called_twice_credentials_not_found(self):
        """Test that on_credentials_not_found is not called twice."""
        self._verify_not_called_twice("credentials_not_found")

    def test_not_called_twice_authorization_denied(self):
        """Test that on_authorization_denied is not called twice."""
        self._verify_not_called_twice("authorization_denied")

    def test_not_called_twice_credentials_error(self):
        """Test that on_credentials_error is not called twice."""
        self._verify_not_called_twice("credentials_error", TEST_ERROR_DICT)

    def test_connect_to_signal(self):
        """The connect_to_signal method is correct."""
        for signal_name in self.cm._SIGNAL_TO_CALLBACK_MAPPING:
            match = self.cm.connect_to_signal(signal_name, self._set_called)
            expected = object()
            match(APP_NAME, expected)
            self.assertEqual(self._called, ((expected,), {}))