-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: view permissions unit tests (#57)
* use new base class * quick save * in_school unit tests * tidy up * tidy up * add isort * ignore migrations * ignore all migrations * use job * move load plugins to pyproject.toml * feedback
- Loading branch information
Showing
19 changed files
with
848 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
© Ocado Group | ||
Created on 15/01/2024 at 15:15:20(+00:00). | ||
Test helpers for Django Rest Framework permissions. | ||
""" | ||
|
||
import typing as t | ||
|
||
from django.test import TestCase | ||
from rest_framework.request import Request | ||
from rest_framework.test import APIRequestFactory | ||
from rest_framework.views import APIView | ||
|
||
from ..permissions import AnyPermission | ||
from ..types import Args, KwArgs | ||
|
||
|
||
class PermissionTestCase(TestCase, t.Generic[AnyPermission]): | ||
"""Base for all permission test cases.""" | ||
|
||
@classmethod | ||
def get_permission_class(cls) -> t.Type[AnyPermission]: | ||
"""Get the permission's class. | ||
Returns: | ||
The permission's class. | ||
""" | ||
|
||
# pylint: disable-next=no-member | ||
return t.get_args(cls.__orig_bases__[0])[ # type: ignore[attr-defined] | ||
0 | ||
] | ||
|
||
def setUp(self): | ||
self.request_factory = APIRequestFactory() | ||
|
||
def _has_permission( | ||
self, | ||
request: Request, | ||
view: t.Optional[APIView], | ||
init_args: t.Optional[Args], | ||
init_kwargs: t.Optional[KwArgs], | ||
): | ||
view = view or APIView() | ||
init_args = init_args or tuple() | ||
init_kwargs = init_kwargs or {} | ||
|
||
permission_class = self.get_permission_class() | ||
permission = permission_class(*init_args, **init_kwargs) | ||
return permission.has_permission(request, view) | ||
|
||
def assert_has_permission( | ||
self, | ||
request: Request, | ||
view: t.Optional[APIView] = None, | ||
init_args: t.Optional[Args] = None, | ||
init_kwargs: t.Optional[KwArgs] = None, | ||
): | ||
"""Assert that the request does have permission. | ||
Args: | ||
request: The request being sent to the view. | ||
view: The view that is being requested. | ||
init_args: The arguments used to initialize the permission. | ||
init_kwargs: The keyword arguments used to initialize the | ||
permission. | ||
""" | ||
|
||
assert self._has_permission( | ||
request, | ||
view, | ||
init_args, | ||
init_kwargs, | ||
) | ||
|
||
def assert_not_has_permission( | ||
self, | ||
request: Request, | ||
view: t.Optional[APIView] = None, | ||
init_args: t.Optional[Args] = None, | ||
init_kwargs: t.Optional[KwArgs] = None, | ||
): | ||
"""Assert that the request does not have permission. | ||
Args: | ||
request: The request being sent to the view. | ||
view: The view that is being requested. | ||
init_args: The arguments used to initialize the permission. | ||
init_kwargs: The keyword arguments used to initialize the | ||
permission. | ||
""" | ||
|
||
assert not self._has_permission( | ||
request, | ||
view, | ||
init_args, | ||
init_kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
© Ocado Group | ||
Created on 15/01/2024 at 15:32:54(+00:00). | ||
Reusable type hints. | ||
""" | ||
|
||
import typing as t | ||
|
||
Args = t.Tuple[t.Any, ...] | ||
KwArgs = t.Dict[str, t.Any] | ||
|
||
JsonList = t.List["JsonValue"] | ||
JsonDict = t.Dict[str, "JsonValue"] | ||
JsonValue = t.Union[int, str, bool, JsonList, JsonDict] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,5 +58,18 @@ | |
"email": "[email protected]", | ||
"password": "pbkdf2_sha256$260000$a2nFLqpwD88sOeZ7wsQskW$WIJACyDluEJWKMPsO/jawrR0sHXHmYebDJoyUihKJxU=" | ||
} | ||
}, | ||
{ | ||
"model": "user.User", | ||
"pk": 6, | ||
"fields": { | ||
"last_saved_at": "2023-01-01 00:00:00.0+00:00", | ||
"is_active": true, | ||
"first_name": "Albert", | ||
"last_name": "Einstein", | ||
"email": "[email protected]", | ||
"password": "pbkdf2_sha256$260000$a2nFLqpwD88sOeZ7wsQskW$WIJACyDluEJWKMPsO/jawrR0sHXHmYebDJoyUihKJxU=", | ||
"teacher": 3 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.