~ubuntu-branches/ubuntu/precise/ubuntuone-client/precise-201112142106

« back to all changes in this revision

Viewing changes to tests/platform/windows/test_credentials.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-08-09 12:47:56 UTC
  • mfrom: (1.1.53 upstream)
  • Revision ID: james.westby@ubuntu.com-20110809124756-7nzilp3oix0a1yl9
Tags: 1.7.1-0ubuntu1
* New upstream release.
* debian/*:
  - Removed obsolete pycompat file
  - Removed ubuntuone-client-gnome deps and binary packaging, as it was
    moved out to separate project upstream.
  - Updated copyright to remove obsolete file reference
* debian/patches:
  - Removed patches which have been included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from ubuntuone.platform.windows import credentials
24
24
 
25
25
TEST_APP_NAME = "test application"
26
 
 
 
26
TEST_ERROR_DICT = {}
27
27
TEST_CREDENTIALS = {
28
28
    "token": "1234token",
29
29
}
32
32
class FakeSSOProxy(object):
33
33
    """A fake SSOProxy."""
34
34
 
35
 
    def find_credentials(self, app_name, options, **kwargs):
 
35
    def __init__(self):
 
36
        """Initialize this fake."""
 
37
        signals = [
 
38
            "credentials_stored",
 
39
            "credentials_cleared",
 
40
            "credentials_found",
 
41
            "credentials_not_found",
 
42
            "authorization_denied",
 
43
            "credentials_error",
 
44
        ]
 
45
        for s in signals:
 
46
            handler_name = "on_%s" % s
 
47
            callback_name = "on_%s_cb" % s
 
48
 
 
49
            def make_handler(callback_name):
 
50
                """Create a handler for a given callback_name."""
 
51
 
 
52
                def handler(*args):
 
53
                    """A signal was called."""
 
54
                    callback = getattr(self, callback_name, None)
 
55
                    if callback is not None:
 
56
                        callback(*args)
 
57
 
 
58
                return handler
 
59
 
 
60
            setattr(self, handler_name, make_handler(callback_name))
 
61
            setattr(self, callback_name, None)
 
62
 
 
63
 
 
64
    def find_credentials(self, app_name, options):
36
65
        """Ask the U1 credentials."""
37
66
        return defer.succeed(TEST_CREDENTIALS)
38
67
 
 
68
    def clear_credentials(self, app_name, options):
 
69
        """Clear the U1 credentials."""
 
70
        return defer.succeed(None)
 
71
 
 
72
    def store_credentials(self, app_name, options):
 
73
        """Store the U1 credentials."""
 
74
        return defer.succeed(None)
 
75
 
 
76
    def register(self, app_name, options):
 
77
        """Store the U1 credentials."""
 
78
        return defer.succeed(None)
 
79
 
 
80
    def login(self, app_name, options):
 
81
        """Store the U1 credentials."""
 
82
        return defer.succeed(None)
 
83
 
 
84
 
 
85
class RemovableSignalTestCase(TestCase):
 
86
    """Tests for RemovableSignal."""
 
87
 
 
88
    def setUp(self):
 
89
        """Initialize this fake instance."""
 
90
        self.proxy = FakeSSOProxy()
 
91
 
 
92
    def test_dunder_callable(self):
 
93
        """__call__ works as expected."""
 
94
        sample_store = []
 
95
        test_cb = lambda app_name, creds: sample_store.append(creds)
 
96
        rs = credentials.RemovableSignal(self.proxy, "on_credentials_found_cb",
 
97
                                         test_cb)
 
98
        rs(TEST_APP_NAME, TEST_CREDENTIALS)
 
99
        self.assertEqual(sample_store[0], TEST_CREDENTIALS)
 
100
 
 
101
    def test_remove(self):
 
102
        """The signal has a .remove that removes the callback."""
 
103
        sample_store = []
 
104
        test_cb = lambda app_name, creds: sample_store.append(creds)
 
105
        rs = credentials.RemovableSignal(self.proxy, "on_credentials_found_cb",
 
106
                                         test_cb)
 
107
        rs.remove()
 
108
        rs(TEST_APP_NAME, TEST_CREDENTIALS)
 
109
        self.assertEqual(len(sample_store), 0)
 
110
 
39
111
 
40
112
class CredentialsManagementTestCase(TestCase):
41
113
    """Tests for CredentialsManagement."""
55
127
        self.cm.find_credentials(reply_handler=ok, error_handler=error)
56
128
        return d
57
129
 
 
130
    def test_clear_credentials(self):
 
131
        """Test the clear_credentials method."""
 
132
        d = defer.Deferred()
 
133
        ok = lambda: d.callback("ok")
 
134
        error = lambda *args: d.errback(args)
 
135
        self.cm.clear_credentials(reply_handler=ok, error_handler=error)
 
136
        return d
 
137
 
 
138
    def test_store_credentials(self):
 
139
        """Test the store_credentials method."""
 
140
        d = defer.Deferred()
 
141
        ok = lambda: d.callback("ok")
 
142
        error = lambda *args: d.errback(args)
 
143
        self.cm.store_credentials(TEST_CREDENTIALS, reply_handler=ok,
 
144
                                  error_handler=error)
 
145
        return d
 
146
 
 
147
    def test_register(self):
 
148
        """Test the register method."""
 
149
        d = defer.Deferred()
 
150
        ok = lambda: d.callback("ok")
 
151
        error = lambda *args: d.errback(args)
 
152
        self.cm.register({}, reply_handler=ok, error_handler=error)
 
153
        return d
 
154
 
 
155
    def test_login(self):
 
156
        """Test the login method."""
 
157
        d = defer.Deferred()
 
158
        ok = lambda: d.callback("ok")
 
159
        error = lambda *args: d.errback(args)
 
160
        self.cm.login({}, reply_handler=ok, error_handler=error)
 
161
        return d
 
162
 
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)
66
171
        cb = lambda: None
67
172
        other_cb = self.cm.register_to_credentials_not_found(cb)
68
173
        other_cb(TEST_APP_NAME)
 
174
 
 
175
    def _verify_not_called_twice(self, signal_name, *args):
 
176
        """Test that the callback is not called twice."""
 
177
        d = defer.Deferred()
 
178
 
 
179
        def signal_handler(*args):
 
180
            """Fake the behaviour of CredentialsManagementToolRoot."""
 
181
            d.callback(args[0] if len(args) > 0 else None)
 
182
 
 
183
        register = getattr(self.cm, "register_to_" + signal_name)
 
184
        signal = register(signal_handler)
 
185
        proxy_cb = getattr(self.proxy, "on_" + signal_name)
 
186
        proxy_cb(*args)
 
187
        if getattr(signal, "remove", False):
 
188
            signal.remove()
 
189
        proxy_cb(*args)
 
190
 
 
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")
 
194
 
 
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")
 
198
 
 
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,
 
202
                                      TEST_CREDENTIALS)
 
203
 
 
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")
 
207
 
 
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")
 
211
 
 
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)