~ubuntu-branches/ubuntu/precise/desktopcouch/precise

« back to all changes in this revision

Viewing changes to desktopcouch/application/plugins/tests/test_ubuntuone_pairing.py

  • Committer: Bazaar Package Importer
  • Author(s): Chad MILLER
  • Date: 2011-01-12 15:08:25 UTC
  • mfrom: (1.5.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110112150825-bzvn23kzufr0qdyb
Tags: 1.0.5-0ubuntu1
* New upstream release, skipping a few buggy releases.
* Split code into binary packages:
  - desktopcouch, configuration files and dependencies, but no code.
  - python-desktopcouch: transitional package
  - python-desktopcouch-application: local DB startup and discovery
  - python-desktopcouch-records: library for DB access anywhere
  - python-desktopcouch-recordtypes: support specific data structures
  - desktopcouch-ubuntuone, replication and pairing with cloud service
* Drop patch that some maverick apps incorrectly needed.
  patches/0-items-should-expose-private-data-for-now.patch
* Update package compatibility-version, 6 -> 7.
* Use newer debhelper and use python-support instead of python-central.
* Depend on contemporary python-couchdb, instead of ancient version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.
 
2
#
 
3
# This file is part of desktopcouch.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
#
 
17
# Authors: Manuel de la pena <manuel.delapena@canonical.com>
 
18
 
 
19
"""Tests for the Ubuntu One pairing."""
 
20
 
 
21
import ubuntu_sso
 
22
 
 
23
from mocker import MockerTestCase
 
24
 
 
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)
 
28
 
 
29
 
 
30
class TestUbuntonePairing(MockerTestCase):
 
31
    """Tests for Ubuntu One Pairing."""
 
32
 
 
33
    def setUp(self):
 
34
        """setup each test"""
 
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')
 
41
 
 
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")
 
53
        self.mocker.replay()
 
54
        pair_with_ubuntuone(self.couchdb)
 
55
        self.mocker.verify()
 
56
 
 
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")
 
65
        self.mocker.replay()
 
66
        pair_with_ubuntuone(self.couchdb)
 
67
        self.mocker.verify()
 
68
 
 
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?
 
79
        _ = row.value
 
80
        self.mocker.result(1)
 
81
        self.mocker.replay()
 
82
        pair_with_ubuntuone(self.couchdb)
 
83
        self.mocker.verify()
 
84
 
 
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?
 
95
        _ = row.value
 
96
        self.mocker.result(0)
 
97
        self.mocker.replay()
 
98
        pair_with_ubuntuone(self.couchdb)
 
99
        self.mocker.verify()
 
100
 
 
101
 
 
102
class TestUbuntuOnePlugin(MockerTestCase):
 
103
    """Test the signal handling in the plug-in."""
 
104
 
 
105
    def setUp(self):
 
106
        super(TestUbuntuOnePlugin, self).setUp()
 
107
        self.called = False
 
108
 
 
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."""
 
113
            self.called = True
 
114
 
 
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.')
 
118
 
 
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."""
 
123
            self.called = True
 
124
 
 
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.')
 
128
 
 
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")
 
133
 
 
134
        session_class()
 
135
        bus = self.mocker.mock()
 
136
        self.mocker.result(bus)
 
137
 
 
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)
 
142
 
 
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)
 
148
 
 
149
        iface_class(an_obj, dbus_interface=iface)
 
150
        sso_backend = self.mocker.mock()
 
151
        self.mocker.result(sso_backend)
 
152
 
 
153
        sso_backend.find_credentials(uone.APP_NAME, {})
 
154
 
 
155
        self.mocker.replay()
 
156
 
 
157
        uone.listen_to_dbus()