~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

« back to all changes in this revision

Viewing changes to tests/test_login.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2010-06-23 23:08:15 UTC
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20100623230815-4m3ugh10u9x9xzw5
Tags: upstream-1.3.2
ImportĀ upstreamĀ versionĀ 1.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# Author: Rodney Dawes <rodney.dawes@canonical.com>
4
 
#
5
 
# Copyright 2010 Canonical Ltd.
6
 
#
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.
10
 
#
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.
15
 
#
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 """
19
 
 
20
 
import dbus.service
21
 
import new
22
 
import os
23
 
 
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
28
 
 
29
 
class InvalidSignalError(Exception):
30
 
    """Exception for when we get the wrong signal called."""
31
 
    pass
32
 
 
33
 
class LoginTests(DBusTwistedTestCase):
34
 
    """Basic tests for the ubuntuone-login script """
35
 
 
36
 
    _path = os.path.join(os.getcwd(), "bin", "ubuntuone-login")
37
 
    u1login = new.module('u1login')
38
 
    execfile(_path, u1login.__dict__)
39
 
 
40
 
    def setUp(self):
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'
45
 
 
46
 
    def tearDown(self):
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)
55
 
        def shutdown(r):
56
 
            self.oauth.shutdown()
57
 
            dbus_interface.DBUS_PATH_AUTH = self._old_path
58
 
        d.addBoth(shutdown)
59
 
        d.addBoth(lambda _: DBusTwistedTestCase.tearDown(self))
60
 
        return d
61
 
 
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 """
66
 
            d.callback(True)
67
 
 
68
 
        def auth_denied():
69
 
            """ Override the callback """
70
 
            d.errback(Failure(InvalidSignalError()))
71
 
 
72
 
        def got_oauth_error(message=None):
73
 
            """ Override the callback """
74
 
            d.errback(Failure(InvalidSignalError()))
75
 
 
76
 
        def set_up_desktopcouch_pairing(consumer_key):
77
 
            """ Override the method """
78
 
            return
79
 
 
80
 
        def main():
81
 
            """ Override LoginMain.main """
82
 
            return
83
 
 
84
 
        login = self.u1login.LoginMain()
85
 
        login.main = main
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
90
 
 
91
 
        login._connect_dbus_signals()
92
 
 
93
 
        client = self.bus.get_object(self.u1login.DBUS_IFACE_AUTH_NAME,
94
 
                                     '/oauthdesktop',
95
 
                                     follow_name_owner_changes=True)
96
 
        d = defer.Deferred()
97
 
 
98
 
        def login_handler():
99
 
            """ login handler """
100
 
            return
101
 
 
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)
106
 
        return d
107
 
 
108
 
    def test_auth_denied(self):
109
 
        """ Test that denying authorization works correctly. """
110
 
 
111
 
        def new_creds(realm=None, consumer_key=None, sender=None):
112
 
            """ Override the callback """
113
 
            d.errback(Failure(InvalidSignalError()))
114
 
 
115
 
        def auth_denied():
116
 
            """ Override the callback """
117
 
            d.callback(True)
118
 
 
119
 
        def got_oauth_error(message=None):
120
 
            """ override the callback """
121
 
            d.errback(Failure(InvalidSignalError()))
122
 
 
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
128
 
 
129
 
        login._connect_dbus_signals()
130
 
 
131
 
        self.oauth.processor.next_login_with(self.oauth.processor.got_denial)
132
 
 
133
 
        client = self.bus.get_object(self.u1login.DBUS_IFACE_AUTH_NAME,
134
 
                                     '/oauthdesktop',
135
 
                                     follow_name_owner_changes=True)
136
 
        d = defer.Deferred()
137
 
 
138
 
        def login_handler():
139
 
            """ login handler """
140
 
            return
141
 
 
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)
146
 
        return d
147
 
 
148
 
    def test_oauth_error(self):
149
 
        """ Test that getting an error works correctly. """
150
 
 
151
 
        def new_creds(realm=None, consumer_key=None, sender=None):
152
 
            """ Override the callback """
153
 
            d.errback(Failure(InvalidSignalError()))
154
 
 
155
 
        def auth_denied():
156
 
            """ Override the callback """
157
 
            d.errback(Failure(InvalidSignalError()))
158
 
 
159
 
        def got_oauth_error(message=None):
160
 
            """ override the callback """
161
 
            d.callback(True)
162
 
 
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
168
 
 
169
 
        login._connect_dbus_signals()
170
 
 
171
 
        self.oauth.processor.next_login_with(self.oauth.processor.got_error,
172
 
                                             ('error!',))
173
 
 
174
 
        client = self.bus.get_object(self.u1login.DBUS_IFACE_AUTH_NAME,
175
 
                                     '/oauthdesktop',
176
 
                                     follow_name_owner_changes=True)
177
 
        d = defer.Deferred()
178
 
 
179
 
        def login_handler():
180
 
            """ login handler """
181
 
            return
182
 
 
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)
187
 
        return d