1717
1818_VWS_HOST = "https://vws.vuforia.com"
1919_DATASET_UUID = "0b12466eee5d49409a440927006ff5d8"
20+ _MOCK_BEARER_TOKEN = "mock.header.signature"
2021
2122
2223def _dataset_request (* , cad_data_url : str ) -> dict [str , Any ]:
@@ -114,6 +115,17 @@ def _assert_model_target_error(
114115 }
115116
116117
118+ def _assert_oauth2_error (
119+ * ,
120+ response : requests .Response ,
121+ status_code : HTTPStatus ,
122+ body : dict [str , str ],
123+ ) -> None :
124+ """Assert an OAuth2 error response."""
125+ assert response .status_code == status_code
126+ assert response .json () == body
127+
128+
117129@pytest .mark .usefixtures ("verify_model_target_mock_vuforia" )
118130class TestAuthentication :
119131 """Tests for Model Target Web API authentication."""
@@ -195,46 +207,95 @@ def test_missing_bearer_token(
195207 },
196208 }
197209
198-
199- class TestMockErrors :
200- """Tests for mock-only Model Target Web API error paths."""
201-
202210 @staticmethod
203- def test_invalid_oauth2_token_request () -> None :
204- """Invalid OAuth2 token requests are rejected."""
205- with MockVWS ():
206- response = requests .post (
207- url = f"{ _VWS_HOST } /oauth2/token" ,
208- data = {"grant_type" : "unsupported" },
209- timeout = 30 ,
210- )
211+ @pytest .mark .parametrize (
212+ argnames = ("authorization" , "message" ),
213+ argvalues = [
214+ pytest .param ("Bearer " , "no Bearer token" , id = "blank" ),
215+ pytest .param (
216+ "Bearer invalid-token" ,
217+ "Invalid JWT serialization: Missing dot delimiter(s)" ,
218+ id = "malformed" ,
219+ ),
220+ ],
221+ )
222+ def test_invalid_bearer_token (
223+ * ,
224+ authorization : str ,
225+ message : str ,
226+ ) -> None :
227+ """Invalid bearer tokens are rejected."""
228+ response = requests .get (
229+ url = f"{ _VWS_HOST } /modeltargets/datasets/{ _DATASET_UUID } /status" ,
230+ headers = {"Authorization" : authorization },
231+ timeout = 30 ,
232+ )
211233
212234 _assert_model_target_error (
213235 response = response ,
214- status_code = HTTPStatus .BAD_REQUEST ,
215- code = "BAD_REQUEST " ,
216- message = "Invalid OAuth2 token request." ,
217- target = "grant_type " ,
236+ status_code = HTTPStatus .UNAUTHORIZED ,
237+ code = "401 " ,
238+ message = message ,
239+ target = "jwt " ,
218240 )
219241
220242 @staticmethod
221- def test_blank_bearer_token () -> None :
222- """A blank bearer token is rejected."""
223- with MockVWS ():
224- response = requests .get (
225- url = f"{ _VWS_HOST } /modeltargets/datasets/{ _DATASET_UUID } /status" ,
226- headers = {"Authorization" : "Bearer " },
227- timeout = 30 ,
228- )
243+ @pytest .mark .parametrize (
244+ argnames = ("auth" , "data" , "status_code" , "body" ),
245+ argvalues = [
246+ pytest .param (
247+ None ,
248+ {"grant_type" : "client_credentials" },
249+ HTTPStatus .UNAUTHORIZED ,
250+ {
251+ "error" : "invalid_request" ,
252+ "error_description" : (
253+ "Missing or invalid authorization header"
254+ ),
255+ },
256+ id = "missing-basic-auth" ,
257+ ),
258+ pytest .param (
259+ ("invalid-client-id" , "invalid-client-secret" ),
260+ {"grant_type" : "client_credentials" },
261+ HTTPStatus .UNAUTHORIZED ,
262+ {"error" : "invalid_client" },
263+ id = "invalid-client" ,
264+ ),
265+ pytest .param (
266+ ("invalid-client-id" , "invalid-client-secret" ),
267+ {"grant_type" : "unsupported" },
268+ HTTPStatus .BAD_REQUEST ,
269+ {"error" : "unsupported_grant_type" },
270+ id = "unsupported-grant-type" ,
271+ ),
272+ ],
273+ )
274+ def test_invalid_oauth2_token_request (
275+ * ,
276+ auth : tuple [str , str ] | None ,
277+ data : dict [str , str ],
278+ status_code : HTTPStatus ,
279+ body : dict [str , str ],
280+ ) -> None :
281+ """Invalid OAuth2 token requests are rejected."""
282+ response = requests .post (
283+ url = f"{ _VWS_HOST } /oauth2/token" ,
284+ auth = auth ,
285+ data = data ,
286+ timeout = 30 ,
287+ )
229288
230- _assert_model_target_error (
289+ _assert_oauth2_error (
231290 response = response ,
232- status_code = HTTPStatus .UNAUTHORIZED ,
233- code = "401" ,
234- message = "invalid Bearer token" ,
235- target = "jwt" ,
291+ status_code = status_code ,
292+ body = body ,
236293 )
237294
295+
296+ class TestMockErrors :
297+ """Tests for mock-only Model Target Web API error paths."""
298+
238299 @staticmethod
239300 @pytest .mark .parametrize (
240301 argnames = ("body" , "headers" , "message" , "target" ),
@@ -266,7 +327,10 @@ def test_invalid_request_body(
266327 with MockVWS ():
267328 response = requests .post (
268329 url = f"{ _VWS_HOST } /modeltargets/datasets" ,
269- headers = {"Authorization" : "Bearer token" , ** headers },
330+ headers = {
331+ "Authorization" : f"Bearer { _MOCK_BEARER_TOKEN } " ,
332+ ** headers ,
333+ },
270334 data = body ,
271335 timeout = 30 ,
272336 )
@@ -337,7 +401,7 @@ def test_invalid_dataset_request(
337401 with MockVWS ():
338402 response = requests .post (
339403 url = f"{ _VWS_HOST } { path } " ,
340- headers = {"Authorization" : "Bearer token " },
404+ headers = {"Authorization" : f "Bearer { _MOCK_BEARER_TOKEN } " },
341405 json = body ,
342406 timeout = 30 ,
343407 )
@@ -381,7 +445,7 @@ def test_unknown_dataset(
381445 response = requests .request (
382446 method = method ,
383447 url = f"{ _VWS_HOST } { path } " ,
384- headers = {"Authorization" : "Bearer token " },
448+ headers = {"Authorization" : f "Bearer { _MOCK_BEARER_TOKEN } " },
385449 timeout = 30 ,
386450 )
387451
@@ -399,7 +463,7 @@ def test_processing_dataset_cannot_be_downloaded() -> None:
399463 with MockVWS (processing_time_seconds = 60 ):
400464 create_response = requests .post (
401465 url = f"{ _VWS_HOST } /modeltargets/datasets" ,
402- headers = {"Authorization" : "Bearer token " },
466+ headers = {"Authorization" : f "Bearer { _MOCK_BEARER_TOKEN } " },
403467 json = _UNAUTHENTICATED_DATASET_REQUEST ,
404468 timeout = 30 ,
405469 )
@@ -408,7 +472,7 @@ def test_processing_dataset_cannot_be_downloaded() -> None:
408472 f"{ _VWS_HOST } /modeltargets/datasets/"
409473 f"{ create_response .json ()['uuid' ]} /dataset"
410474 ),
411- headers = {"Authorization" : "Bearer token " },
475+ headers = {"Authorization" : f "Bearer { _MOCK_BEARER_TOKEN } " },
412476 timeout = 30 ,
413477 )
414478
0 commit comments