Skip to content

Commit

Permalink
fixup! 🛂(backend) use playlist token in apis
Browse files Browse the repository at this point in the history
  • Loading branch information
kernicPanel committed Aug 22, 2023
1 parent 5dded3a commit ef5f356
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
54 changes: 25 additions & 29 deletions src/backend/marsha/core/simple_jwt/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand All @@ -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],
)

Expand All @@ -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],
)

Expand All @@ -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),
)
Expand Down

0 comments on commit ef5f356

Please sign in to comment.