33from datetime import datetime , timezone as baseTimezone
44import json
55import random
6- from unittest import mock
76
87from django .test import TestCase , override_settings
98
109from marsha .core import factories , models
11- from marsha .core .api import timezone
1210from marsha .core .defaults import AWS_PIPELINE
1311from marsha .core .factories import TimedTextTrackFactory , UserFactory , VideoFactory
1412from marsha .core .simple_jwt .factories import (
1513 InstructorOrAdminLtiTokenFactory ,
1614 StudentLtiTokenFactory ,
1715 UserAccessTokenFactory ,
1816)
19- from marsha .core .tests .testing_utils import RSA_KEY_MOCK
17+
18+
19+ # flake8: noqa: E501
20+ # pylint: disable=line-too-long
2021
2122
2223class TimedTextTrackRetrieveAPITest (TestCase ):
@@ -55,7 +56,7 @@ def test_api_timed_text_track_read_detail_student(self):
5556 content , {"detail" : "You do not have permission to perform this action." }
5657 )
5758
58- @override_settings (CLOUDFRONT_SIGNED_URLS_ACTIVE = False )
59+ @override_settings (MEDIA_URL = "https://abc.svc.edge.scw.cloud/" )
5960 def test_api_timed_text_track_read_detail_token_user (self ):
6061 """A token user associated to a video can read a timed text track related to this video."""
6162 timed_text_track = TimedTextTrackFactory (
@@ -91,12 +92,11 @@ def test_api_timed_text_track_read_detail_token_user(self):
9192 "language" : "fr" ,
9293 "upload_state" : "ready" ,
9394 "source_url" : (
94- "https://abc.cloudfront.net/b8d40ed7-95b8-4848-98c9-50728dfee25d/"
95- "timedtext/source/1533686400_fr_cc?response-content-disposition=a"
96- "ttachment%3B+filename%3Dfoo_1533686400.srt"
95+ "https://abc.svc.edge.scw.cloud/aws/b8d40ed7-95b8-4848-98c9-50728dfee25d/"
96+ "timedtext/source/1533686400_fr_cc"
9797 ),
9898 "url" : (
99- "https://abc.cloudfront.net /b8d40ed7-95b8-4848-98c9-50728dfee25d/"
99+ "https://abc.svc.edge.scw.cloud/aws /b8d40ed7-95b8-4848-98c9-50728dfee25d/"
100100 "timedtext/1533686400_fr_cc.vtt"
101101 ),
102102 "video" : str (timed_text_track .video .id ),
@@ -116,7 +116,7 @@ def test_api_timed_text_track_read_detail_token_user(self):
116116 content , {"detail" : "You do not have permission to perform this action." }
117117 )
118118
119- @override_settings (CLOUDFRONT_SIGNED_URLS_ACTIVE = False )
119+ @override_settings (MEDIA_URL = "https://abc.svc.edge.scw.cloud/" )
120120 def test_api_timed_text_track_without_extension_read_detail_token_user (self ):
121121 """A timed text track without extension should return empty source url."""
122122 timed_text_track = TimedTextTrackFactory (
@@ -152,7 +152,7 @@ def test_api_timed_text_track_without_extension_read_detail_token_user(self):
152152 "upload_state" : "ready" ,
153153 "source_url" : None ,
154154 "url" : (
155- "https://abc.cloudfront.net /b8d40ed7-95b8-4848-98c9-50728dfee25d/"
155+ "https://abc.svc.edge.scw.cloud/aws /b8d40ed7-95b8-4848-98c9-50728dfee25d/"
156156 "timedtext/1533686400_fr_cc.vtt"
157157 ),
158158 "video" : str (timed_text_track .video .id ),
@@ -172,7 +172,7 @@ def test_api_timed_text_track_without_extension_read_detail_token_user(self):
172172 content , {"detail" : "You do not have permission to perform this action." }
173173 )
174174
175- @override_settings (CLOUDFRONT_SIGNED_URLS_ACTIVE = False )
175+ @override_settings (MEDIA_URL = "https://abc.svc.edge.scw.cloud/" )
176176 def test_api_timed_text_track_read_detail_admin_user (self ):
177177 """Admin user associated to a video can read a timed text track related to this video."""
178178 timed_text_track = TimedTextTrackFactory (
@@ -209,12 +209,11 @@ def test_api_timed_text_track_read_detail_admin_user(self):
209209 "language" : "fr" ,
210210 "upload_state" : "ready" ,
211211 "source_url" : (
212- "https://abc.cloudfront.net/b8d40ed7-95b8-4848-98c9-50728dfee25d/timedtext/"
213- "source/1533686400_fr_cc?response-content-disposition=attachment%3B+filenam"
214- "e%3Dfoo_1533686400.srt"
212+ "https://abc.svc.edge.scw.cloud/aws/b8d40ed7-95b8-4848-98c9-50728dfee25d/timedtext/"
213+ "source/1533686400_fr_cc"
215214 ),
216215 "url" : (
217- "https://abc.cloudfront.net /b8d40ed7-95b8-4848-98c9-50728dfee25d/timedtext/"
216+ "https://abc.svc.edge.scw.cloud/aws /b8d40ed7-95b8-4848-98c9-50728dfee25d/timedtext/"
218217 "1533686400_fr_cc.vtt"
219218 ),
220219 "video" : str (timed_text_track .video .id ),
@@ -258,7 +257,6 @@ def test_api_timed_text_track_read_instructor_in_read_only(self):
258257 )
259258 self .assertEqual (response .status_code , 403 )
260259
261- @override_settings (CLOUDFRONT_SIGNED_URLS_ACTIVE = False )
262260 def test_api_timed_text_track_read_detail_token_user_no_active_stamp (self ):
263261 """A timed text track with no active stamp should not fail.
264262
@@ -279,7 +277,6 @@ def test_api_timed_text_track_read_detail_token_user_no_active_stamp(self):
279277 content = json .loads (response .content )
280278 self .assertIsNone (content ["url" ])
281279
282- @override_settings (CLOUDFRONT_SIGNED_URLS_ACTIVE = False )
283280 def test_api_timed_text_track_read_detail_token_user_not_ready (self ):
284281 """A timed_text_track that has never been uploaded successfully should have no url."""
285282 timed_text_track = TimedTextTrackFactory (
@@ -299,66 +296,6 @@ def test_api_timed_text_track_read_detail_token_user_not_ready(self):
299296 content = json .loads (response .content )
300297 self .assertIsNone (content ["url" ])
301298
302- @override_settings (
303- CLOUDFRONT_SIGNED_URLS_ACTIVE = True ,
304- CLOUDFRONT_SIGNED_PUBLIC_KEY_ID = "cloudfront-access-key-id" ,
305- )
306- @mock .patch ("builtins.open" , new_callable = mock .mock_open , read_data = RSA_KEY_MOCK )
307- def test_api_timed_text_track_read_detail_token_user_signed_urls (self , _mock_open ):
308- """Activating signed urls should add Cloudfront query string authentication parameters."""
309- timed_text_track = TimedTextTrackFactory (
310- video__pk = "b8d40ed7-95b8-4848-98c9-50728dfee25d" ,
311- video__playlist__title = "foo" ,
312- mode = "cc" ,
313- language = "fr" ,
314- uploaded_on = datetime (2018 , 8 , 8 , tzinfo = baseTimezone .utc ),
315- upload_state = "ready" ,
316- process_pipeline = AWS_PIPELINE ,
317- extension = "srt" ,
318- )
319- jwt_token = InstructorOrAdminLtiTokenFactory (
320- playlist = timed_text_track .video .playlist
321- )
322-
323- # Get the timed_text_track via the API using the JWT token
324- # fix the time so that the url signature is deterministic and can be checked
325- now = datetime (2018 , 8 , 8 , tzinfo = baseTimezone .utc )
326- with mock .patch .object (timezone , "now" , return_value = now ):
327- response = self .client .get (
328- self ._get_url (timed_text_track .video , timed_text_track ),
329- HTTP_AUTHORIZATION = f"Bearer { jwt_token } " ,
330- )
331- self .assertEqual (response .status_code , 200 )
332- content = json .loads (response .content )
333-
334- expected_cloudfront_signature = (
335- "Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6"
336- "Ly9hYmMuY2xvdWRmcm9udC5uZXQvYjhkNDBlZDctOTViOC00ODQ4LTk4YzktNTA3MjhkZmVlM"
337- "jVkLyoiLCJDb25kaXRpb24iOnsiRGF0ZUxlc3NUaGFuIjp7IkFXUzpFcG9jaFRpbWUiOjE1Mz"
338- "M2OTM2MDB9fX1dfQ__&Signature=PkRZOcfOxbalcuNG9XN6wO72enDenSetWgTthNjR4Nsy"
339- "UvCao1rZ9s4MZbqU61NDxB8Q3yDoWZUm-PP0uFa6v2Rz9g6XSTCA~-x8Yhh72-jc1J5NZOavh"
340- "~HT6lbC2HnPAesaxbVG4EejSDuXjncE8kBiUdT6YNotAv1JzbqidXuOBdkSjR32PEav98PT0r"
341- "UKmXohNAL-RFdwHL1cKGhy17CoxABn4ToDJ-t0Z4cT4husb5HebH~6nOmhlDDdFMSdmD7FjZ~"
342- "qaJwagJ3sAqG1ph9NcTX45bDn2rcrDXUy0jHWxBPYUId6NGbKCITp1SFj0QAsoxsXnh90Ibkr"
343- "GQ4XUA__&Key-Pair-Id=cloudfront-access-key-id"
344- )
345-
346- self .assertEqual (
347- content ["url" ],
348- (
349- "https://abc.cloudfront.net/b8d40ed7-95b8-4848-98c9-50728dfee25d/timedtext"
350- f"/1533686400_fr_cc.vtt?{ expected_cloudfront_signature } "
351- ),
352- )
353- self .assertEqual (
354- content ["source_url" ],
355- (
356- "https://abc.cloudfront.net/b8d40ed7-95b8-4848-98c9-50728dfee25d/timedtext"
357- "/source/1533686400_fr_cc?response-content-disposition=attachment%3B+filen"
358- f"ame%3Dfoo_1533686400.srt&{ expected_cloudfront_signature } "
359- ),
360- )
361-
362299 def test_api_timed_text_track_read_detail_staff_or_user (self ):
363300 """Users authenticated via a session are not allowed to read a timed text track detail."""
364301 for user in [UserFactory (), UserFactory (is_staff = True )]:
0 commit comments