1
# -*- coding: utf-8 -*-
3
# Authors: Alejandro J. Cura <alecu@canonical.com>
4
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
6
# Copyright 2010 Canonical Ltd.
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.
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.
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/>.
20
"""Tests for the DBus service when accessing Single Sign On."""
22
# pylint: disable=C0103
23
# DBus signals have CamelCased names
28
from twisted.internet.defer import inlineCallbacks
30
from ubuntuone.controlpanel import dbus_client
31
from ubuntuone.controlpanel.integrationtests import (MockDBusNoMethods,
36
"token": "ABCDEF12345678",
37
"access_token": "DEADCAFE2010",
40
# pylint: disable=C0322
41
# pylint, you have to go to decorator's school
44
class MockDBusSSOService(dbus.service.Object):
45
"""A mock object that mimicks ussoc."""
47
@dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
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)
53
@dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
55
def CredentialsFound(self, app_name, credentials):
56
"""Credentials were finally found."""
59
class MockDBusSSOServiceOther(dbus.service.Object):
60
"""A mock object that mimicks ussoc."""
62
@dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
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)
69
@dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
71
def CredentialsFound(self, app_name, credentials):
72
"""Credentials were finally found."""
75
class MockDBusSSOClientOtherFailing(dbus.service.Object):
76
"""A mock object that mimicks ussoc."""
78
@dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
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)
85
@dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
87
def CredentialsFound(self, app_name, credentials):
88
"""Credentials were finally found."""
90
@dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
92
def CredentialsError(self, app_name, error_message, detailed_error):
93
"""Some error happened and credentials were not found."""
96
class MockDBusSSOClientFailing(dbus.service.Object):
97
"""A mock object that mimicks ussoc but fails."""
99
@dbus.service.method(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
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")
105
@dbus.service.signal(dbus_interface=ubuntu_sso.DBUS_IFACE_CRED_NAME,
107
def CredentialsError(self, app_name, error_message, detailed_error):
108
"""Some error happened and credentials were not found."""
111
class SSOClientTestCase(DBusClientTestCase):
112
"""Test for the SSO dbus client."""
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)
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)
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)
138
yield self.assertFailure(dbus_client.get_credentials(),
139
dbus_client.CredentialsError)
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)
148
creds = yield dbus_client.get_credentials()
149
self.assertEqual(creds, SAMPLE_CREDS)
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,
158
yield self.assertFailure(dbus_client.get_credentials(),