From ef5f35672ed84adcd77e16a519df2a40a88e5769 Mon Sep 17 00:00:00 2001 From: Nicolas Clerc Date: Tue, 22 Aug 2023 11:08:43 +0200 Subject: [PATCH] =?UTF-8?q?fixup!=20=F0=9F=9B=82(backend)=20use=20playlist?= =?UTF-8?q?=20token=20in=20apis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../marsha/core/simple_jwt/factories.py | 54 +++++++++---------- .../tests/api/playlists/test_is_claimed.py | 8 +-- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/backend/marsha/core/simple_jwt/factories.py b/src/backend/marsha/core/simple_jwt/factories.py index 4ba8dd8e7e..7eb90857e3 100644 --- a/src/backend/marsha/core/simple_jwt/factories.py +++ b/src/backend/marsha/core/simple_jwt/factories.py @@ -3,6 +3,7 @@ import uuid from django.conf import settings +from rest_framework_simplejwt.exceptions import TokenError import factory from faker.utils.text import slugify @@ -13,7 +14,14 @@ PlaylistFactory, UserFactory, ) -from marsha.core.models import ADMINISTRATOR, INSTRUCTOR, LTI_ROLES, NONE, STUDENT +from marsha.core.models import ( + ADMINISTRATOR, + INSTRUCTOR, + LTI_ROLES, + NONE, + STUDENT, + Playlist, +) from marsha.core.simple_jwt.permissions import ResourceAccessPermissions from marsha.core.simple_jwt.tokens import ( ChallengeToken, @@ -128,36 +136,24 @@ def _build(cls, model_class, *args, **kwargs): class BaseResourceTokenFactory(BaseTokenFactory): """ Base class for all resource token factories. - This allows to provide a resource (video, document, ...) - to forge the JWT, or nothing to use a random UUID. + This forces to provide a playlist to forge the JWT, + or nothing to use a random UUID. """ - # Should we force the resource to be a playlist? - # - # @staticmethod - # def get_playlist_id(o): - # """Get the playlist id from the resource.""" - # resource_id = uuid.uuid4() - # if not o.resource: - # print("no resource") - # pass - # elif o.resource.__class__.__name__ == "Playlist": - # print("playlist resource") - # resource_id = o.resource.id - # elif o.resource.playlist: - # print("resource has playlist") - # resource_id = o.resource.playlist.id - # elif o.resource.video.playlist.id: - # print("resource has video") - # resource_id = o.resource.video.playlist.id - # - # return str(resource_id) - # - # resource_id = factory.LazyAttribute(get_playlist_id) - - resource_id = factory.LazyAttribute( - lambda o: str(o.resource.id if o.resource else uuid.uuid4()) - ) + @staticmethod + def get_playlist_id(o): + """Get the playlist id from the resource.""" + resource_id = uuid.uuid4() + if not o.resource: + pass + elif isinstance(o.resource, Playlist): + resource_id = o.resource.id + else: + raise TokenError("resource is not a playlist") + + return str(resource_id) + + resource_id = factory.LazyAttribute(get_playlist_id) class Meta: # pylint:disable=missing-class-docstring abstract = True diff --git a/src/backend/marsha/core/tests/api/playlists/test_is_claimed.py b/src/backend/marsha/core/tests/api/playlists/test_is_claimed.py index 40ba6e5012..d1549b0f00 100644 --- a/src/backend/marsha/core/tests/api/playlists/test_is_claimed.py +++ b/src/backend/marsha/core/tests/api/playlists/test_is_claimed.py @@ -29,7 +29,7 @@ def test_playlist_is_claimed_anonymous(self): def test_playlist_is_claimed_student(self): """Random logged-in users cannot check if a playlist is claimed.""" video = factories.VideoFactory() - jwt_token = StudentLtiTokenFactory(resource=video) + jwt_token = StudentLtiTokenFactory(resource=video.playlist) response = self.client.get( f"/api/playlists/{video.playlist.id}/is-claimed/", @@ -43,7 +43,7 @@ def test_playlist_is_claimed_LTI_administrator(self): video = factories.VideoFactory() jwt_token = InstructorOrAdminLtiTokenFactory( - resource=video, + resource=video.playlist, roles=[ADMINISTRATOR], ) @@ -61,7 +61,7 @@ def test_playlist_is_claimed_LTI_instructor(self): video = factories.VideoFactory() jwt_token = InstructorOrAdminLtiTokenFactory( - resource=video, + resource=video.playlist, roles=[INSTRUCTOR], ) @@ -79,7 +79,7 @@ def test_playlist_is_claimed_LTI_instructor_claimed(self): video = factories.VideoFactory(upload_state="pending") consumer_site = factories.ConsumerSiteFactory() jwt_token = InstructorOrAdminLtiTokenFactory( - resource=video, + resource=video.playlist, roles=[INSTRUCTOR], consumer_site=str(consumer_site.id), )