Skip to content

Commit 7bc8578

Browse files
committed
chore: Add documentation on tests and add some assert on cwpp execute method
1 parent def4390 commit 7bc8578

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

tests/test_cwpp_cwpp.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Unit test for PrismaCloudAPICWPPMixin class
2+
"""
13
import unittest
24
import json
35

@@ -8,9 +10,12 @@
810

911

1012
class TestCasePrismaCloudAPICWPPMixin(unittest.TestCase):
11-
13+
"""Unit test on execute_compute method
14+
"""
1215
@responses.activate
1316
def setUp(self):
17+
"""Setup the login and meta_info route to get a mock PrimaCloudAPI object used on test
18+
"""
1419
responses.post(
1520
"https://example.prismacloud.io/login",
1621
body=json.dumps({"token": "token"}),
@@ -26,6 +31,8 @@ def setUp(self):
2631

2732
@responses.activate
2833
def test_execute_compute_nominal_for_credentials_list(self):
34+
"""Nominal test on the mock credentials list route
35+
"""
2936
get_creds = responses.get(
3037
"https://example.prismacloud.io/api/v1/credentials",
3138
body=json.dumps(CREDENTIALS),
@@ -39,6 +46,9 @@ def test_execute_compute_nominal_for_credentials_list(self):
3946

4047
@responses.activate
4148
def test_execute_compute_without_token_for_credentials_list(self):
49+
"""Nominal test on the mock credentials list route with missing Prisma token
50+
On this case, we check the login phase
51+
"""
4252
self.pc_api.token = None
4353
responses.post(
4454
"https://example.prismacloud.io/login",
@@ -60,6 +70,9 @@ def test_execute_compute_without_token_for_credentials_list(self):
6070

6171
@responses.activate
6272
def test_execute_compute_with_an_expire_token_for_credentials_list(self):
73+
"""Nominal test on the mock credentials list route with an expired Prisma token
74+
On this case, we check the re-login phase
75+
"""
6376
self.pc_api.token_timer = 0.0
6477
responses.post(
6578
"https://example.prismacloud.io/login",
@@ -81,6 +94,9 @@ def test_execute_compute_with_an_expire_token_for_credentials_list(self):
8194

8295
@responses.activate(registry=registries.OrderedRegistry)
8396
def test_execute_compute_retry_for_credentials_list(self):
97+
"""Nominal test on the mock credentials list route with an 500 error
98+
On the first call we send a 500 error before getting it 200 to test the retry mecanisme
99+
"""
84100
get_creds_1 = responses.get(
85101
'https://example.prismacloud.io/api/v1/credentials',
86102
body=json.dumps({}),
@@ -103,6 +119,9 @@ def test_execute_compute_retry_for_credentials_list(self):
103119

104120
@responses.activate(registry=registries.OrderedRegistry)
105121
def test_execute_compute_retry_failed_for_credentials_list(self):
122+
"""Non expected test on the mock credentials list route with multiple 500 error
123+
We setup for all call a 500 error return and test if the right exception is raise
124+
"""
106125
self.pc_api.retry_number = 2
107126
get_creds_responses = [
108127
responses.get(
@@ -132,13 +151,16 @@ def test_execute_compute_retry_failed_for_credentials_list(self):
132151

133152
@responses.activate(registry=registries.OrderedRegistry)
134153
def test_execute_compute_nominal_for_hosts_list(self):
135-
responses.get(
154+
"""Norminal test on the mock hosts list route
155+
We expected to get 52 hosts
156+
"""
157+
get_host_1 = responses.get(
136158
"https://example.prismacloud.io/api/v1/hosts",
137159
body=json.dumps([json.dumps(ONE_HOST) for _ in range(0, 50)]),
138160
status=200,
139161
headers={"Total-Count": "52"}
140162
)
141-
responses.get(
163+
get_host_2 = responses.get(
142164
"https://example.prismacloud.io/api/v1/hosts",
143165
body=json.dumps([json.dumps(ONE_HOST) for _ in range(0, 2)]),
144166
status=200,
@@ -150,16 +172,22 @@ def test_execute_compute_nominal_for_hosts_list(self):
150172
paginated=True,
151173
)
152174
self.assertEqual(len(hosts), 52)
175+
self.assertEqual(get_host_1.call_count, 1)
176+
self.assertEqual(get_host_2.call_count, 1)
153177

154178
@responses.activate(registry=registries.OrderedRegistry)
155179
def test_execute_compute_failed_all_hosts_list_because_unexpected_status_code_using_force_parameter(self):
156-
responses.get(
180+
"""Non expected test on the mock hosts list route with a 404 HTTP error code
181+
The first batch of hosts is correctly send by the second batch have an expected error code
182+
Since the force flag is set, we expected a 50 hosts list and no raised exception
183+
"""
184+
get_host_1 = responses.get(
157185
"https://example.prismacloud.io/api/v1/hosts",
158186
body=json.dumps([json.dumps(ONE_HOST) for _ in range(0, 50)]),
159187
status=200,
160188
headers={"Total-Count": "52"}
161189
)
162-
responses.get(
190+
get_host_2 = responses.get(
163191
"https://example.prismacloud.io/api/v1/hosts",
164192
body=json.dumps([json.dumps(ONE_HOST) for _ in range(0, 2)]),
165193
status=404,
@@ -172,27 +200,35 @@ def test_execute_compute_failed_all_hosts_list_because_unexpected_status_code_us
172200
force=True
173201
)
174202
self.assertEqual(len(hosts), 50)
203+
self.assertEqual(get_host_1.call_count, 1)
204+
self.assertEqual(get_host_2.call_count, 1)
175205

176206
@responses.activate(registry=registries.OrderedRegistry)
177207
def test_execute_compute_failed_all_hosts_list_because_unexpected_status_code_without_force_parameter(self):
178-
responses.get(
208+
"""Non expected test on the mock hosts list route with a 404 HTTP error code
209+
The first batch of hosts is correctly send by the second batch have an expected error code
210+
We must have a raised exception and no host response
211+
"""
212+
get_host_1 = responses.get(
179213
"https://example.prismacloud.io/api/v1/hosts",
180214
body=json.dumps([json.dumps(ONE_HOST) for _ in range(0, 50)]),
181215
status=200,
182216
headers={"Total-Count": "52"}
183217
)
184-
responses.get(
218+
get_host_2 = responses.get(
185219
"https://example.prismacloud.io/api/v1/hosts",
186220
body=json.dumps({}),
187221
status=404,
188222
headers={"Total-Count": "52"}
189223
)
190224
with self.assertRaises(SystemExit) as context:
191-
hosts = self.pc_api.execute_compute(
225+
self.pc_api.execute_compute(
192226
'GET',
193227
'api/v1/hosts',
194228
paginated=True
195229
)
230+
self.assertEqual(get_host_1.call_count, 1)
231+
self.assertEqual(get_host_2.call_count, 1)
196232
self.assertEqual(str(context.exception), """
197233
198234
Status Code: 404
@@ -203,7 +239,10 @@ def test_execute_compute_failed_all_hosts_list_because_unexpected_status_code_wi
203239

204240
@responses.activate(registry=registries.OrderedRegistry)
205241
def test_excute_compute_nominal_for_tag_update(self):
206-
responses.put(
242+
"""Nominal test on the mock tags put
243+
We expect no response
244+
"""
245+
put_tag = responses.put(
207246
"https://example.prismacloud.io/api/v1/tags/my_tag",
208247
status=200,
209248
match=[
@@ -224,9 +263,13 @@ def test_excute_compute_nominal_for_tag_update(self):
224263
},
225264
)
226265
self.assertIsNone(response)
266+
self.assertEqual(put_tag.call_count, 1)
227267

228268
@responses.activate
229269
def test_execute_compute_failed_because_bad_status_code(self):
270+
"""Non expected test on the mock credentials list route with an 404 error
271+
We expect an expection raised
272+
"""
230273
get_creds = responses.get(
231274
"https://example.prismacloud.io/api/v1/credentials",
232275
body=json.dumps(CREDENTIALS),
@@ -251,6 +294,8 @@ def test_execute_compute_failed_because_bad_status_code(self):
251294

252295
@responses.activate
253296
def test_execute_compute_with_csv_content_type(self):
297+
"""Nominal test on the mock discovery cloud download file on CSV
298+
"""
254299
discovery = responses.get(
255300
"https://example.prismacloud.io/api/v1/cloud/discovery/download",
256301
body="discovery_download",

0 commit comments

Comments
 (0)