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
from mocker import Mocker
21
import desktopcouch.tests as test_environment
22
from desktopcouch.pair.couchdb_pairing.ubuntuone_pairing import (
23
pair_with_ubuntuone, U1_PAIR_RECORD, MAP_JS)
25
class TestUbuntonePairing(testtools.TestCase):
29
super(TestUbuntonePairing, self).setUp()
30
# set a number of mocks that are used in all tests
31
self.mocker = Mocker()
32
self.couchdb = self.mocker.mock()
33
self.put_static_paired_service = self.mocker.replace(
34
'desktopcouch.pair.couchdb_pairing.couchdb_io.' +
35
'put_static_paired_service')
37
def test_pair_with_ubuntuone_no_view(self):
38
"""Tests that when the view is not present it is indeed created."""
39
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
40
self.mocker.result(False)
41
# we are interested in the fact that the view is created
42
self.couchdb.add_view(U1_PAIR_RECORD, MAP_JS, None, U1_PAIR_RECORD)
43
# added to esure that the method carries on normally
44
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
45
self.mocker.result([])
46
self.put_static_paired_service(None, "ubuntuone")
48
pair_with_ubuntuone(self.couchdb)
52
def test_pair_with_ubuntuone_no_record(self):
53
"""Ensure pairing is not done when there are no records ."""
54
# execute the steps when no records are returned by the view
55
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
56
self.mocker.result(True)
57
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
58
self.mocker.result([])
59
self.put_static_paired_service(None, "ubuntuone")
61
pair_with_ubuntuone(self.couchdb)
64
def test_pair_with_ubuntuone_user_deleted_record(self):
65
"""Ensure pairing is not done when the user explicitly removed it."""
66
# create a mock object that will be the result from the view
67
row = self.mocker.mock()
68
# execute the steps to show that the user deleted the record
69
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
70
self.mocker.result(True)
71
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
72
self.mocker.result([row])
76
pair_with_ubuntuone(self.couchdb)
79
def test_pair_with_ubuntuone_record_present(self):
80
"""Ensure pairing is not done when the record is already present."""
81
# create a mock object that will be the result from the view
82
row = self.mocker.mock()
83
# execute the steps to show that the user deleted the record
84
self.couchdb.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD)
85
self.mocker.result(True)
86
self.couchdb.execute_view(U1_PAIR_RECORD, U1_PAIR_RECORD)
87
self.mocker.result([row])
91
pair_with_ubuntuone(self.couchdb)