77import requests
88from jsonschema import validate
99
10- from .configuration .config import SERVICE_BASE_PATH , ENVIRONMENT , ACCESS_TOKEN_HASH_SECRET
10+ from .configuration .config import SERVICE_BASE_PATH , ENVIRONMENT , ACCESS_TOKEN_HASH_SECRET , APP_CLIENT_ID
1111
1212
1313class TestSplunkLogging :
14- url = f"https://{ ENVIRONMENT } .api.service.nhs.uk/{ SERVICE_BASE_PATH } /splunk-test"
14+ oauth_protected_url = f"https://{ ENVIRONMENT } .api.service.nhs.uk/{ SERVICE_BASE_PATH } /splunk-test"
15+ apikey_protected_url = f"https://{ ENVIRONMENT } .api.service.nhs.uk/{ SERVICE_BASE_PATH } /apikey-protected"
1516 open_access_url = f"https://{ ENVIRONMENT } .api.service.nhs.uk/{ SERVICE_BASE_PATH } /open-access"
1617
1718 @staticmethod
@@ -37,7 +38,7 @@ async def test_splunk_auth_with_client_credentials(self, get_token_client_creden
3738 # When
3839 await debug .start_trace ()
3940 requests .get (
40- url = self .url ,
41+ url = self .oauth_protected_url ,
4142 headers = {"Authorization" : f"Bearer { token } " },
4243 )
4344 payload = await self ._get_payload_from_splunk (debug )
@@ -65,7 +66,7 @@ async def test_splunk_auth_with_authorization_code(self, get_token, debug):
6566 # When
6667 await debug .start_trace ()
6768 requests .get (
68- url = self .url ,
69+ url = self .oauth_protected_url ,
6970 headers = {"Authorization" : f"Bearer { token } " },
7071 )
7172 payload = await self ._get_payload_from_splunk (debug )
@@ -93,7 +94,7 @@ async def test_splunk_auth_with_cis2_token_exchange(self, get_token_cis2_token_e
9394 # When
9495 await debug .start_trace ()
9596 requests .get (
96- url = self .url ,
97+ url = self .oauth_protected_url ,
9798 headers = {"Authorization" : f"Bearer { token } " },
9899 )
99100 payload = await self ._get_payload_from_splunk (debug )
@@ -121,7 +122,7 @@ async def test_splunk_auth_with_nhs_login_token_exchange(self, get_token_nhs_log
121122 # When
122123 await debug .start_trace ()
123124 requests .get (
124- url = self .url ,
125+ url = self .oauth_protected_url ,
125126 headers = {"Authorization" : f"Bearer { token } " },
126127 )
127128 payload = await self ._get_payload_from_splunk (debug )
@@ -139,6 +140,162 @@ async def test_splunk_auth_with_nhs_login_token_exchange(self, get_token_nhs_log
139140 auth_user = auth ["user" ]
140141 assert auth_user ["user_id" ] == "900000000001"
141142
143+ @pytest .mark .splunk
144+ @pytest .mark .asyncio
145+ async def test_splunk_auth_with_invalid_token (self , debug ):
146+ # Given
147+ token = "invalid token"
148+ expected_hashed_token = ""
149+
150+ # When
151+ await debug .start_trace ()
152+ requests .get (
153+ url = self .oauth_protected_url ,
154+ headers = {"Authorization" : f"Bearer { token } " },
155+ )
156+ payload = await self ._get_payload_from_splunk (debug )
157+
158+ # Then
159+ auth = payload ["auth" ]
160+ assert auth ["access_token_hash" ] == expected_hashed_token
161+
162+ auth_meta = auth ["meta" ]
163+ assert auth_meta ["auth_type" ] == "app"
164+ assert auth_meta ["grant_type" ] == ""
165+ assert auth_meta ["level" ] == "Level0"
166+ assert auth_meta ["provider" ] == "apim"
167+
168+ auth_user = auth ["user" ]
169+ assert auth_user ["user_id" ] == ""
170+
171+ meta = payload ["meta" ]
172+ assert meta ["client_id" ] == "Not provided"
173+ assert meta ["application" ] == "unknown"
174+ assert meta ["product" ] == ""
175+
176+ @pytest .mark .splunk
177+ @pytest .mark .asyncio
178+ async def test_splunk_auth_with_expired_token (self , debug ):
179+ # Given
180+ token = "zRygtc34R2pwxbiUktLsMJWX0iJW"
181+ expected_hashed_token = ""
182+
183+ # When
184+ await debug .start_trace ()
185+ requests .get (
186+ url = self .oauth_protected_url ,
187+ headers = {"Authorization" : f"Bearer { token } " },
188+ )
189+ payload = await self ._get_payload_from_splunk (debug )
190+
191+ # Then
192+ auth = payload ["auth" ]
193+ assert auth ["access_token_hash" ] == expected_hashed_token
194+
195+ auth_meta = auth ["meta" ]
196+ assert auth_meta ["auth_type" ] == "app"
197+ assert auth_meta ["grant_type" ] == ""
198+ assert auth_meta ["level" ] == "Level0"
199+ assert auth_meta ["provider" ] == "apim"
200+
201+ auth_user = auth ["user" ]
202+ assert auth_user ["user_id" ] == ""
203+
204+ meta = payload ["meta" ]
205+ assert meta ["client_id" ] == "Not provided"
206+ assert meta ["application" ] == "unknown"
207+ assert meta ["product" ] == ""
208+
209+ @pytest .mark .splunk
210+ @pytest .mark .asyncio
211+ @pytest .mark .debug
212+ async def test_splunk_auth_with_apikey (self , debug ):
213+ # Given
214+ apikey = APP_CLIENT_ID
215+
216+ # When
217+ await debug .start_trace ()
218+ requests .get (
219+ url = self .apikey_protected_url ,
220+ headers = {"apikey" : apikey },
221+ )
222+ payload = await self ._get_payload_from_splunk (debug )
223+
224+ # Then
225+ auth = payload ["auth" ]
226+ assert auth ["access_token_hash" ] == ""
227+
228+ auth_meta = auth ["meta" ]
229+ assert auth_meta ["auth_type" ] == "app"
230+ assert auth_meta ["grant_type" ] == ""
231+ assert auth_meta ["level" ] == "Level0"
232+ assert auth_meta ["provider" ] == "apim"
233+
234+ auth_user = auth ["user" ]
235+ assert auth_user ["user_id" ] == ""
236+
237+ meta = payload ["meta" ]
238+ assert meta ["client_id" ] == apikey
239+
240+ @pytest .mark .splunk
241+ @pytest .mark .asyncio
242+ async def test_splunk_auth_with_invalid_apikey (self , debug ):
243+ # Given
244+ apikey = "invalid api key"
245+
246+ # When
247+ await debug .start_trace ()
248+ requests .get (
249+ url = self .apikey_protected_url ,
250+ headers = {"apikey" : apikey },
251+ )
252+ payload = await self ._get_payload_from_splunk (debug )
253+
254+ # Then
255+ auth = payload ["auth" ]
256+ assert auth ["access_token_hash" ] == ""
257+
258+ auth_meta = auth ["meta" ]
259+ assert auth_meta ["auth_type" ] == "app"
260+ assert auth_meta ["grant_type" ] == ""
261+ assert auth_meta ["level" ] == "Level0"
262+ assert auth_meta ["provider" ] == "apim"
263+
264+ auth_user = auth ["user" ]
265+ assert auth_user ["user_id" ] == ""
266+
267+ meta = payload ["meta" ]
268+ assert meta ["client_id" ] == "Not provided"
269+ assert meta ["application" ] == "unknown"
270+ assert meta ["product" ] == ""
271+
272+ @pytest .mark .splunk
273+ @pytest .mark .asyncio
274+ async def test_splunk_auth_open_access (self , debug ):
275+ # When
276+ await debug .start_trace ()
277+ requests .get (
278+ url = self .open_access_url ,
279+ )
280+ payload = await self ._get_payload_from_splunk (debug )
281+
282+ # Then
283+ auth = payload ["auth" ]
284+ assert auth ["access_token_hash" ] == ""
285+
286+ auth_meta = auth ["meta" ]
287+ assert auth_meta ["auth_type" ] == "app"
288+ assert auth_meta ["grant_type" ] == ""
289+ assert auth_meta ["level" ] == "Level0"
290+ assert auth_meta ["provider" ] == "apim"
291+
292+ auth_user = auth ["user" ]
293+ assert auth_user ["user_id" ] == ""
294+
295+ meta = payload ["meta" ]
296+ assert meta ["client_id" ] == "Not provided"
297+ assert meta ["application" ] == "unknown"
298+
142299 @pytest .mark .splunk
143300 @pytest .mark .asyncio
144301 async def test_splunk_payload_schema (self , get_token , debug ):
@@ -148,7 +305,7 @@ async def test_splunk_payload_schema(self, get_token, debug):
148305 # When
149306 await debug .start_trace ()
150307 requests .get (
151- url = self .url ,
308+ url = self .oauth_protected_url ,
152309 headers = {"Authorization" : f"Bearer { token } " },
153310 )
154311 payload = await self ._get_payload_from_splunk (debug )
0 commit comments