-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtest_rest_deploy.py
More file actions
271 lines (233 loc) · 10.3 KB
/
test_rest_deploy.py
File metadata and controls
271 lines (233 loc) · 10.3 KB
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
import base64
import io
import unittest
import zipfile
from unittest.mock import MagicMock, Mock, call, patch
from cumulusci.salesforce_api.rest_deploy import RestDeploy
from cumulusci.tests.util import CURRENT_SF_API_VERSION
def generate_sample_zip_data(parent=""):
# Create a sample ZIP with two files
zip_data = io.BytesIO()
with zipfile.ZipFile(zip_data, "w") as zip_file:
zip_file.writestr(
f"{parent}objects/mockfile1.obj", "Sample content for mockfile1"
)
zip_file.writestr(
f"{parent}objects/mockfile2.obj", "Sample content for mockfile2"
)
return base64.b64encode(zip_data.getvalue()).decode("utf-8")
class TestRestDeploy(unittest.TestCase):
# Setup method executed before each test method
def setUp(self):
self.mock_logger = Mock()
self.mock_task = MagicMock()
self.mock_task.logger = self.mock_logger
self.mock_task.org_config.instance_url = "https://example.com"
self.mock_task.org_config.access_token = "dummy_token"
self.mock_task.project_config.project__package__api_version = (
CURRENT_SF_API_VERSION
)
# Empty zip file for testing
self.mock_zip = generate_sample_zip_data()
# Test case for a successful deployment and deploy status
@patch("requests.post")
@patch("requests.get")
def test_deployment_success(self, mock_get, mock_post):
response_post = Mock(status_code=201)
response_post.json.return_value = {"id": "dummy_id"}
mock_post.return_value = response_post
response_get = Mock(status_code=200)
response_get.json.side_effect = [
{"deployResult": {"status": "InProgress"}},
{"deployResult": {"status": "Succeeded"}},
]
mock_get.return_value = response_get
deployer = RestDeploy(
self.mock_task, self.mock_zip, False, False, "NoTestRun", []
)
deployer()
# Assertions to verify log messages
assert (
call("Deployment request successful")
in self.mock_logger.info.call_args_list
)
assert call("Deployment InProgress") in self.mock_logger.info.call_args_list
assert call("Deployment Succeeded") in self.mock_logger.info.call_args_list
# Assertions to verify API Calls
expected_get_calls = [
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
]
mock_post.assert_called_once()
mock_get.assert_has_calls(expected_get_calls, any_order=True)
# Test case for a deployment failure
@patch("requests.post")
def test_deployment_failure(self, mock_post):
response_post = Mock(status_code=500)
response_post.json.return_value = {"id": "dummy_id"}
mock_post.return_value = response_post
deployer = RestDeploy(
self.mock_task, self.mock_zip, False, False, "NoTestRun", []
)
deployer()
# Assertions to verify log messages
assert (
call("Deployment request failed with status code 500")
in self.mock_logger.error.call_args_list
)
# Assertions to verify API Calls
mock_post.assert_called_once()
# Test for deployment success but deploy status failure
@patch("requests.post")
@patch("requests.get")
def test_deployStatus_failure(self, mock_get, mock_post):
response_post = Mock(status_code=201)
response_post.json.return_value = {"id": "dummy_id"}
mock_post.return_value = response_post
response_get = Mock(status_code=200)
response_get.json.side_effect = [
{"deployResult": {"status": "InProgress"}},
{
"deployResult": {
"status": "Failed",
"details": {
"componentFailures": [
{
"problemType": "Error",
"fileName": "metadata/classes/mockfile1.cls",
"problem": "someproblem1",
"lineNumber": 1,
"columnNumber": 1,
},
{
"problemType": "Error",
"fileName": "metadata/objects/mockfile2.obj",
"problem": "someproblem2",
"lineNumber": 2,
"columnNumber": 2,
},
]
},
}
},
]
mock_get.return_value = response_get
deployer = RestDeploy(
self.mock_task, self.mock_zip, False, False, "NoTestRun", []
)
deployer()
# Assertions to verify log messages
assert (
call("Deployment request successful")
in self.mock_logger.info.call_args_list
)
assert call("Deployment InProgress") in self.mock_logger.info.call_args_list
assert call("Deployment Failed") in self.mock_logger.info.call_args_list
assert (
call("ERROR in file classes/mockfile1.cls: someproblem1 at line 1:1")
in self.mock_logger.error.call_args_list
)
assert (
call("ERROR in file objects/mockfile2.obj: someproblem2 at line 2:2")
in self.mock_logger.error.call_args_list
)
# Assertions to verify API Calls
expected_get_calls = [
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
]
mock_post.assert_called_once()
mock_get.assert_has_calls(expected_get_calls, any_order=True)
# Test case for a deployment with a pending status
@patch("requests.post")
@patch("requests.get")
def test_pending_call(self, mock_get, mock_post):
response_post = Mock(status_code=201)
response_post.json.return_value = {"id": "dummy_id"}
mock_post.return_value = response_post
response_get = Mock(status_code=200)
response_get.json.side_effect = [
{"deployResult": {"status": "InProgress"}},
{"deployResult": {"status": "Pending"}},
{"deployResult": {"status": "Succeeded"}},
]
mock_get.return_value = response_get
deployer = RestDeploy(
self.mock_task, self.mock_zip, False, False, "NoTestRun", []
)
deployer()
# Assertions to verify log messages
assert (
call("Deployment request successful")
in self.mock_logger.info.call_args_list
)
assert call("Deployment InProgress") in self.mock_logger.info.call_args_list
assert call("Deployment Pending") in self.mock_logger.info.call_args_list
assert call("Deployment Succeeded") in self.mock_logger.info.call_args_list
# Assertions to verify API Calls
expected_get_calls = [
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
call(
f"https://example.com/services/data/v{CURRENT_SF_API_VERSION}/metadata/deployRequest/dummy_id?includeDetails=true",
headers={"Authorization": "Bearer dummy_token"},
),
]
mock_post.assert_called_once()
mock_get.assert_has_calls(expected_get_calls, any_order=True)
def test_reformat_zip(self):
input_zip = generate_sample_zip_data()
expected_zip = generate_sample_zip_data("metadata/")
deployer = RestDeploy(
self.mock_task, self.mock_zip, False, False, "NoTestRun", []
)
actual_output_zip = deployer._reformat_zip(input_zip)
# ZIP container metadata (for example file timestamps) can differ between
# platforms even when file names and contents are identical.
expected_bytes = base64.b64decode(expected_zip)
with zipfile.ZipFile(io.BytesIO(actual_output_zip), "r") as actual_zip:
with zipfile.ZipFile(io.BytesIO(expected_bytes), "r") as expected_zip_file:
self.assertEqual(actual_zip.namelist(), expected_zip_file.namelist())
for name in expected_zip_file.namelist():
self.assertEqual(
actual_zip.read(name), expected_zip_file.read(name)
)
def test_purge_on_delete(self):
test_data = [
("not_sandbox_developer", "Not Developer Edition", False, False, "false"),
("purgeOnDelete_true", "Developer Edition", True, True, "true"),
("purgeOnDelete_none", "Developer Edition", True, None, "true"),
]
for name, org_type, is_sandbox, purge_on_delete, expected_result in test_data:
with self.subTest(name=name):
self.mock_task.org_config.org_type = org_type
self.mock_task.org_config.is_sandbox = is_sandbox
deployer = RestDeploy(
self.mock_task,
self.mock_zip,
purge_on_delete,
False,
"NoTestRun",
[],
)
self.assertEqual(deployer.purge_on_delete, expected_result)
if __name__ == "__main__":
unittest.main()