|
8 | 8 | from guardpost.authorization import ( |
9 | 9 | AuthorizationContext, |
10 | 10 | AuthorizationStrategy, |
| 11 | + ForbiddenError, |
11 | 12 | Policy, |
12 | 13 | PolicyNotFoundError, |
13 | 14 | Requirement, |
@@ -164,32 +165,6 @@ def request_identity_getter(request): |
164 | 165 | return request.user |
165 | 166 |
|
166 | 167 |
|
167 | | -@pytest.mark.asyncio |
168 | | -async def test_authorization_identity_getter(): |
169 | | - class UserNameRequirement(Requirement): |
170 | | - def __init__(self, expected_name: str): |
171 | | - self.expected_name = expected_name |
172 | | - |
173 | | - async def handle(self, context: AuthorizationContext): |
174 | | - assert context.identity is not None |
175 | | - |
176 | | - if context.identity.has_claim_value("name", self.expected_name): |
177 | | - context.succeed(self) |
178 | | - |
179 | | - auth = get_strategy( |
180 | | - [Policy("user", UserNameRequirement("Tybek"))], request_identity_getter |
181 | | - ) |
182 | | - |
183 | | - @auth(policy="user") |
184 | | - async def some_method(request: Request): |
185 | | - assert request is not None |
186 | | - return True |
187 | | - |
188 | | - value = await some_method(Request(User({"name": "Tybek"}))) |
189 | | - |
190 | | - assert value is True |
191 | | - |
192 | | - |
193 | 168 | @pytest.mark.asyncio |
194 | 169 | async def test_claims_requirement(): |
195 | 170 | auth = get_strategy( |
@@ -422,3 +397,50 @@ async def some_method(): |
422 | 397 |
|
423 | 398 | with raises(TypeError, match="Missing identity getter function."): |
424 | 399 | await some_method() |
| 400 | + |
| 401 | + |
| 402 | +class UserNameRequirement(Requirement): |
| 403 | + def __init__(self, expected_name: str): |
| 404 | + self.expected_name = expected_name |
| 405 | + |
| 406 | + async def handle(self, context: AuthorizationContext): |
| 407 | + assert context.identity is not None |
| 408 | + |
| 409 | + if context.identity.has_claim_value("name", self.expected_name): |
| 410 | + context.succeed(self) |
| 411 | + |
| 412 | + |
| 413 | +@pytest.mark.asyncio |
| 414 | +async def test_authorization_identity_getter(): |
| 415 | + auth = get_strategy( |
| 416 | + [Policy("user", UserNameRequirement("Tybek"))], request_identity_getter |
| 417 | + ) |
| 418 | + |
| 419 | + @auth(policy="user") |
| 420 | + async def some_method(request: Request): |
| 421 | + assert request is not None |
| 422 | + return True |
| 423 | + |
| 424 | + value = await some_method(Request(User({"name": "Tybek"}))) |
| 425 | + |
| 426 | + assert value is True |
| 427 | + |
| 428 | + |
| 429 | +@pytest.mark.asyncio |
| 430 | +async def test_authorization_identity_getter_forbidden(): |
| 431 | + auth = get_strategy( |
| 432 | + [Policy("user", UserNameRequirement("Tybek"))], request_identity_getter |
| 433 | + ) |
| 434 | + |
| 435 | + @auth(policy="user") |
| 436 | + async def some_method(request: Request): |
| 437 | + assert request is not None |
| 438 | + return True |
| 439 | + |
| 440 | + with pytest.raises(UnauthorizedError): |
| 441 | + await some_method( |
| 442 | + Request(User({"some_prop": "Example"}, authentication_mode=None)) |
| 443 | + ) |
| 444 | + |
| 445 | + with pytest.raises(ForbiddenError): |
| 446 | + await some_method(Request(User({"name": "Foo"}, authentication_mode="cookie"))) |
0 commit comments