This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathreplicator_mock_tests.py
175 lines (135 loc) · 5.79 KB
/
replicator_mock_tests.py
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
#!/usr/bin/env python
# Copyright (C) 2018 IBM Corp. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
_replicator_mock_tests_
replicator module - Mock unit tests for the Replicator class
"""
import mock
import unittest
from cloudant.database import CouchDatabase
from cloudant.replicator import Replicator
from tests.unit.iam_auth_tests import MOCK_API_KEY
class ReplicatorDocumentValidationMockTests(unittest.TestCase):
"""
Replicator document validation tests
"""
def setUp(self):
self.repl_id = 'rep_test'
self.server_url = 'http://localhost:5984'
self.user_ctx = {
'name': 'foo',
'roles': ['erlanger', 'researcher']
}
self.source_db = 'source_db'
self.target_db = 'target_db'
def setUpClientMocks(self, admin_party=False, iam_api_key=None):
m_client = mock.MagicMock()
type(m_client).server_url = mock.PropertyMock(
return_value=self.server_url)
type(m_client).admin_party = mock.PropertyMock(
return_value=admin_party)
iam_authenticated = False
if iam_api_key is not None:
iam_authenticated = True
m_session = mock.MagicMock()
type(m_session).get_api_key = mock.PropertyMock(
return_value=iam_api_key)
type(m_client).r_session = mock.PropertyMock(
return_value=m_session)
type(m_client).is_iam_authenticated = mock.PropertyMock(
return_value=iam_authenticated)
return m_client
def test_using_admin_party_source_and_target(self):
m_admin_party_client = self.setUpClientMocks(admin_party=True)
m_replicator = mock.MagicMock()
type(m_replicator).creds = mock.PropertyMock(return_value=None)
m_admin_party_client.__getitem__.return_value = m_replicator
# create source/target databases
src = CouchDatabase(m_admin_party_client, self.source_db)
tgt = CouchDatabase(m_admin_party_client, self.target_db)
# trigger replication
rep = Replicator(m_admin_party_client)
rep.create_replication(src, tgt, repl_id=self.repl_id)
kcall = m_replicator.create_document.call_args_list
self.assertEqual(len(kcall), 1)
args, kwargs = kcall[0]
self.assertEqual(len(args), 1)
expected_doc = {
'_id': self.repl_id,
'source': {'url': '/'.join((self.server_url, self.source_db))},
'target': {'url': '/'.join((self.server_url, self.target_db))}
}
self.assertDictEqual(args[0], expected_doc)
self.assertTrue(kwargs['throw_on_exists'])
def test_using_basic_auth_source_and_target(self):
test_basic_auth_header = 'abc'
m_basic_auth_client = self.setUpClientMocks()
m_replicator = mock.MagicMock()
m_basic_auth_client.__getitem__.return_value = m_replicator
m_basic_auth_client.basic_auth_str.return_value = test_basic_auth_header
# create source/target databases
src = CouchDatabase(m_basic_auth_client, self.source_db)
tgt = CouchDatabase(m_basic_auth_client, self.target_db)
# trigger replication
rep = Replicator(m_basic_auth_client)
rep.create_replication(
src, tgt, repl_id=self.repl_id, user_ctx=self.user_ctx)
kcall = m_replicator.create_document.call_args_list
self.assertEqual(len(kcall), 1)
args, kwargs = kcall[0]
self.assertEqual(len(args), 1)
expected_doc = {
'_id': self.repl_id,
'user_ctx': self.user_ctx,
'source': {
'headers': {'Authorization': test_basic_auth_header},
'url': '/'.join((self.server_url, self.source_db))
},
'target': {
'headers': {'Authorization': test_basic_auth_header},
'url': '/'.join((self.server_url, self.target_db))
}
}
self.assertDictEqual(args[0], expected_doc)
self.assertTrue(kwargs['throw_on_exists'])
def test_using_iam_auth_source_and_target(self):
m_iam_auth_client = self.setUpClientMocks(iam_api_key=MOCK_API_KEY)
m_replicator = mock.MagicMock()
m_iam_auth_client.__getitem__.return_value = m_replicator
# create source/target databases
src = CouchDatabase(m_iam_auth_client, self.source_db)
tgt = CouchDatabase(m_iam_auth_client, self.target_db)
# trigger replication
rep = Replicator(m_iam_auth_client)
rep.create_replication(
src, tgt, repl_id=self.repl_id, user_ctx=self.user_ctx)
kcall = m_replicator.create_document.call_args_list
self.assertEqual(len(kcall), 1)
args, kwargs = kcall[0]
self.assertEqual(len(args), 1)
expected_doc = {
'_id': self.repl_id,
'user_ctx': self.user_ctx,
'source': {
'auth': {'iam': {'api_key': MOCK_API_KEY}},
'url': '/'.join((self.server_url, self.source_db))
},
'target': {
'auth': {'iam': {'api_key': MOCK_API_KEY}},
'url': '/'.join((self.server_url, self.target_db))
}
}
self.assertDictEqual(args[0], expected_doc)
self.assertTrue(kwargs['throw_on_exists'])