32
32
class FakeSSOProxy(object):
33
33
"""A fake SSOProxy."""
35
def find_credentials(self, app_name, options, **kwargs):
36
"""Initialize this fake."""
39
"credentials_cleared",
41
"credentials_not_found",
42
"authorization_denied",
46
handler_name = "on_%s" % s
47
callback_name = "on_%s_cb" % s
49
def make_handler(callback_name):
50
"""Create a handler for a given callback_name."""
53
"""A signal was called."""
54
callback = getattr(self, callback_name, None)
55
if callback is not None:
60
setattr(self, handler_name, make_handler(callback_name))
61
setattr(self, callback_name, None)
64
def find_credentials(self, app_name, options):
36
65
"""Ask the U1 credentials."""
37
66
return defer.succeed(TEST_CREDENTIALS)
68
def clear_credentials(self, app_name, options):
69
"""Clear the U1 credentials."""
70
return defer.succeed(None)
72
def store_credentials(self, app_name, options):
73
"""Store the U1 credentials."""
74
return defer.succeed(None)
76
def register(self, app_name, options):
77
"""Store the U1 credentials."""
78
return defer.succeed(None)
80
def login(self, app_name, options):
81
"""Store the U1 credentials."""
82
return defer.succeed(None)
85
class RemovableSignalTestCase(TestCase):
86
"""Tests for RemovableSignal."""
89
"""Initialize this fake instance."""
90
self.proxy = FakeSSOProxy()
92
def test_dunder_callable(self):
93
"""__call__ works as expected."""
95
test_cb = lambda app_name, creds: sample_store.append(creds)
96
rs = credentials.RemovableSignal(self.proxy, "on_credentials_found_cb",
98
rs(TEST_APP_NAME, TEST_CREDENTIALS)
99
self.assertEqual(sample_store[0], TEST_CREDENTIALS)
101
def test_remove(self):
102
"""The signal has a .remove that removes the callback."""
104
test_cb = lambda app_name, creds: sample_store.append(creds)
105
rs = credentials.RemovableSignal(self.proxy, "on_credentials_found_cb",
108
rs(TEST_APP_NAME, TEST_CREDENTIALS)
109
self.assertEqual(len(sample_store), 0)
40
112
class CredentialsManagementTestCase(TestCase):
41
113
"""Tests for CredentialsManagement."""
55
127
self.cm.find_credentials(reply_handler=ok, error_handler=error)
130
def test_clear_credentials(self):
131
"""Test the clear_credentials method."""
133
ok = lambda: d.callback("ok")
134
error = lambda *args: d.errback(args)
135
self.cm.clear_credentials(reply_handler=ok, error_handler=error)
138
def test_store_credentials(self):
139
"""Test the store_credentials method."""
141
ok = lambda: d.callback("ok")
142
error = lambda *args: d.errback(args)
143
self.cm.store_credentials(TEST_CREDENTIALS, reply_handler=ok,
147
def test_register(self):
148
"""Test the register method."""
150
ok = lambda: d.callback("ok")
151
error = lambda *args: d.errback(args)
152
self.cm.register({}, reply_handler=ok, error_handler=error)
155
def test_login(self):
156
"""Test the login method."""
158
ok = lambda: d.callback("ok")
159
error = lambda *args: d.errback(args)
160
self.cm.login({}, reply_handler=ok, error_handler=error)
58
163
def test_register_to_credentials_found(self):
59
164
"""Test the register_to_credentials_found method."""
60
165
cb = lambda creds: self.assertEqual(creds, TEST_CREDENTIALS)
67
172
other_cb = self.cm.register_to_credentials_not_found(cb)
68
173
other_cb(TEST_APP_NAME)
175
def _verify_not_called_twice(self, signal_name, *args):
176
"""Test that the callback is not called twice."""
179
def signal_handler(*args):
180
"""Fake the behaviour of CredentialsManagementToolRoot."""
181
d.callback(args[0] if len(args) > 0 else None)
183
register = getattr(self.cm, "register_to_" + signal_name)
184
signal = register(signal_handler)
185
proxy_cb = getattr(self.proxy, "on_" + signal_name)
187
if getattr(signal, "remove", False):
191
def test_not_called_twice_credentials_stored(self):
192
"""Test that on_credentials_stored is not called twice."""
193
self._verify_not_called_twice("credentials_stored")
195
def test_not_called_twice_credentials_cleared(self):
196
"""Test that on_credentials_cleared is not called twice."""
197
self._verify_not_called_twice("credentials_cleared")
199
def test_not_called_twice_credentials_found(self):
200
"""Test that on_credentials_found is not called twice."""
201
self._verify_not_called_twice("credentials_found", TEST_APP_NAME,
204
def test_not_called_twice_credentials_not_found(self):
205
"""Test that on_credentials_not_found is not called twice."""
206
self._verify_not_called_twice("credentials_not_found")
208
def test_not_called_twice_authorization_denied(self):
209
"""Test that on_authorization_denied is not called twice."""
210
self._verify_not_called_twice("authorization_denied")
212
def test_not_called_twice_credentials_error(self):
213
"""Test that on_credentials_error is not called twice."""
214
self._verify_not_called_twice("credentials_error", TEST_ERROR_DICT)