1
1
import pytest
2
- from ellar .common import APIException , Inject , UseGuards , get , serialize_object
2
+ from ellar .common import (
3
+ APIException ,
4
+ GlobalGuard ,
5
+ Inject ,
6
+ UseGuards ,
7
+ get ,
8
+ serialize_object ,
9
+ )
3
10
from ellar .core import AppFactory , Reflector , Request
4
11
from ellar .core .guards import (
5
12
GuardAPIKeyCookie ,
9
16
GuardHttpBearerAuth ,
10
17
GuardHttpDigestAuth ,
11
18
)
12
- from ellar .di import injectable
19
+ from ellar .di import ProviderConfig , injectable
13
20
from ellar .openapi import OpenAPIDocumentBuilder
14
21
from ellar .testing import TestClient
15
22
from starlette .status import HTTP_401_UNAUTHORIZED
@@ -78,6 +85,17 @@ async def authentication_handler(self, context, credentials):
78
85
if credentials .credentials == "digesttoken" :
79
86
return credentials .credentials
80
87
88
+ if credentials .credentials == "dict" :
89
+ return {
90
+ "username" : "ellar" ,
91
+ "message" : "converted to Identity Object" ,
92
+ "auth_type" : "digest" ,
93
+ "id" : "12" ,
94
+ }
95
+
96
+ if credentials .credentials == "boolean" :
97
+ return True
98
+
81
99
82
100
app = AppFactory .create_app ()
83
101
@@ -247,7 +265,7 @@ def test_auth_schema():
247
265
}
248
266
249
267
250
- def test_global_guard_works ():
268
+ def test_global_guard_case_1_works ():
251
269
_app = AppFactory .create_app (global_guards = [DigestAuth ])
252
270
253
271
@get ("/global" )
@@ -262,3 +280,65 @@ def _auth_demo_endpoint(request: Inject[Request]):
262
280
data = res .json ()
263
281
264
282
assert data == {"detail" : "Forbidden" }
283
+
284
+
285
+ def test_global_guard_case_2_works ():
286
+ _app = AppFactory .create_app (
287
+ providers = [ProviderConfig (GlobalGuard , use_class = DigestAuth )]
288
+ )
289
+
290
+ @get ("/global" )
291
+ def _auth_demo_endpoint (request : Request ):
292
+ return {"authentication" : request .user }
293
+
294
+ _app .router .append (_auth_demo_endpoint )
295
+ _client = TestClient (_app )
296
+ res = _client .get ("/global" )
297
+
298
+ assert res .status_code == 401
299
+ data = res .json ()
300
+
301
+ assert data == {"detail" : "Forbidden" }
302
+
303
+
304
+ @pytest .mark .parametrize (
305
+ "input_type, is_authenticated, expect_result" ,
306
+ [
307
+ (
308
+ "dict" ,
309
+ True ,
310
+ {
311
+ "authentication" : {
312
+ "username" : "ellar" ,
313
+ "message" : "converted to Identity Object" ,
314
+ "id" : "12" ,
315
+ "auth_type" : "digest" ,
316
+ }
317
+ },
318
+ ),
319
+ ("boolean" , False , {"authentication" : {"id" : None , "auth_type" : None }}),
320
+ ],
321
+ )
322
+ def test_if_an_auth_guard_return_converts_to_identity (
323
+ input_type , is_authenticated , expect_result
324
+ ):
325
+ _app = AppFactory .create_app (
326
+ providers = [ProviderConfig (GlobalGuard , use_class = DigestAuth )]
327
+ )
328
+
329
+ @get ("/global" )
330
+ def _auth_demo_endpoint (request : Request ):
331
+ assert request .user .is_authenticated == is_authenticated
332
+ return {"authentication" : request .user }
333
+
334
+ _app .router .append (_auth_demo_endpoint )
335
+ _client = TestClient (_app )
336
+ res = _client .get (
337
+ "/global" ,
338
+ ** {"headers" : {"Authorization" : f"Digest { input_type } " }},
339
+ )
340
+
341
+ assert res .status_code == 200
342
+ data = res .json ()
343
+
344
+ assert data == expect_result
0 commit comments