|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (C) 2018 IBM Corp. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +_replicator_mock_tests_ |
| 18 | +
|
| 19 | +replicator module - Mock unit tests for the Replicator class |
| 20 | +""" |
| 21 | + |
| 22 | +import mock |
| 23 | +import unittest |
| 24 | + |
| 25 | +from cloudant.database import CouchDatabase |
| 26 | +from cloudant.replicator import Replicator |
| 27 | + |
| 28 | +from tests.unit.iam_auth_tests import MOCK_API_KEY |
| 29 | + |
| 30 | + |
| 31 | +class ReplicatorDocumentValidationMockTests(unittest.TestCase): |
| 32 | + """ |
| 33 | + Replicator document validation tests |
| 34 | + """ |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + self.repl_id = 'rep_test' |
| 38 | + |
| 39 | + self.server_url = 'http://localhost:5984' |
| 40 | + self.user_ctx = { |
| 41 | + 'name': 'foo', |
| 42 | + 'roles': ['erlanger', 'researcher'] |
| 43 | + } |
| 44 | + |
| 45 | + self.source_db = 'source_db' |
| 46 | + self.target_db = 'target_db' |
| 47 | + |
| 48 | + def setUpClientMocks(self, admin_party=False, iam_api_key=None): |
| 49 | + m_client = mock.MagicMock() |
| 50 | + type(m_client).server_url = mock.PropertyMock( |
| 51 | + return_value=self.server_url) |
| 52 | + |
| 53 | + type(m_client).admin_party = mock.PropertyMock( |
| 54 | + return_value=admin_party) |
| 55 | + |
| 56 | + iam_authenticated = False |
| 57 | + |
| 58 | + if iam_api_key is not None: |
| 59 | + iam_authenticated = True |
| 60 | + |
| 61 | + m_session = mock.MagicMock() |
| 62 | + type(m_session).get_api_key = mock.PropertyMock( |
| 63 | + return_value=iam_api_key) |
| 64 | + |
| 65 | + type(m_client).r_session = mock.PropertyMock( |
| 66 | + return_value=m_session) |
| 67 | + |
| 68 | + type(m_client).is_iam_authenticated = mock.PropertyMock( |
| 69 | + return_value=iam_authenticated) |
| 70 | + |
| 71 | + return m_client |
| 72 | + |
| 73 | + def test_using_admin_party_source_and_target(self): |
| 74 | + m_admin_party_client = self.setUpClientMocks(admin_party=True) |
| 75 | + |
| 76 | + m_replicator = mock.MagicMock() |
| 77 | + type(m_replicator).creds = mock.PropertyMock(return_value=None) |
| 78 | + m_admin_party_client.__getitem__.return_value = m_replicator |
| 79 | + |
| 80 | + # create source/target databases |
| 81 | + src = CouchDatabase(m_admin_party_client, self.source_db) |
| 82 | + tgt = CouchDatabase(m_admin_party_client, self.target_db) |
| 83 | + |
| 84 | + # trigger replication |
| 85 | + rep = Replicator(m_admin_party_client) |
| 86 | + rep.create_replication(src, tgt, repl_id=self.repl_id) |
| 87 | + |
| 88 | + kcall = m_replicator.create_document.call_args_list |
| 89 | + self.assertEquals(len(kcall), 1) |
| 90 | + args, kwargs = kcall[0] |
| 91 | + self.assertEquals(len(args), 1) |
| 92 | + |
| 93 | + expected_doc = { |
| 94 | + '_id': self.repl_id, |
| 95 | + 'source': {'url': '/'.join((self.server_url, self.source_db))}, |
| 96 | + 'target': {'url': '/'.join((self.server_url, self.target_db))} |
| 97 | + } |
| 98 | + |
| 99 | + self.assertDictEqual(args[0], expected_doc) |
| 100 | + self.assertTrue(kwargs['throw_on_exists']) |
| 101 | + |
| 102 | + def test_using_basic_auth_source_and_target(self): |
| 103 | + test_basic_auth_header = 'abc' |
| 104 | + |
| 105 | + m_basic_auth_client = self.setUpClientMocks() |
| 106 | + |
| 107 | + m_replicator = mock.MagicMock() |
| 108 | + m_basic_auth_client.__getitem__.return_value = m_replicator |
| 109 | + m_basic_auth_client.basic_auth_str.return_value = test_basic_auth_header |
| 110 | + |
| 111 | + # create source/target databases |
| 112 | + src = CouchDatabase(m_basic_auth_client, self.source_db) |
| 113 | + tgt = CouchDatabase(m_basic_auth_client, self.target_db) |
| 114 | + |
| 115 | + # trigger replication |
| 116 | + rep = Replicator(m_basic_auth_client) |
| 117 | + rep.create_replication( |
| 118 | + src, tgt, repl_id=self.repl_id, user_ctx=self.user_ctx) |
| 119 | + |
| 120 | + kcall = m_replicator.create_document.call_args_list |
| 121 | + self.assertEquals(len(kcall), 1) |
| 122 | + args, kwargs = kcall[0] |
| 123 | + self.assertEquals(len(args), 1) |
| 124 | + |
| 125 | + expected_doc = { |
| 126 | + '_id': self.repl_id, |
| 127 | + 'user_ctx': self.user_ctx, |
| 128 | + 'source': { |
| 129 | + 'headers': {'Authorization': test_basic_auth_header}, |
| 130 | + 'url': '/'.join((self.server_url, self.source_db)) |
| 131 | + }, |
| 132 | + 'target': { |
| 133 | + 'headers': {'Authorization': test_basic_auth_header}, |
| 134 | + 'url': '/'.join((self.server_url, self.target_db)) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + self.assertDictEqual(args[0], expected_doc) |
| 139 | + self.assertTrue(kwargs['throw_on_exists']) |
| 140 | + |
| 141 | + def test_using_iam_auth_source_and_target(self): |
| 142 | + m_iam_auth_client = self.setUpClientMocks(iam_api_key=MOCK_API_KEY) |
| 143 | + |
| 144 | + m_replicator = mock.MagicMock() |
| 145 | + m_iam_auth_client.__getitem__.return_value = m_replicator |
| 146 | + |
| 147 | + # create source/target databases |
| 148 | + src = CouchDatabase(m_iam_auth_client, self.source_db) |
| 149 | + tgt = CouchDatabase(m_iam_auth_client, self.target_db) |
| 150 | + |
| 151 | + # trigger replication |
| 152 | + rep = Replicator(m_iam_auth_client) |
| 153 | + rep.create_replication( |
| 154 | + src, tgt, repl_id=self.repl_id, user_ctx=self.user_ctx) |
| 155 | + |
| 156 | + kcall = m_replicator.create_document.call_args_list |
| 157 | + self.assertEquals(len(kcall), 1) |
| 158 | + args, kwargs = kcall[0] |
| 159 | + self.assertEquals(len(args), 1) |
| 160 | + |
| 161 | + expected_doc = { |
| 162 | + '_id': self.repl_id, |
| 163 | + 'user_ctx': self.user_ctx, |
| 164 | + 'source': { |
| 165 | + 'auth': {'iam': {'api_key': MOCK_API_KEY}}, |
| 166 | + 'url': '/'.join((self.server_url, self.source_db)) |
| 167 | + }, |
| 168 | + 'target': { |
| 169 | + 'auth': {'iam': {'api_key': MOCK_API_KEY}}, |
| 170 | + 'url': '/'.join((self.server_url, self.target_db)) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + self.assertDictEqual(args[0], expected_doc) |
| 175 | + self.assertTrue(kwargs['throw_on_exists']) |
0 commit comments