summaryrefslogtreecommitdiff
path: root/testing/tests/couch/test_ddocs.py
blob: 9ff326337b531644aa0dec2d0677bef5fc95be1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from uuid import uuid4

from leap.soledad.common.couch import errors
from leap.soledad.common import couch

from test_soledad.util import CouchDBTestCase


class CouchDesignDocsTests(CouchDBTestCase):

    def setUp(self):
        CouchDBTestCase.setUp(self)

    def create_db(self, ensure=True, dbname=None):
        if not dbname:
            dbname = ('test-%s' % uuid4().hex)
        if dbname not in self.couch_server:
            self.couch_server.create(dbname)
        self.db = couch.CouchDatabase(
            ('http://127.0.0.1:%d' % self.couch_port),
            dbname,
            ensure_ddocs=ensure)

    def tearDown(self):
        self.db.delete_database()
        self.db.close()
        CouchDBTestCase.tearDown(self)

    def test_missing_design_doc_raises(self):
        """
        Test that all methods that access design documents will raise if the
        design docs are not present.
        """
        self.create_db(ensure=False)
        # get_generation_info()
        self.assertRaises(
            errors.MissingDesignDocError,
            self.db.get_generation_info)
        # get_trans_id_for_gen()
        self.assertRaises(
            errors.MissingDesignDocError,
            self.db.get_trans_id_for_gen, 1)
        # get_transaction_log()
        self.assertRaises(
            errors.MissingDesignDocError,
            self.db.get_transaction_log)
        # whats_changed()
        self.assertRaises(
            errors.MissingDesignDocError,
            self.db.whats_changed)

    def test_missing_design_doc_functions_raises(self):
        """
        Test that all methods that access design documents list functions
        will raise if the functions are not present.
        """
        self.create_db(ensure=True)
        # erase views from _design/transactions
        transactions = self.db._database['_design/transactions']
        transactions['lists'] = {}
        self.db._database.save(transactions)
        # get_generation_info()
        self.assertRaises(
            errors.MissingDesignDocListFunctionError,
            self.db.get_generation_info)
        # get_trans_id_for_gen()
        self.assertRaises(
            errors.MissingDesignDocListFunctionError,
            self.db.get_trans_id_for_gen, 1)
        # whats_changed()
        self.assertRaises(
            errors.MissingDesignDocListFunctionError,
            self.db.whats_changed)

    def test_absent_design_doc_functions_raises(self):
        """
        Test that all methods that access design documents list functions
        will raise if the functions are not present.
        """
        self.create_db(ensure=True)
        # erase views from _design/transactions
        transactions = self.db._database['_design/transactions']
        del transactions['lists']
        self.db._database.save(transactions)
        # get_generation_info()
        self.assertRaises(
            errors.MissingDesignDocListFunctionError,
            self.db.get_generation_info)
        # _get_trans_id_for_gen()
        self.assertRaises(
            errors.MissingDesignDocListFunctionError,
            self.db.get_trans_id_for_gen, 1)
        # whats_changed()
        self.assertRaises(
            errors.MissingDesignDocListFunctionError,
            self.db.whats_changed)

    def test_missing_design_doc_named_views_raises(self):
        """
        Test that all methods that access design documents' named views  will
        raise if the views are not present.
        """
        self.create_db(ensure=True)
        # erase views from _design/docs
        docs = self.db._database['_design/docs']
        del docs['views']
        self.db._database.save(docs)
        # erase views from _design/syncs
        syncs = self.db._database['_design/syncs']
        del syncs['views']
        self.db._database.save(syncs)
        # erase views from _design/transactions
        transactions = self.db._database['_design/transactions']
        del transactions['views']
        self.db._database.save(transactions)
        # get_generation_info()
        self.assertRaises(
            errors.MissingDesignDocNamedViewError,
            self.db.get_generation_info)
        # _get_trans_id_for_gen()
        self.assertRaises(
            errors.MissingDesignDocNamedViewError,
            self.db.get_trans_id_for_gen, 1)
        # _get_transaction_log()
        self.assertRaises(
            errors.MissingDesignDocNamedViewError,
            self.db.get_transaction_log)
        # whats_changed()
        self.assertRaises(
            errors.MissingDesignDocNamedViewError,
            self.db.whats_changed)

    def test_deleted_design_doc_raises(self):
        """
        Test that all methods that access design documents will raise if the
        design docs are not present.
        """
        self.create_db(ensure=True)
        # delete _design/docs
        del self.db._database['_design/docs']
        # delete _design/syncs
        del self.db._database['_design/syncs']
        # delete _design/transactions
        del self.db._database['_design/transactions']
        # get_generation_info()
        self.assertRaises(
            errors.MissingDesignDocDeletedError,
            self.db.get_generation_info)
        # get_trans_id_for_gen()
        self.assertRaises(
            errors.MissingDesignDocDeletedError,
            self.db.get_trans_id_for_gen, 1)
        # get_transaction_log()
        self.assertRaises(
            errors.MissingDesignDocDeletedError,
            self.db.get_transaction_log)
        # whats_changed()
        self.assertRaises(
            errors.MissingDesignDocDeletedError,
            self.db.whats_changed)

    def test_ensure_ddoc_independently(self):
        """
        Test that a missing ddocs other than _design/docs will be ensured
        even if _design/docs is there.
        """
        self.create_db(ensure=True)
        del self.db._database['_design/transactions']
        self.assertRaises(
            errors.MissingDesignDocDeletedError,
            self.db.get_transaction_log)
        self.create_db(ensure=True, dbname=self.db._dbname)
        self.db.get_transaction_log()

    def test_ensure_security_doc(self):
        """
        Ensure_security creates a _security ddoc to ensure that only soledad
        will have the lowest privileged access to an user db.
        """
        self.create_db(ensure=False)
        self.assertFalse(self.db._database.resource.get_json('_security')[2])
        self.db.ensure_security_ddoc()
        security_ddoc = self.db._database.resource.get_json('_security')[2]
        self.assertIn('admins', security_ddoc)
        self.assertFalse(security_ddoc['admins']['names'])
        self.assertIn('members', security_ddoc)
        self.assertIn('soledad', security_ddoc['members']['names'])

    def test_ensure_security_from_configuration(self):
        """
        Given a configuration, follow it to create the security document
        """
        self.create_db(ensure=False)
        configuration = {'members': ['user1', 'user2'],
                         'members_roles': ['role1', 'role2'],
                         'admins': ['admin'],
                         'admins_roles': ['administrators']
                         }
        self.db.ensure_security_ddoc(configuration)

        security_ddoc = self.db._database.resource.get_json('_security')[2]
        self.assertEquals(configuration['admins'],
                          security_ddoc['admins']['names'])
        self.assertEquals(configuration['admins_roles'],
                          security_ddoc['admins']['roles'])
        self.assertEquals(configuration['members'],
                          security_ddoc['members']['names'])
        self.assertEquals(configuration['members_roles'],
                          security_ddoc['members']['roles'])