~dobey/ubuntu/natty/ubuntuone-control-panel/release-0-9-1

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/integrationtests/test_dbus_client_sso.py

  • Committer: Bazaar Package Importer
  • Author(s): Natalia Bidart (nessita)
  • Date: 2010-12-06 12:27:11 UTC
  • Revision ID: james.westby@ubuntu.com-20101206122711-0wvvlliao34bjztf
Tags: upstream-0.0.9
ImportĀ upstreamĀ versionĀ 0.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Authors: Alejandro J. Cura <alecu@canonical.com>
 
4
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
 
5
#
 
6
# Copyright 2010 Canonical Ltd.
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify it
 
9
# under the terms of the GNU General Public License version 3, as published
 
10
# by the Free Software Foundation.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
# PURPOSE.  See the GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along
 
18
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
"""Tests for the DBus service when accessing Single Sign On."""
 
21
 
 
22
# pylint: disable=C0103
 
23
# DBus signals have CamelCased names
 
24
 
 
25
import dbus
 
26
import ubuntu_sso
 
27
 
 
28
from twisted.internet.defer import inlineCallbacks
 
29
 
 
30
from ubuntuone.controlpanel import dbus_client
 
31
from ubuntuone.controlpanel.integrationtests import (MockDBusNoMethods,
 
32
    DBusClientTestCase)
 
33
 
 
34
 
 
35
SAMPLE_CREDS = {
 
36
    "token": "ABCDEF12345678",
 
37
    "access_token": "DEADCAFE2010",
 
38
}
 
39
 
 
40
# pylint: disable=C0322
 
41
# pylint, you have to go to decorator's school
 
42
 
 
43
 
 
44
class MockDBusSSOService(dbus.service.Object):
 
45
    """A mock object that mimicks ussoc."""
 
46
 
 
47
    @dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
48
                         in_signature="sssx")
 
49
    def login_or_register_to_get_credentials(self, app_name, tcurl, hlp, wid):
 
50
        """Get creds from the keyring, login/register if needed."""
 
51
        self.CredentialsFound(app_name, SAMPLE_CREDS)
 
52
 
 
53
    @dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
54
                         signature="sa{ss}")
 
55
    def CredentialsFound(self, app_name, credentials):
 
56
        """Credentials were finally found."""
 
57
 
 
58
 
 
59
class MockDBusSSOServiceOther(dbus.service.Object):
 
60
    """A mock object that mimicks ussoc."""
 
61
 
 
62
    @dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
63
                         in_signature="sssx")
 
64
    def login_or_register_to_get_credentials(self, app_name, tcurl, hlp, wid):
 
65
        """Get creds from the keyring, login/register if needed."""
 
66
        self.CredentialsFound("wrong app name", {})
 
67
        self.CredentialsFound(app_name, SAMPLE_CREDS)
 
68
 
 
69
    @dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
70
                         signature="sa{ss}")
 
71
    def CredentialsFound(self, app_name, credentials):
 
72
        """Credentials were finally found."""
 
73
 
 
74
 
 
75
class MockDBusSSOClientOtherFailing(dbus.service.Object):
 
76
    """A mock object that mimicks ussoc."""
 
77
 
 
78
    @dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
79
                         in_signature="sssx")
 
80
    def login_or_register_to_get_credentials(self, app_name, tcurl, hlp, wid):
 
81
        """Get creds from the keyring, login/register if needed."""
 
82
        self.CredentialsError("wrong app", "error message", "error details")
 
83
        self.CredentialsFound(app_name, SAMPLE_CREDS)
 
84
 
 
85
    @dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
86
                         signature="sa{ss}")
 
87
    def CredentialsFound(self, app_name, credentials):
 
88
        """Credentials were finally found."""
 
89
 
 
90
    @dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
91
                         signature="sss")
 
92
    def CredentialsError(self, app_name, error_message, detailed_error):
 
93
        """Some error happened and credentials were not found."""
 
94
 
 
95
 
 
96
class MockDBusSSOClientFailing(dbus.service.Object):
 
97
    """A mock object that mimicks ussoc but fails."""
 
98
 
 
99
    @dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
100
                         in_signature="sssx")
 
101
    def login_or_register_to_get_credentials(self, app_name, tcurl, hlp, wid):
 
102
        """Fail while trying to get creds from the keyring."""
 
103
        self.CredentialsError(app_name, "error message", "error details")
 
104
 
 
105
    @dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
 
106
                         signature="sss")
 
107
    def CredentialsError(self, app_name, error_message, detailed_error):
 
108
        """Some error happened and credentials were not found."""
 
109
 
 
110
 
 
111
class SSOClientTestCase(DBusClientTestCase):
 
112
    """Test for the SSO dbus client."""
 
113
 
 
114
    @inlineCallbacks
 
115
    def test_get_credentials_ok(self):
 
116
        """Test the success case for get_credentials."""
 
117
        self.register_mockserver(ubuntu_sso.DBUS_BUS_NAME,
 
118
                                 ubuntu_sso.DBUS_CRED_PATH, MockDBusSSOService)
 
119
        creds = yield dbus_client.get_credentials()
 
120
        self.assertEqual(creds, SAMPLE_CREDS)
 
121
 
 
122
    @inlineCallbacks
 
123
    def test_get_credentials_other(self):
 
124
        """Creds for other apps are ignored."""
 
125
        self.register_mockserver(ubuntu_sso.DBUS_BUS_NAME,
 
126
                                 ubuntu_sso.DBUS_CRED_PATH,
 
127
                                 MockDBusSSOServiceOther)
 
128
        creds = yield dbus_client.get_credentials()
 
129
        self.assertEqual(creds, SAMPLE_CREDS)
 
130
 
 
131
    @inlineCallbacks
 
132
    def test_get_credentials_error(self):
 
133
        """Test what happens when the creds can't be retrieved."""
 
134
        self.register_mockserver(ubuntu_sso.DBUS_BUS_NAME,
 
135
                                 ubuntu_sso.DBUS_CRED_PATH,
 
136
                                 MockDBusSSOClientFailing)
 
137
 
 
138
        yield self.assertFailure(dbus_client.get_credentials(),
 
139
                                 dbus_client.CredentialsError)
 
140
 
 
141
    @inlineCallbacks
 
142
    def test_get_credentials_other_error(self):
 
143
        """Other creds err before ours are retrieved."""
 
144
        self.register_mockserver(ubuntu_sso.DBUS_BUS_NAME,
 
145
                                 ubuntu_sso.DBUS_CRED_PATH,
 
146
                                 MockDBusSSOClientOtherFailing)
 
147
 
 
148
        creds = yield dbus_client.get_credentials()
 
149
        self.assertEqual(creds, SAMPLE_CREDS)
 
150
 
 
151
    @inlineCallbacks
 
152
    def test_get_credentials_dbus_error(self):
 
153
        """Test what happens when there's a DBus error."""
 
154
        self.register_mockserver(ubuntu_sso.DBUS_BUS_NAME,
 
155
                                 ubuntu_sso.DBUS_CRED_PATH,
 
156
                                 MockDBusNoMethods)
 
157
 
 
158
        yield self.assertFailure(dbus_client.get_credentials(),
 
159
                                 dbus.DBusException)