1
# Copyright 2010 Canonical Ltd.
3
# This file is part of desktopcouch.
5
# desktopcouch is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3
7
# as published by the Free Software Foundation.
9
# desktopcouch is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with desktopcouch. If not, see <http://www.gnu.org/licenses/>.
17
# Authors: Manuel de la pena <manuel.delapena@canonical.com>
19
"""Tests for the Ubuntu One pairing."""
23
from mocker import MockerTestCase
25
from desktopcouch.application.plugins import ubuntuone_pairing as uone
26
from desktopcouch.application.plugins.ubuntuone_pairing import (
27
pair_with_ubuntuone, U1_PAIR_RECORD, MAP_JS)
30
class TestUbuntonePairing(MockerTestCase):
31
"""Tests for Ubuntu One Pairing."""
35
super(TestUbuntonePairing, self).setUp()
36
# set a number of mocks that are used in all tests
37
self.couchdb = self.mocker.mock()
38
self.put_static_paired_service = self.mocker.replace(
39
'desktopcouch.application.pair.couchdb_pairing.couchdb_io.' +
40
'put_static_paired_service')
42
def test_pair_with_ubuntuone_no_view(self):
43
"""Test that when the view is not present it is indeed created."""
44
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
45
self.mocker.result(False)
46
# we are interested in the fact that the view is created
47
self.couchdb.add_view(
48
U1_PAIR_RECORD, MAP_JS, design_doc=U1_PAIR_RECORD)
49
# added to esure that the method carries on normally
50
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
51
self.mocker.result([])
52
self.put_static_paired_service(None, "ubuntuone")
54
pair_with_ubuntuone(self.couchdb)
57
def test_pair_with_ubuntuone_no_record(self):
58
"""Ensure pairing is not done when there are no records ."""
59
# execute the steps when no records are returned by the view
60
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
61
self.mocker.result(True)
62
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
63
self.mocker.result([])
64
self.put_static_paired_service(None, "ubuntuone")
66
pair_with_ubuntuone(self.couchdb)
69
def test_pair_with_ubuntuone_user_deleted_record(self):
70
"""Ensure pairing is not done when the user explicitly removed it."""
71
# create a mock object that will be the result from the view
72
row = self.mocker.mock()
73
# execute the steps to show that the user deleted the record
74
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
75
self.mocker.result(True)
76
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
77
self.mocker.result([row])
78
# FIXME does this do anything?
82
pair_with_ubuntuone(self.couchdb)
85
def test_pair_with_ubuntuone_record_present(self):
86
"""Ensure pairing is not done when the record is already present."""
87
# create a mock object that will be the result from the view
88
row = self.mocker.mock()
89
# execute the steps to show that the user deleted the record
90
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
91
self.mocker.result(True)
92
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
93
self.mocker.result([row])
94
# FIXME does this do anything?
98
pair_with_ubuntuone(self.couchdb)
102
class TestUbuntuOnePlugin(MockerTestCase):
103
"""Test the signal handling in the plug-in."""
106
super(TestUbuntuOnePlugin, self).setUp()
109
def test_got_new_credentials_other(self):
110
"""Check that pairing is not called."""
111
def fail_if_called(db=None):
112
"""Fail if we get called."""
115
uone.pair_with_ubuntuone = fail_if_called
116
uone.got_new_credentials('Unknown App', {})
117
self.assertFalse(self.called, 'pair_with_ubuntuone was not expected.')
119
def test_got_new_credentials(self):
120
"""Check that pairing is called for Ubuntu One."""
121
def pass_if_called(db=None):
122
"""Check that pair_with_ubuntuone was called."""
125
uone.pair_with_ubuntuone = pass_if_called
126
uone.got_new_credentials(uone.APP_NAME, {})
127
self.assertTrue(self.called, 'pair_with_ubuntuone was not called.')
129
def test_listen_to_dbus(self):
130
"""Test that listening to credentails works."""
131
session_class = self.mocker.replace("dbus.SessionBus")
132
iface_class = self.mocker.replace("dbus.Interface")
135
bus = self.mocker.mock()
136
self.mocker.result(bus)
138
iface = ubuntu_sso.DBUS_CREDENTIALS_IFACE
139
bus.add_signal_receiver(handler_function=uone.got_new_credentials,
140
signal_name='CredentialsFound',
141
dbus_interface=iface)
143
bus.get_object(ubuntu_sso.DBUS_BUS_NAME,
144
ubuntu_sso.DBUS_CREDENTIALS_PATH,
145
follow_name_owner_changes=True)
146
an_obj = self.mocker.mock()
147
self.mocker.result(an_obj)
149
iface_class(an_obj, dbus_interface=iface)
150
sso_backend = self.mocker.mock()
151
self.mocker.result(sso_backend)
153
sso_backend.find_credentials(uone.APP_NAME, {})
157
uone.listen_to_dbus()