-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfluence_unit_test.py
280 lines (203 loc) · 12.3 KB
/
confluence_unit_test.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from requests.exceptions import SSLError
import json
import unittest
import unittest.mock as mock
from parameterized import parameterized
import cf_uploader.errors as errors
import cf_uploader.confluence as confluence
class ConfluenceTest(unittest.TestCase):
def setUp(self):
self._config = mock.Mock()
self._confluence = confluence.ConfluenceAPI(self._config)
# Set default config values
self._config.get_api_interaction_delay_seconds.return_value = 0
self._config.get_base_url.return_value = "https://test.com/rest/api/content/"
self._config.get_template_id.return_value = "123456"
self._config.get_username.return_value = "username"
self._config.get_token.return_value = "password"
self._config.get_upload_parent_page.return_value = "31415"
self._config.get_upload_space.return_value = "AUTOSITE"
@mock.patch("requests.get")
def test_retrieve_template_error_returned_from_rest(self, requests_get_mock):
response_mock = mock.Mock()
response_mock.status_code = 403
response_mock.content = bytearray("Error message test", "utf-8")
requests_get_mock.return_value = response_mock
with self.assertRaises(RuntimeError) as cm:
self._confluence.retrieve_template()
self.assertEqual(errors.CONFLUENCE_DOWNLOAD_FAILED,
cm.exception.args[0])
self.assertEqual("123456",
cm.exception.args[2])
@mock.patch("requests.get")
def test_retrieve_template_ssl_error(self, requests_get_mock):
requests_get_mock.side_effect = SSLError("SSL Error Test")
with self.assertRaises(RuntimeError) as cm:
self._confluence.retrieve_template()
self.assertEqual(errors.SSL_ERROR, cm.exception.args[0])
@mock.patch("requests.get")
def test_retrieve_template_key_error_upon_processing(self, requests_get_mock):
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.json.return_value = {"title": "Title"}
requests_get_mock.return_value = response_mock
with self.assertRaises(RuntimeError) as cm:
self._confluence.retrieve_template()
self.assertEqual(errors.CONFLUENCE_TEMPLATE_PROCESSING_FAILED,
cm.exception.args[0])
self.assertEqual("123456",
cm.exception.args[1])
@mock.patch("requests.get")
@mock.patch("time.sleep")
def test_retrieve_template(self, time_sleep_mock, requests_get_mock):
self._config.get_api_interaction_delay_seconds.return_value = 1.43
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.json.return_value = {"type": "page", "trim": "remove", "title": "Title", "space": {"key": "AUTOSITE", "trim": "val"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage", "trim": "s"}, "trim": "2"}}
requests_get_mock.return_value = response_mock
# Test that the unnecessary properties are trimmed
processed_template = {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "ancestors": [{"id": "31415"}]}
self.assertEqual(processed_template,
self._confluence.retrieve_template())
# Test that the pause delay is taken into account
time_sleep_mock.assert_called_with(1.43)
@mock.patch("requests.get")
def test_retrieve_template_no_parent_page(self, requests_get_mock):
self._config.get_upload_parent_page.return_value = None
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.json.return_value = {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}}
requests_get_mock.return_value = response_mock
self.assertEqual(response_mock.json.return_value,
self._confluence.retrieve_template())
def test_exists_none_page_title(self):
with self.assertRaises(TypeError):
self._confluence.exists(None)
@mock.patch("requests.get")
@mock.patch("cf_uploader.errors.CONFLUENCE_EXISTENCE_CHECK_FAILED.print_message")
def test_exists_error_returned_from_rest(self, error_print_msg_mock, requests_get_mock):
response_mock = mock.Mock()
response_mock.status_code = 403
response_mock.content = bytearray("Error message test", "utf-8")
requests_get_mock.return_value = response_mock
self.assertIsNone(self._confluence.exists("article"))
# Check that the error message is logged
error_print_msg_mock.assert_called()
@parameterized.expand([({},), ({"arg": "1"},), ({"size": "df"},), ({"size": "4"},), ({"size": "4", "results": {"id": "1"}},),
({"size": "4", "results": []},), ({"size": "4", "results": [
{"value": "value"}]},), ({"size": "4", "results": [{"id": "4"}]},),
({"size": "4", "results": [
{"id": "4", "version": {"arg": "3"}}]},),
({"size": "4", "results": [{"id": "4", "version": {"number": "df"}}]},)])
@mock.patch("requests.get")
def test_exists_invalid_responses(self, invalid_response_json, requests_get_mock):
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.content = bytearray("Success", "utf-8")
response_mock.json.return_value = invalid_response_json
requests_get_mock.return_value = response_mock
self.assertIsNone(self._confluence.exists("article"))
@mock.patch("requests.get")
def test_exists_not_existing(self, requests_get_mock):
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.content = bytearray("Success", "utf-8")
response_mock.json.return_value = {"size": "0"}
requests_get_mock.return_value = response_mock
self.assertIsNone(self._confluence.exists("article"))
@mock.patch("requests.get")
def test_exists_existing(self, requests_get_mock):
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.content = bytearray("Success", "utf-8")
response_mock.json.return_value = {"size": "1", "results": [
{"id": "314", "version": {"number": "12"}}]}
requests_get_mock.return_value = response_mock
self.assertEqual(("314", 12), self._confluence.exists("article"))
def test_upload_article_none_id(self):
with self.assertRaises(TypeError):
self._confluence.upload_article(None, {})
def test_upload_article_none_json(self):
with self.assertRaises(TypeError):
self._confluence.upload_article("3", None)
@ mock.patch("requests.post")
@ mock.patch("cf_uploader.errors.CONFLUENCE_UPLOAD_FAILED.print_message")
def test_upload_article_no_existing_override_error_returned_from_rest(self, error_print_msg_mock, requests_post_mock):
self._config.get_overwrite_existing_articles.return_value = False
response_mock = mock.Mock()
response_mock.status_code = 403
response_mock.content = bytearray("Error message test", "utf-8")
requests_post_mock.return_value = response_mock
self.assertFalse(self._confluence.upload_article("article", {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "ancestors": [{"id": "31415"}]}))
# Check that the error message is logged
error_print_msg_mock.assert_called()
@ mock.patch("requests.post")
def test_upload_article_no_existing_override_ssl_error(self, requests_post_mock):
self._config.get_overwrite_existing_articles.return_value = False
requests_post_mock.side_effect = SSLError("SSL Error Test")
with self.assertRaises(RuntimeError) as cm:
self._confluence.upload_article("article", {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "ancestors": [{"id": "31415"}]})
self.assertEqual(errors.SSL_ERROR, cm.exception.args[0])
@ mock.patch("requests.post")
@ mock.patch("cf_uploader.logger.main_logger.info")
def test_upload_article_no_existing_override(self, success_print_msg_mock, requests_post_mock):
self._config.get_overwrite_existing_articles.return_value = False
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.content = bytearray("Success message", "utf-8")
requests_post_mock.return_value = response_mock
self.assertTrue(self._confluence.upload_article("article", {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "ancestors": [{"id": "31415"}]}))
# Check that the success message is logged
success_print_msg_mock.assert_called()
@ mock.patch("requests.put")
@ mock.patch("cf_uploader.errors.CONFLUENCE_UPDATE_FAILED.print_message")
def test_upload_article_existing_override_error_returned_from_rest(self, error_print_msg_mock, requests_post_mock):
self._config.get_overwrite_existing_articles.return_value = True
self._confluence.exists = mock.Mock(return_value=("123454", 23))
response_mock = mock.Mock()
response_mock.status_code = 403
response_mock.content = bytearray("Error message test", "utf-8")
requests_post_mock.return_value = response_mock
self.assertFalse(self._confluence.upload_article("article", {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "ancestors": [{"id": "31415"}]}))
# Check that the error message is logged
error_print_msg_mock.assert_called()
@ mock.patch("requests.put")
def test_upload_article_existing_override_ssl_error(self, requests_post_mock):
self._config.get_overwrite_existing_articles.return_value = True
self._confluence.exists = mock.Mock(return_value=("123454", 23))
requests_post_mock.side_effect = SSLError("SSL Error Test")
with self.assertRaises(RuntimeError) as cm:
self._confluence.upload_article("article", {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "ancestors": [{"id": "31415"}]})
self.assertEqual(errors.SSL_ERROR, cm.exception.args[0])
@ mock.patch("requests.put")
@ mock.patch("cf_uploader.logger.main_logger.info")
@ mock.patch("time.sleep")
def test_upload_article_existing_override(self, time_sleep_mock, info_print_msg_mock, requests_put_mock):
self._config.get_api_interaction_delay_seconds.return_value = 1.43
self._config.get_overwrite_existing_articles.return_value = True
self._confluence.exists = mock.Mock(return_value=("123454", 23))
response_mock = mock.Mock()
response_mock.status_code = 200
response_mock.content = bytearray("Success", "utf-8")
requests_put_mock.return_value = response_mock
self.assertTrue(self._confluence.upload_article("article", {"type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}}))
# Check that a success message is logged
info_print_msg_mock.assert_called()
# The JSON to send when an existing article should be replaced
update_article_data = {"id": "123454", "type": "page", "title": "Title", "space": {"key": "AUTOSITE"}, "body": {
"storage": {"value": "CONTENT", "representation": "storage"}}, "version": {"number": "24"}}
requests_put_mock.assert_called()
# Check that the JSON put into requests.put(data=...) is the same as update_article_data
self.assertEqual(update_article_data, json.loads(
requests_put_mock.call_args.kwargs["data"]))
# Test that the pause delay is taken into account
time_sleep_mock.assert_called_with(1.43)