Skip to content

Commit a8bcbd5

Browse files
committed
fixed test coverage
1 parent 52425a1 commit a8bcbd5

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

ellar/auth/guard.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ def openapi_security_scheme(self) -> t.Dict:
2121
return {}
2222

2323
async def can_activate(self, context: IExecutionContext) -> bool:
24-
return ( # type:ignore[no-any-return]
25-
context.switch_to_http_connection().get_request().user.is_authenticated
26-
)
24+
return context.user.is_authenticated

ellar/core/connection/http.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typing as t
22

3+
from ellar.common import Identity
34
from ellar.common.constants import SCOPE_SERVICE_PROVIDER
45
from starlette.requests import (
56
HTTPConnection as StarletteHTTPConnection,
@@ -20,6 +21,10 @@ def service_provider(self) -> "EllarInjector":
2021
), "RequestServiceProviderMiddleware must be installed to access request.service_provider"
2122
return t.cast("EllarInjector", self.scope[SCOPE_SERVICE_PROVIDER])
2223

24+
@property
25+
def user(self) -> Identity:
26+
return t.cast(Identity, self.scope["user"])
27+
2328

2429
class Request(StarletteRequest, HTTPConnection):
2530
pass

tests/test_auth/app/controllers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def fast_x(self):
1919
@Authorize()
2020
class ArticleController:
2121
@get("/create")
22-
@AuthenticationRequired("SimpleHeaderAuthHandler")
22+
@AuthenticationRequired
2323
@CheckPolicies(RequiredClaimsPolicy("article", "create", "publish"))
2424
async def create_and_publish(self):
2525
return "fast and furious 10 Article"

tests/test_guard.py

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
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+
)
310
from ellar.core import AppFactory, Reflector, Request
411
from ellar.core.guards import (
512
GuardAPIKeyCookie,
@@ -9,7 +16,7 @@
916
GuardHttpBearerAuth,
1017
GuardHttpDigestAuth,
1118
)
12-
from ellar.di import injectable
19+
from ellar.di import ProviderConfig, injectable
1320
from ellar.openapi import OpenAPIDocumentBuilder
1421
from ellar.testing import TestClient
1522
from starlette.status import HTTP_401_UNAUTHORIZED
@@ -78,6 +85,17 @@ async def authentication_handler(self, context, credentials):
7885
if credentials.credentials == "digesttoken":
7986
return credentials.credentials
8087

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+
8199

82100
app = AppFactory.create_app()
83101

@@ -247,7 +265,7 @@ def test_auth_schema():
247265
}
248266

249267

250-
def test_global_guard_works():
268+
def test_global_guard_case_1_works():
251269
_app = AppFactory.create_app(global_guards=[DigestAuth])
252270

253271
@get("/global")
@@ -262,3 +280,65 @@ def _auth_demo_endpoint(request: Inject[Request]):
262280
data = res.json()
263281

264282
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

Comments
 (0)