1
# -*- coding: utf-8 -*-
3
# Author: Rodney Dawes <rodney.dawes@canonical.com>
5
# Copyright 2010 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
18
""" Tests for the ubuntuone-login script """
24
from contrib.testing.testcase import DBusTwistedTestCase, FakeLogin
25
from twisted.internet import defer
26
from twisted.python.failure import Failure
27
from ubuntuone.syncdaemon import dbus_interface
29
class InvalidSignalError(Exception):
30
"""Exception for when we get the wrong signal called."""
33
class LoginTests(DBusTwistedTestCase):
34
"""Basic tests for the ubuntuone-login script """
36
_path = os.path.join(os.getcwd(), "bin", "ubuntuone-login")
37
u1login = new.module('u1login')
38
execfile(_path, u1login.__dict__)
41
DBusTwistedTestCase.setUp(self)
42
self.oauth = FakeLogin(self.bus)
43
self._old_path = dbus_interface.DBUS_PATH_AUTH
44
dbus_interface.DBUS_PATH_AUTH = '/oauthdesktop'
47
# collect all signal receivers registered during the test
48
signal_receivers = set()
49
with self.bus._signals_lock:
50
for group in self.bus._signal_recipients_by_object_path.values():
51
for matches in group.values():
52
for match in matches.values():
53
signal_receivers.update(match)
54
d = self.cleanup_signal_receivers(signal_receivers)
57
dbus_interface.DBUS_PATH_AUTH = self._old_path
59
d.addBoth(lambda _: DBusTwistedTestCase.tearDown(self))
62
def test_new_credentials(self):
63
""" Test logging in """
64
def new_creds(realm=None, consumer_key=None, sender=None):
65
""" Override the callback """
69
""" Override the callback """
70
d.errback(Failure(InvalidSignalError()))
72
def got_oauth_error(message=None):
73
""" Override the callback """
74
d.errback(Failure(InvalidSignalError()))
76
def set_up_desktopcouch_pairing(consumer_key):
77
""" Override the method """
81
""" Override LoginMain.main """
84
login = self.u1login.LoginMain()
86
login.new_credentials = new_creds
87
login.auth_denied = auth_denied
88
login.got_oauth_error = got_oauth_error
89
login.set_up_desktopcouch_pairing = set_up_desktopcouch_pairing
91
login._connect_dbus_signals()
93
client = self.bus.get_object(self.u1login.DBUS_IFACE_AUTH_NAME,
95
follow_name_owner_changes=True)
102
iface = dbus.Interface(client, self.u1login.DBUS_IFACE_AUTH_NAME)
103
iface.login('http://localhost', self.u1login.OAUTH_CONSUMER,
104
reply_handler=login_handler,
105
error_handler=self.error_handler)
108
def test_auth_denied(self):
109
""" Test that denying authorization works correctly. """
111
def new_creds(realm=None, consumer_key=None, sender=None):
112
""" Override the callback """
113
d.errback(Failure(InvalidSignalError()))
116
""" Override the callback """
119
def got_oauth_error(message=None):
120
""" override the callback """
121
d.errback(Failure(InvalidSignalError()))
123
login = self.u1login.LoginMain()
124
login.main = self.main
125
login.new_credentials = new_creds
126
login.auth_denied = auth_denied
127
login.got_oauth_error = got_oauth_error
129
login._connect_dbus_signals()
131
self.oauth.processor.next_login_with(self.oauth.processor.got_denial)
133
client = self.bus.get_object(self.u1login.DBUS_IFACE_AUTH_NAME,
135
follow_name_owner_changes=True)
139
""" login handler """
142
iface = dbus.Interface(client, self.u1login.DBUS_IFACE_AUTH_NAME)
143
iface.login('http://localhost', self.u1login.OAUTH_CONSUMER,
144
reply_handler=login_handler,
145
error_handler=self.error_handler)
148
def test_oauth_error(self):
149
""" Test that getting an error works correctly. """
151
def new_creds(realm=None, consumer_key=None, sender=None):
152
""" Override the callback """
153
d.errback(Failure(InvalidSignalError()))
156
""" Override the callback """
157
d.errback(Failure(InvalidSignalError()))
159
def got_oauth_error(message=None):
160
""" override the callback """
163
login = self.u1login.LoginMain()
164
login.main = self.main
165
login.new_credentials = new_creds
166
login.auth_denied = auth_denied
167
login.got_oauth_error = got_oauth_error
169
login._connect_dbus_signals()
171
self.oauth.processor.next_login_with(self.oauth.processor.got_error,
174
client = self.bus.get_object(self.u1login.DBUS_IFACE_AUTH_NAME,
176
follow_name_owner_changes=True)
180
""" login handler """
183
iface = dbus.Interface(client, self.u1login.DBUS_IFACE_AUTH_NAME)
184
iface.login('http://localhost', self.u1login.OAUTH_CONSUMER,
185
reply_handler=login_handler,
186
error_handler=self.error_handler)