generated from ocadotechnology/codeforlife-template-backend
-
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.
- Loading branch information
Showing
13 changed files
with
192 additions
and
15 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
""" | ||
© Ocado Group | ||
Created on 24/01/2024 at 12:14:21(+00:00). | ||
""" | ||
|
||
from codeforlife.user.serializers import ClassSerializer as _ClassSerializer | ||
|
||
|
||
# pylint: disable-next=missing-class-docstring | ||
class ClassSerializer(_ClassSerializer): | ||
class Meta(_ClassSerializer.Meta): | ||
pass |
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,89 @@ | ||
""" | ||
© Ocado Group | ||
Created on 24/01/2024 at 09:47:04(+00:00). | ||
""" | ||
|
||
from unittest.mock import patch | ||
|
||
from codeforlife.tests import ModelViewSetTestCase | ||
from codeforlife.user.models import OtpBypassToken, User | ||
from rest_framework import status | ||
|
||
from ...views import OtpBypassTokenViewSet | ||
|
||
|
||
# pylint: disable-next=missing-class-docstring | ||
class TestOtpBypassTokenViewSet(ModelViewSetTestCase[OtpBypassToken]): | ||
basename = "otp-bypass-token" | ||
model_view_set_class = OtpBypassTokenViewSet | ||
|
||
def setUp(self): | ||
self.user = User.objects.get(email="[email protected]") | ||
assert not self.user.otp_bypass_tokens.exists() | ||
|
||
self.otp_bypass_tokens = OtpBypassToken.objects.bulk_create( | ||
[ | ||
OtpBypassToken(user=self.user, token=token) | ||
for token in OtpBypassToken.generate_tokens() | ||
] | ||
) | ||
|
||
def test_generate(self): | ||
""" | ||
Generate max number of OTP bypass tokens. | ||
""" | ||
|
||
user = self.client.login_teacher( | ||
email="[email protected]", | ||
password="Password1", | ||
) | ||
assert user == self.user | ||
|
||
tokens = { | ||
"aaaaaaaa", | ||
"bbbbbbbb", | ||
"cccccccc", | ||
"dddddddd", | ||
"eeeeeeee", | ||
"ffffffff", | ||
"gggggggg", | ||
"hhhhhhhh", | ||
"iiiiiiii", | ||
"jjjjjjjj", | ||
} | ||
assert len(tokens) == OtpBypassToken.max_count | ||
|
||
with patch.object( | ||
OtpBypassToken, "generate_tokens", return_value=tokens | ||
) as generate_tokens: | ||
response = self.client.post( | ||
self.client.reverse("generate"), | ||
status_code_assertion=status.HTTP_201_CREATED, | ||
) | ||
|
||
generate_tokens.assert_called_once() | ||
|
||
# We received the expected tokens. | ||
assert set(response.json()) == tokens | ||
|
||
# The user's pre-existing tokens were deleted. | ||
assert ( | ||
OtpBypassToken.objects.filter( | ||
pk__in=[ | ||
otp_bypass_token.pk | ||
for otp_bypass_token in self.otp_bypass_tokens | ||
] | ||
).count() | ||
== 0 | ||
) | ||
|
||
# The new tokens all check out. | ||
for otp_bypass_token in OtpBypassToken.objects.filter(user=user): | ||
found_token = False | ||
for token in tokens.copy(): | ||
found_token = otp_bypass_token.check_token(token) | ||
if found_token: | ||
tokens.remove(token) | ||
break | ||
|
||
assert found_token |
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
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
""" | ||
© Ocado Group | ||
Created on 23/01/2024 at 17:53:37(+00:00). | ||
""" | ||
|
||
from codeforlife.user.views import ClassViewSet as _ClassViewSet | ||
|
||
from ..serializers import ClassSerializer | ||
|
||
|
||
# pylint: disable-next=missing-class-docstring,too-many-ancestors | ||
class ClassViewSet(_ClassViewSet): | ||
http_method_names = ["get", "post", "patch", "delete"] | ||
serializer_class = ClassSerializer |
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,46 @@ | ||
""" | ||
© Ocado Group | ||
Created on 23/01/2024 at 17:54:08(+00:00). | ||
""" | ||
|
||
import typing as t | ||
|
||
from codeforlife.permissions import AllowNone | ||
from codeforlife.request import Request | ||
from codeforlife.user.models import OtpBypassToken, User | ||
from codeforlife.user.permissions import IsTeacher | ||
from codeforlife.views import ModelViewSet | ||
from rest_framework import status | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
|
||
|
||
# pylint: disable-next=missing-class-docstring,too-many-ancestors | ||
class OtpBypassTokenViewSet(ModelViewSet[OtpBypassToken]): | ||
http_method_names = ["post"] | ||
|
||
def get_queryset(self): | ||
user: User = self.request.user # type: ignore[assignment] | ||
return OtpBypassToken.objects.filter(user=user) | ||
|
||
def get_permissions(self): | ||
if self.action == "create": | ||
return [AllowNone()] | ||
|
||
return [IsTeacher()] | ||
|
||
@action(detail=False, methods=["post"]) | ||
def generate(self, request: Request): | ||
"""Generates some OTP bypass tokens for a user.""" | ||
|
||
user = t.cast(User, request.user) | ||
|
||
OtpBypassToken.objects.filter(user=user).delete() | ||
|
||
tokens = OtpBypassToken.generate_tokens() | ||
|
||
OtpBypassToken.objects.bulk_create( | ||
[OtpBypassToken(user=user, token=token) for token in tokens] | ||
) | ||
|
||
return Response(tokens, status.HTTP_201_CREATED) |
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
""" | ||
© Ocado Group | ||
Created on 23/01/2024 at 17:53:50(+00:00). | ||
""" | ||
|
||
from codeforlife.user.views import SchoolViewSet as _SchoolViewSet | ||
|
||
from ..serializers import SchoolSerializer | ||
|
||
|
||
# pylint: disable-next=missing-class-docstring,too-many-ancestors | ||
class SchoolViewSet(_SchoolViewSet): | ||
http_method_names = ["get", "post", "patch"] | ||
serializer_class = SchoolSerializer |
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