test_video_handlers.py 29.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# -*- coding: utf-8 -*-
"""Video xmodule tests in mongo."""

from mock import patch
import os
import tempfile
import textwrap
import json
from datetime import timedelta
from webob import Request
Oleg Marshev committed
11
from mock import MagicMock, Mock
12 13 14

from xmodule.contentstore.content import StaticContent
from xmodule.contentstore.django import contentstore
15
from xmodule.modulestore.django import modulestore
16
from xmodule.modulestore import ModuleStoreEnum
17
from xmodule.x_module import STUDENT_VIEW
18 19 20 21 22
from . import BaseTestXmodule
from .test_video_xml import SOURCE_XML
from cache_toolbox.core import del_cached_content
from xmodule.exceptions import NotFoundError

23 24 25 26
from xmodule.video_module.transcripts_utils import (
    TranscriptException,
    TranscriptsGenerationException,
)
27

28
SRT_content = textwrap.dedent("""
29 30 31 32
        0
        00:00:00,12 --> 00:00:00,100
        Привіт, edX вітає вас.
    """)
33 34 35 36 37 38 39


def _create_srt_file(content=None):
    """
    Create srt file in filesystem.
    """
    content = content or SRT_content
40
    srt_file = tempfile.NamedTemporaryFile(suffix=".srt")
41
    srt_file.content_type = 'application/x-subrip; charset=utf-8'
42 43 44 45 46
    srt_file.write(content)
    srt_file.seek(0)
    return srt_file


47 48 49 50 51
def _check_asset(location, asset_name):
    """
    Check that asset with asset_name exists in assets.
    """
    content_location = StaticContent.compute_location(
52
        location.course_key, asset_name
53 54 55 56 57 58 59 60
    )
    try:
        contentstore().find(content_location)
    except NotFoundError:
        return False
    else:
        return True

61 62 63 64 65 66
def _clear_assets(location):
    """
    Clear all assets for location.
    """
    store = contentstore()

67
    assets, __ = store.get_all_content_for_course(location.course_key)
68
    for asset in assets:
69
        asset_location = asset['asset_key']
70
        del_cached_content(asset_location)
Don Mitchell committed
71
        store.delete(asset_location)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


def _get_subs_id(filename):
    basename = os.path.splitext(os.path.basename(filename))[0]
    return basename.replace('subs_', '').replace('.srt', '')


def _create_file(content=''):
    """
    Create temporary subs_somevalue.srt.sjson file.
    """
    sjson_file = tempfile.NamedTemporaryFile(prefix="subs_", suffix=".srt.sjson")
    sjson_file.content_type = 'application/json'
    sjson_file.write(textwrap.dedent(content))
    sjson_file.seek(0)
    return sjson_file


def _upload_sjson_file(subs_file, location, default_filename='subs_{}.srt.sjson'):
    filename = default_filename.format(_get_subs_id(subs_file.name))
    _upload_file(subs_file, location, filename)


def _upload_file(subs_file, location, filename):
    mime_type = subs_file.content_type
    content_location = StaticContent.compute_location(
98
        location.course_key, filename
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    )
    content = StaticContent(content_location, filename, mime_type, subs_file.read())
    contentstore().save(content)
    del_cached_content(content.location)


class TestVideo(BaseTestXmodule):
    """Integration tests: web client + mongo."""
    CATEGORY = "video"
    DATA = SOURCE_XML
    METADATA = {}

    def test_handle_ajax_wrong_dispatch(self):
        responses = {
            user.username: self.clients[user.username].post(
                self.get_url('whatever'),
                {},
                HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            for user in self.users
        }

        self.assertEqual(
            set([
                response.status_code
                for _, response in responses.items()
                ]).pop(),
            404)

    def test_handle_ajax(self):

        data = [
            {'speed': 2.0},
            {'saved_video_position': "00:00:10"},
132
            {'transcript_language': 'uk'},
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        ]
        for sample in data:
            response = self.clients[self.users[0].username].post(
                self.get_url('save_user_state'),
                sample,
                HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(response.status_code, 200)

        self.assertEqual(self.item_descriptor.speed, None)
        self.item_descriptor.handle_ajax('save_user_state', {'speed': json.dumps(2.0)})
        self.assertEqual(self.item_descriptor.speed, 2.0)
        self.assertEqual(self.item_descriptor.global_speed, 2.0)

        self.assertEqual(self.item_descriptor.saved_video_position, timedelta(0))
        self.item_descriptor.handle_ajax('save_user_state', {'saved_video_position': "00:00:10"})
        self.assertEqual(self.item_descriptor.saved_video_position, timedelta(0, 10))

        self.assertEqual(self.item_descriptor.transcript_language, 'en')
151
        self.item_descriptor.handle_ajax('save_user_state', {'transcript_language': "uk"})
152 153 154 155 156
        self.assertEqual(self.item_descriptor.transcript_language, 'uk')

    def tearDown(self):
        _clear_assets(self.item_descriptor.location)

157 158 159
class TestTranscriptAvailableTranslationsDispatch(TestVideo):
    """
    Test video handler that provide available translations info.
160

161
    Tests for `available_translations` dispatch.
162
    """
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    non_en_file = _create_srt_file()
    DATA = """
        <video show_captions="true"
        display_name="A Name"
        >
            <source src="example.mp4"/>
            <source src="example.webm"/>
            <transcript language="uk" src="{}"/>
        </video>
    """.format(os.path.split(non_en_file.name)[1])

    MODEL_DATA = {
        'data': DATA
    }

    def setUp(self):
        super(TestTranscriptAvailableTranslationsDispatch, self).setUp()
180
        self.item_descriptor.render(STUDENT_VIEW)
181 182 183 184 185 186 187 188
        self.item = self.item_descriptor.xmodule_runtime.xmodule_instance
        self.subs = {"start": [10], "end": [100], "text": ["Hi, welcome to Edx."]}

    def test_available_translation_en(self):
        good_sjson = _create_file(json.dumps(self.subs))
        _upload_sjson_file(good_sjson, self.item_descriptor.location)
        self.item.sub = _get_subs_id(good_sjson.name)

189
        request = Request.blank('/available_translations')
190 191 192 193 194 195
        response = self.item.transcript(request=request, dispatch='available_translations')
        self.assertEqual(json.loads(response.body), ['en'])

    def test_available_translation_non_en(self):
        _upload_file(self.non_en_file, self.item_descriptor.location, os.path.split(self.non_en_file.name)[1])

196
        request = Request.blank('/available_translations')
197 198 199 200 201
        response = self.item.transcript(request=request, dispatch='available_translations')
        self.assertEqual(json.loads(response.body), ['uk'])

    def test_multiple_available_translations(self):
        good_sjson = _create_file(json.dumps(self.subs))
202

203 204 205 206 207 208
        # Upload english transcript.
        _upload_sjson_file(good_sjson, self.item_descriptor.location)

        # Upload non-english transcript.
        _upload_file(self.non_en_file, self.item_descriptor.location, os.path.split(self.non_en_file.name)[1])

209 210 211
        self.item.sub = _get_subs_id(good_sjson.name)

        request = Request.blank('/available_translations')
212 213 214
        response = self.item.transcript(request=request, dispatch='available_translations')
        self.assertEqual(json.loads(response.body), ['en', 'uk'])

215

216 217 218 219 220
class TestTranscriptDownloadDispatch(TestVideo):
    """
    Test video handler that provide translation transcripts.

    Tests for `download` dispatch.
221 222 223 224 225
    """

    DATA = """
        <video show_captions="true"
        display_name="A Name"
226
        sub='OEoXaMPEzfM'
227 228 229 230
        >
            <source src="example.mp4"/>
            <source src="example.webm"/>
        </video>
231
    """
232 233 234 235 236 237

    MODEL_DATA = {
        'data': DATA
    }

    def setUp(self):
238
        super(TestTranscriptDownloadDispatch, self).setUp()
239
        self.item_descriptor.render(STUDENT_VIEW)
240 241 242
        self.item = self.item_descriptor.xmodule_runtime.xmodule_instance

    def test_download_transcript_not_exist(self):
243
        request = Request.blank('/download')
244 245 246
        response = self.item.transcript(request=request, dispatch='download')
        self.assertEqual(response.status, '404 Not Found')

247
    @patch('xmodule.video_module.VideoModule.get_transcript', return_value=('Subs!', 'test_filename.srt', 'application/x-subrip; charset=utf-8'))
248
    def test_download_srt_exist(self, __):
249
        request = Request.blank('/download')
250 251
        response = self.item.transcript(request=request, dispatch='download')
        self.assertEqual(response.body, 'Subs!')
252 253
        self.assertEqual(response.headers['Content-Type'], 'application/x-subrip; charset=utf-8')
        self.assertEqual(response.headers['Content-Language'], 'en')
254

255
    @patch('xmodule.video_module.VideoModule.get_transcript', return_value=('Subs!', 'txt', 'text/plain; charset=utf-8'))
256 257
    def test_download_txt_exist(self, __):
        self.item.transcript_format = 'txt'
258
        request = Request.blank('/download')
259 260
        response = self.item.transcript(request=request, dispatch='download')
        self.assertEqual(response.body, 'Subs!')
261 262
        self.assertEqual(response.headers['Content-Type'], 'text/plain; charset=utf-8')
        self.assertEqual(response.headers['Content-Language'], 'en')
263

264
    def test_download_en_no_sub(self):
265
        request = Request.blank('/download')
266 267 268 269 270
        response = self.item.transcript(request=request, dispatch='download')
        self.assertEqual(response.status, '404 Not Found')
        with self.assertRaises(NotFoundError):
            self.item.get_transcript()

271 272 273 274 275 276 277 278 279 280
    @patch('xmodule.video_module.VideoModule.get_transcript', return_value=('Subs!', u"塞.srt", 'application/x-subrip; charset=utf-8'))
    def test_download_non_en_non_ascii_filename(self, __):
        request = Request.blank('/download')
        response = self.item.transcript(request=request, dispatch='download')
        self.assertEqual(response.body, 'Subs!')
        self.assertEqual(response.headers['Content-Type'], 'application/x-subrip; charset=utf-8')
        self.assertEqual(response.headers['Content-Disposition'], 'attachment; filename="塞.srt"')


class TestTranscriptTranslationGetDispatch(TestVideo):
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    """
    Test video handler that provide translation transcripts.

    Tests for `translation` dispatch.
    """

    non_en_file = _create_srt_file()
    DATA = """
        <video show_captions="true"
        display_name="A Name"
        >
            <source src="example.mp4"/>
            <source src="example.webm"/>
            <transcript language="uk" src="{}"/>
        </video>
    """.format(os.path.split(non_en_file.name)[1])

    MODEL_DATA = {
        'data': DATA
    }

    def setUp(self):
303
        super(TestTranscriptTranslationGetDispatch, self).setUp()
304
        self.item_descriptor.render(STUDENT_VIEW)
305
        self.item = self.item_descriptor.xmodule_runtime.xmodule_instance
306

307
    def test_translation_fails(self):
308 309
        # No language
        request = Request.blank('/translation')
310 311 312
        response = self.item.transcript(request=request, dispatch='translation')
        self.assertEqual(response.status, '400 Bad Request')

313
        # No videoId - HTML5 video with language that is not in available languages
314 315
        request = Request.blank('/translation/ru')
        response = self.item.transcript(request=request, dispatch='translation/ru')
316 317
        self.assertEqual(response.status, '404 Not Found')

318
        # Language is not in available languages
319 320
        request = Request.blank('/translation/ru?videoId=12345')
        response = self.item.transcript(request=request, dispatch='translation/ru')
321 322
        self.assertEqual(response.status, '404 Not Found')

323
    def test_translaton_en_youtube_success(self):
324 325 326 327 328 329
        subs = {"start": [10], "end": [100], "text": ["Hi, welcome to Edx."]}
        good_sjson = _create_file(json.dumps(subs))
        _upload_sjson_file(good_sjson, self.item_descriptor.location)
        subs_id = _get_subs_id(good_sjson.name)

        self.item.sub = subs_id
330 331
        request = Request.blank('/translation/en?videoId={}'.format(subs_id))
        response = self.item.transcript(request=request, dispatch='translation/en')
332 333
        self.assertDictEqual(json.loads(response.body), subs)

334
    def test_translation_non_en_youtube_success(self):
335 336 337 338 339 340 341 342 343 344 345 346 347
        subs = {
            u'end': [100],
            u'start': [12],
            u'text': [
            u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.'
        ]}
        self.non_en_file.seek(0)
        _upload_file(self.non_en_file, self.item_descriptor.location, os.path.split(self.non_en_file.name)[1])
        subs_id = _get_subs_id(self.non_en_file.name)

        # youtube 1_0 request, will generate for all speeds for existing ids
        self.item.youtube_id_1_0 = subs_id
        self.item.youtube_id_0_75 = '0_75'
348 349
        request = Request.blank('/translation/uk?videoId={}'.format(subs_id))
        response = self.item.transcript(request=request, dispatch='translation/uk')
350 351 352
        self.assertDictEqual(json.loads(response.body), subs)

        # 0_75 subs are exist
353 354
        request = Request.blank('/translation/uk?videoId={}'.format('0_75'))
        response = self.item.transcript(request=request, dispatch='translation/uk')
355 356 357 358 359 360 361 362 363 364
        calculated_0_75 = {
            u'end': [75],
            u'start': [9],
            u'text': [
            u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.'
            ]
        }
        self.assertDictEqual(json.loads(response.body), calculated_0_75)
        # 1_5 will be generated from 1_0
        self.item.youtube_id_1_5 = '1_5'
365 366
        request = Request.blank('/translation/uk?videoId={}'.format('1_5'))
        response = self.item.transcript(request=request, dispatch='translation/uk')
367 368 369 370 371 372 373 374 375
        calculated_1_5 = {
            u'end': [150],
            u'start': [18],
            u'text': [
            u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.'
            ]
        }
        self.assertDictEqual(json.loads(response.body), calculated_1_5)

376 377 378 379 380 381 382
    def test_translaton_en_html5_success(self):
        subs = {"start": [10], "end": [100], "text": ["Hi, welcome to Edx."]}
        good_sjson = _create_file(json.dumps(subs))
        _upload_sjson_file(good_sjson, self.item_descriptor.location)
        subs_id = _get_subs_id(good_sjson.name)

        self.item.sub = subs_id
383 384
        request = Request.blank('/translation/en')
        response = self.item.transcript(request=request, dispatch='translation/en')
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        self.assertDictEqual(json.loads(response.body), subs)

    def test_translaton_non_en_html5_success(self):
        subs = {
            u'end': [100],
            u'start': [12],
            u'text': [
            u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.'
            ]
        }
        self.non_en_file.seek(0)
        _upload_file(self.non_en_file, self.item_descriptor.location, os.path.split(self.non_en_file.name)[1])

        # manually clean youtube_id_1_0, as it has default value
        self.item.youtube_id_1_0 = ""
400 401
        request = Request.blank('/translation/uk')
        response = self.item.transcript(request=request, dispatch='translation/uk')
402 403
        self.assertDictEqual(json.loads(response.body), subs)

Oleg Marshev committed
404
    def test_translation_static_transcript_xml_with_data_dirc(self):
405
        """
Oleg Marshev committed
406 407 408 409
        Test id data_dir is set in XML course.

        Set course data_dir and ensure we get redirected to that path
        if it isn't found in the contentstore.
410
        """
Oleg Marshev committed
411 412 413 414 415
        # Simulate data_dir set in course.
        test_modulestore = MagicMock()
        attrs = {'get_course.return_value': Mock(data_dir='dummy/static', static_asset_path='')}
        test_modulestore.configure_mock(**attrs)
        self.item_descriptor.runtime.modulestore = test_modulestore
416 417 418 419 420 421 422

        # Test youtube style en
        request = Request.blank('/translation/en?videoId=12345')
        response = self.item.transcript(request=request, dispatch='translation/en')
        self.assertEqual(response.status, '307 Temporary Redirect')
        self.assertIn(
            ('Location', '/static/dummy/static/subs_12345.srt.sjson'),
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
            response.headerlist
        )

        # Test HTML5 video style
        self.item.sub = 'OEoXaMPEzfM'
        request = Request.blank('/translation/en')
        response = self.item.transcript(request=request, dispatch='translation/en')
        self.assertEqual(response.status, '307 Temporary Redirect')
        self.assertIn(
            ('Location', '/static/dummy/static/subs_OEoXaMPEzfM.srt.sjson'),
            response.headerlist
        )

        # Test different language to ensure we are just ignoring it since we can't
        # translate with static fallback
        request = Request.blank('/translation/uk')
        response = self.item.transcript(request=request, dispatch='translation/uk')
        self.assertEqual(response.status, '404 Not Found')

Oleg Marshev committed
442
    def test_translation_static_transcript(self):
443
        """
Oleg Marshev committed
444 445
        Set course static_asset_path and ensure we get redirected to that path
        if it isn't found in the contentstore
446
        """
Oleg Marshev committed
447 448 449 450 451
        self.course.static_asset_path = 'dummy/static'
        self.course.save()
        store = modulestore()
        with store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
            store.update_item(self.course, self.user.id)
452 453 454 455 456 457 458

        # Test youtube style en
        request = Request.blank('/translation/en?videoId=12345')
        response = self.item.transcript(request=request, dispatch='translation/en')
        self.assertEqual(response.status, '307 Temporary Redirect')
        self.assertIn(
            ('Location', '/static/dummy/static/subs_12345.srt.sjson'),
459 460 461 462
            response.headerlist
        )

        # Test HTML5 video style
463
        self.item.sub = 'OEoXaMPEzfM'
464 465 466 467
        request = Request.blank('/translation/en')
        response = self.item.transcript(request=request, dispatch='translation/en')
        self.assertEqual(response.status, '307 Temporary Redirect')
        self.assertIn(
468
            ('Location', '/static/dummy/static/subs_OEoXaMPEzfM.srt.sjson'),
469 470 471
            response.headerlist
        )

472 473
        # Test different language to ensure we are just ignoring it since we can't
        # translate with static fallback
474 475
        request = Request.blank('/translation/uk')
        response = self.item.transcript(request=request, dispatch='translation/uk')
476
        self.assertEqual(response.status, '404 Not Found')
477

478

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
class TestStudioTranscriptTranslationGetDispatch(TestVideo):
    """
    Test Studio video handler that provide translation transcripts.

    Tests for `translation` dispatch GET HTTP method.
    """
    non_en_file = _create_srt_file()
    DATA = """
        <video show_captions="true"
        display_name="A Name"
        >
            <source src="example.mp4"/>
            <source src="example.webm"/>
            <transcript language="uk" src="{}"/>
            <transcript language="zh" src="{}"/>
        </video>
    """.format(os.path.split(non_en_file.name)[1], u"塞.srt".encode('utf8'))

    MODEL_DATA = {'data': DATA}

    def test_translation_fails(self):
        # No language
        request = Request.blank('')
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation')
        self.assertEqual(response.status, '400 Bad Request')

        # No filename in request.GET
        request = Request.blank('')
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')
        self.assertEqual(response.status, '400 Bad Request')

        # Correct case:
        filename = os.path.split(self.non_en_file.name)[1]
        _upload_file(self.non_en_file, self.item_descriptor.location, filename)
        self.non_en_file.seek(0)
        request = Request.blank(u'translation/uk?filename={}'.format(filename))
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')
        self.assertEqual(response.body, self.non_en_file.read())
        self.assertEqual(response.headers['Content-Type'], 'application/x-subrip; charset=utf-8')
        self.assertEqual(
            response.headers['Content-Disposition'],
            'attachment; filename="{}"'.format(filename)
        )
        self.assertEqual(response.headers['Content-Language'], 'uk')

        # Non ascii file name download:
        self.non_en_file.seek(0)
        _upload_file(self.non_en_file, self.item_descriptor.location, u'塞.srt')
        self.non_en_file.seek(0)
        request = Request.blank('translation/zh?filename={}'.format(u'塞.srt'.encode('utf8')))
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/zh')
        self.assertEqual(response.body, self.non_en_file.read())
        self.assertEqual(response.headers['Content-Type'], 'application/x-subrip; charset=utf-8')
        self.assertEqual(response.headers['Content-Disposition'], 'attachment; filename="塞.srt"')
        self.assertEqual(response.headers['Content-Language'], 'zh')


class TestStudioTranscriptTranslationPostDispatch(TestVideo):
    """
    Test Studio video handler that provide translation transcripts.

    Tests for `translation` dispatch with HTTP POST method.
    """
    DATA = """
        <video show_captions="true"
        display_name="A Name"
        >
            <source src="example.mp4"/>
            <source src="example.webm"/>

        </video>
    """

    MODEL_DATA = {
        'data': DATA
    }

    METADATA = {}

    def test_studio_transcript_post(self):
        # Check for exceptons:

        # Language is passed, bad content or filename:

        # should be first, as other tests save transcrips to store.
        request = Request.blank('/translation/uk', POST={'file': ('filename.srt', SRT_content)})
        with patch('xmodule.video_module.video_handlers.save_to_store'):
            with self.assertRaises(TranscriptException):  # transcripts were not saved to store for some reason.
                response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')
        request = Request.blank('/translation/uk', POST={'file': ('filename', 'content')})
        with self.assertRaises(TranscriptsGenerationException):  # Not an srt filename
            self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')

        request = Request.blank('/translation/uk', POST={'file': ('filename.srt', 'content')})
        with self.assertRaises(TranscriptsGenerationException):  # Content format is not srt.
            response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')

        request = Request.blank('/translation/uk', POST={'file': ('filename.srt', SRT_content.decode('utf8').encode('cp1251'))})
        with self.assertRaises(UnicodeDecodeError):  # Non-UTF8 file content encoding.
            response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')

        # No language is passed.
        request = Request.blank('/translation', POST={'file': ('filename', SRT_content)})
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation')
        self.assertEqual(response.status,  '400 Bad Request')

        # Language, good filename and good content.
        request = Request.blank('/translation/uk', POST={'file': ('filename.srt', SRT_content)})
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation/uk')
        self.assertEqual(response.status, '201 Created')
        self.assertDictEqual(json.loads(response.body), {'filename': u'filename.srt', 'status': 'Success'})
        self.assertDictEqual(self.item_descriptor.transcripts, {})
        self.assertTrue(_check_asset(self.item_descriptor.location, u'filename.srt'))


594
class TestGetTranscript(TestVideo):
595 596 597
    """
    Make sure that `get_transcript` method works correctly
    """
598
    non_en_file = _create_srt_file()
599 600 601 602 603 604
    DATA = """
        <video show_captions="true"
        display_name="A Name"
        >
            <source src="example.mp4"/>
            <source src="example.webm"/>
605
            <transcript language="uk" src="{}"/>
606
            <transcript language="zh" src="{}"/>
607
        </video>
608
    """.format(os.path.split(non_en_file.name)[1], u"塞.srt".encode('utf8'))
609

610 611 612 613 614 615
    MODEL_DATA = {
        'data': DATA
    }
    METADATA = {}

    def setUp(self):
616
        super(TestGetTranscript, self).setUp()
617
        self.item_descriptor.render(STUDENT_VIEW)
618 619
        self.item = self.item_descriptor.xmodule_runtime.xmodule_instance

620 621 622 623
    def test_good_transcript(self):
        """
        Test for download 'en' sub with html5 video and self.sub has correct non-empty value.
        """
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
        good_sjson = _create_file(content=textwrap.dedent("""\
                {
                  "start": [
                    270,
                    2720
                  ],
                  "end": [
                    2720,
                    5430
                  ],
                  "text": [
                    "Hi, welcome to Edx.",
                    "Let&#39;s start with what is on your screen right now."
                  ]
                }
            """))

        _upload_sjson_file(good_sjson, self.item.location)
        self.item.sub = _get_subs_id(good_sjson.name)
643 644 645

        text, filename, mime_type = self.item.get_transcript()

646 647 648 649 650 651 652 653 654 655 656 657
        expected_text = textwrap.dedent("""\
            0
            00:00:00,270 --> 00:00:02,720
            Hi, welcome to Edx.

            1
            00:00:02,720 --> 00:00:05,430
            Let&#39;s start with what is on your screen right now.

            """)

        self.assertEqual(text, expected_text)
658
        self.assertEqual(filename[:-4], self.item.sub)
659
        self.assertEqual(mime_type, 'application/x-subrip; charset=utf-8')
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    def test_good_txt_transcript(self):
        good_sjson = _create_file(content=textwrap.dedent("""\
                {
                  "start": [
                    270,
                    2720
                  ],
                  "end": [
                    2720,
                    5430
                  ],
                  "text": [
                    "Hi, welcome to Edx.",
                    "Let&#39;s start with what is on your screen right now."
                  ]
                }
            """))

        _upload_sjson_file(good_sjson, self.item.location)
        self.item.sub = _get_subs_id(good_sjson.name)
681
        text, filename, mime_type = self.item.get_transcript("txt")
682 683 684 685 686
        expected_text = textwrap.dedent("""\
            Hi, welcome to Edx.
            Let's start with what is on your screen right now.""")

        self.assertEqual(text, expected_text)
687
        self.assertEqual(filename, self.item.sub + '.txt')
688
        self.assertEqual(mime_type, 'text/plain; charset=utf-8')
689 690

    def test_en_with_empty_sub(self):
691

692
        # no self.sub, self.youttube_1_0 exist, but no file in assets
693 694 695
        with self.assertRaises(NotFoundError):
            self.item.get_transcript()

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
        # no self.sub and no self.youtube_1_0
        self.item.youtube_id_1_0 = None
        with self.assertRaises(ValueError):
            self.item.get_transcript()

        # no self.sub but youtube_1_0 exists with file in assets
        good_sjson = _create_file(content=textwrap.dedent("""\
                {
                  "start": [
                    270,
                    2720
                  ],
                  "end": [
                    2720,
                    5430
                  ],
                  "text": [
                    "Hi, welcome to Edx.",
                    "Let&#39;s start with what is on your screen right now."
                  ]
                }
            """))
        _upload_sjson_file(good_sjson, self.item.location)
        self.item.youtube_id_1_0 = _get_subs_id(good_sjson.name)

        text, filename, mime_type = self.item.get_transcript()
        expected_text = textwrap.dedent("""\
            0
            00:00:00,270 --> 00:00:02,720
            Hi, welcome to Edx.

            1
            00:00:02,720 --> 00:00:05,430
            Let&#39;s start with what is on your screen right now.

            """)

        self.assertEqual(text, expected_text)
        self.assertEqual(filename, self.item.youtube_id_1_0 + '.srt')
735
        self.assertEqual(mime_type, 'application/x-subrip; charset=utf-8')
736

737 738
    def test_non_en_with_non_ascii_filename(self):
        self.item.transcript_language = 'zh'
739
        self.non_en_file.seek(0)
740
        _upload_file(self.non_en_file, self.item_descriptor.location, u"塞.srt")
741 742 743 744 745 746 747 748

        text, filename, mime_type = self.item.get_transcript()
        expected_text = textwrap.dedent("""
        0
        00:00:00,12 --> 00:00:00,100
        Привіт, edX вітає вас.
        """)
        self.assertEqual(text, expected_text)
749 750 751
        self.assertEqual(filename, u"塞.srt")
        self.assertEqual(mime_type, 'application/x-subrip; charset=utf-8')

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    def test_value_error(self):
        good_sjson = _create_file(content='bad content')

        _upload_sjson_file(good_sjson, self.item.location)
        self.item.sub = _get_subs_id(good_sjson.name)

        with self.assertRaises(ValueError):
            self.item.get_transcript()

    def test_key_error(self):
        good_sjson = _create_file(content="""
                {
                  "start": [
                    270,
                    2720
                  ],
                  "end": [
                    2720,
                    5430
                  ]
                }
            """)

        _upload_sjson_file(good_sjson, self.item.location)
        self.item.sub = _get_subs_id(good_sjson.name)

        with self.assertRaises(KeyError):
            self.item.get_transcript()