11import uuid
2- import unittest
32from typing import Set
43
54from lib .apigee import ApigeeApp
1615from utils .immunisation_api import ImmunisationApi
1716from utils .resource import generate_imms_resource
1817from utils .mappings import VaccineTypes
19- from utils .constants import env_internal_dev
2018
2119
22- @unittest .skipIf (env_internal_dev , "TestApplicationRestrictedAuthorization for internal-dev environment" )
2320class TestApplicationRestrictedAuthorization (ImmunizationBaseTest ):
2421
2522 my_app : ApigeeApp
@@ -40,12 +37,16 @@ def make_app(self, permissions: Set[Permission], vaxx_type_perms: Set = None):
4037
4138 self .my_imms_api = ImmunisationApi (base_url , app_res_auth )
4239
40+ # Runs after each individual test method in a test class.
41+ # It’s used to clean up resources that were initialized specifically for a single test.
4342 def tearDown (self ):
4443 self .apigee_service .delete_application (self .my_app .name )
44+ self .my_imms_api .cleanup_test_records ()
45+ self .default_imms_api .cleanup_test_records ()
4546
4647 def test_get_imms_authorised (self ):
4748 """it should get Immunization if app has immunization:read permission"""
48- imms_id = self .create_immunization_resource (self . default_imms_api )
49+ imms_id = self .default_imms_api . create_immunization_resource ()
4950 self .make_app ({Permission .READ })
5051 # When
5152 response = self .my_imms_api .get_immunization_by_id (imms_id )
@@ -57,7 +58,7 @@ def test_get_imms_unauthorised(self):
5758 perms = app_full_access (exclude = {Permission .READ })
5859 self .make_app (perms )
5960 # When
60- response = self .my_imms_api .get_immunization_by_id ("id-doesn't-matter" )
61+ response = self .my_imms_api .get_immunization_by_id ("id-doesn't-matter" , expected_status_code = 403 )
6162 # Then
6263 self .assertEqual (response .status_code , 403 , response .text )
6364
@@ -75,7 +76,7 @@ def test_create_imms_unauthorised_vaxx(self):
7576 self .make_app ({Permission .CREATE }, {"flu:create" })
7677 # When
7778 imms = generate_imms_resource ()
78- response = self .my_imms_api .create_immunization (imms )
79+ response = self .my_imms_api .create_immunization (imms , expected_status_code = 403 )
7980 # Then
8081 self .assertEqual (response .status_code , 403 , response .text )
8182
@@ -85,14 +86,14 @@ def test_create_imms_unauthorised(self):
8586 self .make_app (perms )
8687 # When
8788 imms = generate_imms_resource ()
88- result = self .my_imms_api .create_immunization (imms )
89+ result = self .my_imms_api .create_immunization (imms , expected_status_code = 403 )
8990 # Then
9091 self .assertEqual (result .status_code , 403 , result .text )
9192
9293 def test_update_imms_authorised (self ):
9394 """it should update Immunization if app has immunization:update and immunization:create permission"""
9495 imms = generate_imms_resource ()
95- imms_id = self .create_immunization_resource (self . default_imms_api , imms )
96+ imms_id = self .default_imms_api . create_immunization_resource (imms )
9697 imms ["id" ] = imms_id
9798
9899 self .make_app ({Permission .CREATE , Permission .UPDATE })
@@ -106,26 +107,26 @@ def test_update_imms_unauthorised(self):
106107 perms = app_full_access (exclude = {Permission .UPDATE })
107108 self .make_app (perms )
108109 # When
109- response = self .my_imms_api .update_immunization ("doesn't-matter" , {})
110+ response = self .my_imms_api .update_immunization ("doesn't-matter" , {}, expected_status_code = 403 )
110111 # Then
111112 self .assertEqual (response .status_code , 403 , response .text )
112113
113114 def test_update_imms_unauthorised_2 (self ):
114115 """it should not update Immunization if app doesn't immunization:create permission"""
115116 imms = generate_imms_resource ()
116- imms_id = self .create_immunization_resource (self . default_imms_api , imms )
117+ imms_id = self .default_imms_api . create_immunization_resource (imms )
117118 imms ["id" ] = imms_id
118119
119120 perms = app_full_access (exclude = {Permission .CREATE })
120121 self .make_app (perms )
121122 # When
122- response = self .my_imms_api .update_immunization (imms_id , imms )
123+ response = self .my_imms_api .update_immunization (imms_id , imms , expected_status_code = 403 )
123124 # Then
124125 self .assertEqual (response .status_code , 403 , response .text )
125126
126127 def test_delete_imms_authorised (self ):
127128 """it should delete Immunization if app has immunization:delete permission"""
128- imms_id = self .create_immunization_resource (self . default_imms_api )
129+ imms_id = self .default_imms_api . create_immunization_resource ()
129130 self .make_app ({Permission .DELETE })
130131 # When
131132 response = self .my_imms_api .delete_immunization (imms_id )
@@ -137,14 +138,14 @@ def test_delete_imms_unauthorised(self):
137138 perms = app_full_access (exclude = {Permission .DELETE })
138139 self .make_app (perms )
139140 # When
140- response = self .my_imms_api .delete_immunization ("doesn't-matter" )
141+ response = self .my_imms_api .delete_immunization ("doesn't-matter" , expected_status_code = 403 )
141142 # Then
142143 self .assertEqual (response .status_code , 403 , response .text )
143144
144145 def test_search_imms_authorised (self ):
145146 """it should search Immunization if app has immunization:search permission"""
146147 mmr = generate_imms_resource (valid_nhs_number1 , VaccineTypes .mmr )
147- _ = self .create_immunization_resource (self . default_imms_api , mmr )
148+ _ = self .default_imms_api . create_immunization_resource (mmr )
148149
149150 self .make_app ({Permission .SEARCH })
150151 # When
@@ -157,23 +158,22 @@ def test_search_imms_unauthorised(self):
157158 perms = app_full_access (exclude = {Permission .SEARCH })
158159 self .make_app (perms )
159160 # When
160- response = self .my_imms_api .search_immunizations (valid_nhs_number1 , VaccineTypes .mmr )
161+ response = self .my_imms_api .search_immunizations (valid_nhs_number1 , VaccineTypes .mmr , expected_status_code = 403 )
161162 # Then
162163 self .assertEqual (response .status_code , 403 , response .text )
163164
164165 def test_search_imms_unauthorised_vax (self ):
165166 """it should not search Immunization if app does not have proper vax permissions"""
166167 mmr = generate_imms_resource (valid_nhs_number1 , VaccineTypes .mmr )
167- _ = self .create_immunization_resource (self . default_imms_api , mmr )
168+ _ = self .default_imms_api . create_immunization_resource (mmr )
168169
169170 self .make_app ({Permission .SEARCH }, {"flu:read" })
170171 # When
171- response = self .my_imms_api .search_immunizations (valid_nhs_number1 , VaccineTypes .mmr )
172+ response = self .my_imms_api .search_immunizations (valid_nhs_number1 , VaccineTypes .mmr , expected_status_code = 403 )
172173 # Then
173174 self .assertEqual (response .status_code , 403 , response .text )
174175
175176
176- @unittest .skipIf (env_internal_dev , "TestCis2Authorization for internal-dev environment" )
177177class TestCis2Authorization (ImmunizationBaseTest ):
178178 my_app : ApigeeApp
179179 my_imms_api : ImmunisationApi
@@ -193,12 +193,16 @@ def make_app(self, permissions: Set[Permission], vaxx_type_perms: Set = None):
193193
194194 self .my_imms_api = ImmunisationApi (base_url , app_res_auth )
195195
196+ # Runs after each individual test method in a test class.
197+ # It’s used to clean up resources that were initialized specifically for a single test.
196198 def tearDown (self ):
197199 self .apigee_service .delete_application (self .my_app .name )
200+ self .my_imms_api .cleanup_test_records ()
201+ self .default_imms_api .cleanup_test_records ()
198202
199203 def test_get_imms_authorised (self ):
200204 """it should get Immunization if app has immunization:read permission"""
201- imms_id = self .create_immunization_resource (self . default_imms_api )
205+ imms_id = self .default_imms_api . create_immunization_resource ()
202206 self .make_app ({Permission .READ })
203207 # When
204208 response = self .my_imms_api .get_immunization_by_id (imms_id )
@@ -210,16 +214,16 @@ def test_get_imms_unauthorised(self):
210214 perms = app_full_access (exclude = {Permission .READ })
211215 self .make_app (perms )
212216 # When
213- response = self .my_imms_api .get_immunization_by_id ("id-doesn't-matter" )
217+ response = self .my_imms_api .get_immunization_by_id ("id-doesn't-matter" , expected_status_code = 403 )
214218 # Then
215219 self .assertEqual (response .status_code , 403 , response .text )
216220
217221 def test_get_imms__unauthorised_vaxx (self ):
218222 """it should not get Immunization if app does not have the correct vaccine permission"""
219- imms_id = self .create_immunization_resource (self . default_imms_api )
223+ imms_id = self .default_imms_api . create_immunization_resource ()
220224 self .make_app ({Permission .READ }, {"flu:create" })
221225 # When
222- response = self .my_imms_api .get_immunization_by_id (imms_id )
226+ response = self .my_imms_api .get_immunization_by_id (imms_id , expected_status_code = 403 )
223227 # Then
224228 self .assertEqual (response .status_code , 403 , response .text )
225229
@@ -238,7 +242,7 @@ def test_create_imms_unauthorised(self):
238242 self .make_app (perms )
239243 # When
240244 imms = generate_imms_resource ()
241- result = self .my_imms_api .create_immunization (imms )
245+ result = self .my_imms_api .create_immunization (imms , expected_status_code = 403 )
242246 # Then
243247 self .assertEqual (result .status_code , 403 , result .text )
244248
@@ -247,14 +251,14 @@ def test_create_imms_unauthorised_vaxx(self):
247251 self .make_app ({Permission .CREATE }, {"flu:create" })
248252 # When
249253 imms = generate_imms_resource ()
250- response = self .my_imms_api .create_immunization (imms )
254+ response = self .my_imms_api .create_immunization (imms , expected_status_code = 403 )
251255 # Then
252256 self .assertEqual (response .status_code , 403 , response .text )
253257
254258 def test_update_imms_authorised (self ):
255259 """it should update Immunization if app has the immunization:update and immunization:create permission"""
256260 imms = generate_imms_resource ()
257- imms_id = self .create_immunization_resource (self . default_imms_api , imms )
261+ imms_id = self .default_imms_api . create_immunization_resource (imms )
258262 imms ["id" ] = imms_id
259263
260264 self .make_app ({Permission .CREATE , Permission .UPDATE })
@@ -268,25 +272,25 @@ def test_update_imms_unauthorised(self):
268272 perms = app_full_access (exclude = {Permission .UPDATE })
269273 self .make_app (perms )
270274 # When
271- response = self .my_imms_api .update_immunization ("doesn't-matter" , {})
275+ response = self .my_imms_api .update_immunization ("doesn't-matter" , {}, expected_status_code = 403 )
272276 # Then
273277 self .assertEqual (response .status_code , 403 , response .text )
274278
275279 def test_update_imms_unauthorised_vaxx (self ):
276280 """it should not update Immunization if app does not have the correct vaccine permission"""
277281 imms = generate_imms_resource ()
278- imms_id = self .create_immunization_resource (self . default_imms_api , imms )
282+ imms_id = self .default_imms_api . create_immunization_resource (imms )
279283 imms ["id" ] = imms_id
280284
281285 self .make_app ({Permission .CREATE , Permission .UPDATE }, {"flu:create" })
282286 # When
283- response = self .my_imms_api .update_immunization (imms_id , imms )
287+ response = self .my_imms_api .update_immunization (imms_id , imms , expected_status_code = 403 )
284288 # Then
285289 self .assertEqual (response .status_code , 403 , response .text )
286290
287291 def test_delete_imms_authorised (self ):
288292 """it should delete Immunization if app has immunization:delete permission"""
289- imms_id = self .create_immunization_resource (self . default_imms_api )
293+ imms_id = self .default_imms_api . create_immunization_resource ()
290294 self .make_app ({Permission .DELETE })
291295 # When
292296 response = self .my_imms_api .delete_immunization (imms_id )
@@ -298,23 +302,23 @@ def test_delete_imms_unauthorised(self):
298302 perms = app_full_access (exclude = {Permission .DELETE })
299303 self .make_app (perms )
300304 # When
301- response = self .my_imms_api .delete_immunization ("doesn't-matter" )
305+ response = self .my_imms_api .delete_immunization ("doesn't-matter" , expected_status_code = 403 )
302306 # Then
303307 self .assertEqual (response .status_code , 403 , response .text )
304308
305309 def test_delete_imms__unauthorised_vaxx (self ):
306310 """it should not delete Immunization if app does not have the correct vaccine permission"""
307- imms_id = self .create_immunization_resource (self . default_imms_api )
311+ imms_id = self .default_imms_api . create_immunization_resource ()
308312 self .make_app ({Permission .READ }, {"flu:create" })
309313 # When
310- response = self .my_imms_api .delete_immunization (imms_id )
314+ response = self .my_imms_api .delete_immunization (imms_id , expected_status_code = 403 )
311315 # Then
312316 self .assertEqual (response .status_code , 403 , response .text )
313317
314318 def test_search_imms_authorised (self ):
315319 """it should search Immunization if app has immunization:search permission"""
316320 mmr = generate_imms_resource (valid_nhs_number1 , VaccineTypes .mmr )
317- _ = self .create_immunization_resource (self . default_imms_api , mmr )
321+ _ = self .default_imms_api . create_immunization_resource (mmr )
318322
319323 self .make_app ({Permission .SEARCH })
320324 # When
@@ -327,6 +331,6 @@ def test_search_imms_unauthorised(self):
327331 perms = app_full_access (exclude = {Permission .SEARCH })
328332 self .make_app (perms )
329333 # When
330- response = self .my_imms_api .search_immunizations (valid_nhs_number1 , VaccineTypes .mmr )
334+ response = self .my_imms_api .search_immunizations (valid_nhs_number1 , VaccineTypes .mmr , expected_status_code = 403 )
331335 # Then
332336 self .assertEqual (response .status_code , 403 , response .text )
0 commit comments