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

« back to all changes in this revision

Viewing changes to desktopcouch/application/pair/tests/test_couchdb_io.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 2009 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
"""Test Couchdb IO"""
 
18
 
 
19
import pygtk
 
20
pygtk.require('2.0')
 
21
 
 
22
import desktopcouch.application.tests as test_environment
 
23
 
 
24
from desktopcouch.application.pair.couchdb_pairing import couchdb_io
 
25
from desktopcouch.application.server import DesktopDatabase
 
26
from desktopcouch.records import Record
 
27
from desktopcouch.application.replication_services import ubuntuone
 
28
import unittest
 
29
import uuid
 
30
import os
 
31
 
 
32
URI = None  # use autodiscovery that desktopcouch.tests permits.
 
33
 
 
34
 
 
35
class TestCouchdbIo(unittest.TestCase):
 
36
    """Test case for Couchdb IO"""
 
37
 
 
38
    def setUp(self):
 
39
        """setup each test"""
 
40
        self.mgt_database = DesktopDatabase('management', create=True, uri=URI,
 
41
                ctx=test_environment.test_context)
 
42
        self.foo_database = DesktopDatabase('foo', create=True, uri=URI,
 
43
                ctx=test_environment.test_context)
 
44
        #create some records to pull out and test
 
45
        self.foo_database = DesktopDatabase('bar', create=True, uri=URI,
 
46
                ctx=test_environment.test_context)
 
47
        #create some records to pull out and test
 
48
        self.foo_database.put_record(Record({
 
49
            "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3",
 
50
            "record_type": "test.com"}))
 
51
        self.foo_database.put_record(Record({
 
52
            "key2_1": "val2_1", "key2_2": "val2_2", "key2_3": "val2_3",
 
53
            "record_type": "test.com"}))
 
54
        self.foo_database.put_record(Record({
 
55
            "key13_1": "va31_1", "key3_2": "val3_2", "key3_3": "val3_3",
 
56
            "record_type": "test.com"}))
 
57
 
 
58
        couchdb_io.put_static_paired_service({
 
59
            "consumer_key": str("abcdef"),
 
60
            "consumer_secret": str("ghighjklm"),
 
61
            "token": str("opqrst"),
 
62
            "token_secret": str("uvwxyz")}, "ubuntuone", uri=URI,
 
63
            ctx=test_environment.test_context)
 
64
        excl = ubuntuone.ReplicationExclusion(
 
65
            ctx=test_environment.test_context)
 
66
        excl.exclude("bar")
 
67
 
 
68
    def tearDown(self):
 
69
        """tear down each test"""
 
70
        del self.mgt_database._server['management']  # pylint: disable=W0212
 
71
        del self.mgt_database._server['foo']         # pylint: disable=W0212
 
72
 
 
73
    def test_obsfuscation(self):
 
74
        """Test the obfuscation of sensitive data."""
 
75
        test_record = {
 
76
            'url':
 
77
            'https://couchdb.one.ubuntu.com/u%2Fb2%2Fc8%2F276%2Ftest',
 
78
            'auth': {
 
79
                'oauth': {
 
80
                    'consumer_secret': 'SeCrEtSe',
 
81
                    'token': '3XRjQrWX92TTTJFDTWJJ',
 
82
                    'consumer_key': 'ubuntuone',
 
83
                    'token_secret':
 
84
                        'jBmSeCrEtawkefwklefliwuregqwlkeh347wq87w4fiuq4fyu3q4f'
 
85
                        'iqwu4fqwfiqufM6xjsPwSeCrEt4'}}}
 
86
        cleaned_t = couchdb_io.obsfuscate(test_record)
 
87
        self.failIf(
 
88
            "SeCrEt" in str(cleaned_t), {
 
89
                'url':
 
90
                'https://couchdb.one.ubuntu.com/u%2Fb2%2Fc8%2F276%2Ftest',
 
91
                'auth': {'oauth': {
 
92
                    'consumer_secret': 'HiddenHidd',
 
93
                    'token': '3XRjQrWX92TTTJFDTWJJ',
 
94
                    'consumer_key': 'ubuntuone',
 
95
                    'token_secret':
 
96
                        'HiddenHiddenHiddenHiddenHiddenHiddenHiddenHiddenHidde'
 
97
                        'nHiddenHiddenHiddenHiddenHi'}}})
 
98
        self.assertEqual(couchdb_io.obsfuscate(""), "")
 
99
        self.assertEqual(couchdb_io.obsfuscate({}), {})
 
100
        self.assertEqual(couchdb_io.obsfuscate({1: {}}), {1: {}})
 
101
        self.assertEqual(couchdb_io.obsfuscate({1: 1}), {1: 1})
 
102
 
 
103
    def test_put_static_paired_service(self):  # pylint: disable=R0201
 
104
        """Test putting a static paired service."""
 
105
        service_name = "dummyfortest"
 
106
        oauth_data = {
 
107
                "consumer_key": str("abcdef"),
 
108
                "consumer_secret": str("ghighjklm"),
 
109
                "token": str("opqrst"),
 
110
                "token_secret": str("uvwxyz"),
 
111
            }
 
112
        couchdb_io.put_static_paired_service(
 
113
            oauth_data, service_name, uri=URI,
 
114
            ctx=test_environment.test_context)
 
115
        couchdb_io.get_pairings(ctx=test_environment.test_context)
 
116
        # Assert something?
 
117
 
 
118
    def test_put_dynamic_paired_host(self):
 
119
        """Test putting a dynamically paired host."""
 
120
        hostname = "host%d" % (os.getpid(),)
 
121
        remote_uuid = str(uuid.uuid4())
 
122
        oauth_data = {
 
123
                "consumer_key": str("abcdef"),
 
124
                "consumer_secret": str("ghighjklm"),
 
125
                "token": str("opqrst"),
 
126
                "token_secret": str("uvwxyz"),
 
127
            }
 
128
 
 
129
        couchdb_io.put_dynamic_paired_host(hostname, remote_uuid, oauth_data,
 
130
                uri=URI, ctx=test_environment.test_context)
 
131
        couchdb_io.put_dynamic_paired_host(hostname, remote_uuid, oauth_data,
 
132
                uri=URI, ctx=test_environment.test_context)
 
133
        couchdb_io.put_dynamic_paired_host(hostname, remote_uuid, oauth_data,
 
134
                uri=URI, ctx=test_environment.test_context)
 
135
 
 
136
        pairings = list(couchdb_io.get_pairings(
 
137
            ctx=test_environment.test_context))
 
138
        # 3, plus 1 from setUp()
 
139
        self.assertEqual(4, len(pairings))
 
140
        self.assertEqual(pairings[0].value["oauth"], oauth_data)
 
141
        self.assertEqual(pairings[0].value["server"], hostname)
 
142
        self.assertEqual(pairings[0].value["pairing_identifier"], remote_uuid)
 
143
 
 
144
        for i, row in enumerate(pairings):
 
145
            couchdb_io.remove_pairing(
 
146
                row.id, i == 1, ctx=test_environment.test_context)
 
147
 
 
148
        pairings = list(
 
149
            couchdb_io.get_pairings(ctx=test_environment.test_context))
 
150
        self.assertEqual(0, len(pairings))
 
151
 
 
152
    def test_get_database_names_replicatable(self):
 
153
        """Test get the names of replicatable databases."""
 
154
        names = couchdb_io.get_database_names_replicatable(
 
155
            uri=URI, ctx=test_environment.test_context)
 
156
        self.assertFalse('management' in names)
 
157
        self.assertTrue('foo' in names)
 
158
        self.assertFalse('bar' in names, names)  # is excluded
 
159
 
 
160
    def test_get_my_host_unique_id(self):
 
161
        """Test get the unique id of the host record."""
 
162
        got = couchdb_io.get_my_host_unique_id(
 
163
            uri=URI, ctx=test_environment.test_context)
 
164
        again = couchdb_io.get_my_host_unique_id(
 
165
            uri=URI, ctx=test_environment.test_context)
 
166
        self.assertEquals(len(got), 1)
 
167
        self.assertEquals(got, again)
 
168
 
 
169
    def test_mkuri(self):
 
170
        """Test creating a URI."""
 
171
        uri = couchdb_io.mkuri(
 
172
            'fnord.org', 55241, has_ssl=True, path='a/b/c',
 
173
            auth_pair=('f o o', 'b=a=r'))
 
174
        self.assertEquals(
 
175
            'https://f%20o%20o:b%3Da%3Dr@fnord.org:55241/a/b/c', uri)