Skip to content

Commit

Permalink
test: behavior test for public video xblock
Browse files Browse the repository at this point in the history
  • Loading branch information
leangseu-edx committed Mar 15, 2023
1 parent 8fcca00 commit 5c47374
Showing 1 changed file with 128 additions and 8 deletions.
136 changes: 128 additions & 8 deletions lms/djangoapps/courseware/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
from lms.djangoapps.courseware.testutils import RenderXBlockTestMixin
from lms.djangoapps.courseware.toggles import COURSEWARE_OPTIMIZED_RENDER_XBLOCK, PUBLIC_VIDEO_SHARE
from lms.djangoapps.courseware.user_state_client import DjangoXBlockUserStateClient
from lms.djangoapps.courseware.views.views import (
BasePublicVideoXBlockView,
PublicVideoXBlockView,
PublicVideoXBlockEmbedView,
)
from lms.djangoapps.instructor.access import allow_access
from lms.djangoapps.verify_student.services import IDVerificationService
from openedx.core.djangoapps.catalog.tests.factories import CourseFactory as CatalogCourseFactory
Expand Down Expand Up @@ -2998,19 +3003,19 @@ def _mock_token_unpack_fn(token, user_id):
assert response.status_code == expected_response


@ddt.ddt
class TestRenderPublicVideoXBlock(ModuleStoreTestCase):
class TestBasePublicVideoXBlock(ModuleStoreTestCase):
"""
Tests for the courseware.render_public_video_xblock endpoint.
Tests for public video xblock.
"""

def setup_course(self, enable_waffle=True):
"""
Helper method to create the course.
"""
# pylint:disable=attribute-defined-outside-init

with self.store.default_store(self.store.default_modulestore.get_modulestore_type()):
course = CourseFactory.create(**{'start': datetime.now() - timedelta(days=1)})
chapter = BlockFactory.create(parent=course, category='chapter')
self.course = CourseFactory.create(**{'start': datetime.now() - timedelta(days=1)})
chapter = BlockFactory.create(parent=self.course, category='chapter')
vertical_block = BlockFactory.create(
parent_location=chapter.location,
category='vertical',
Expand All @@ -3034,11 +3039,17 @@ def setup_course(self, enable_waffle=True):
)
WaffleFlagCourseOverrideModel.objects.create(
waffle_flag=PUBLIC_VIDEO_SHARE.name,
course_id=course.id,
course_id=self.course.id,
enabled=enable_waffle,
)
CourseOverview.load_from_module_store(course.id)
CourseOverview.load_from_module_store(self.course.id)


@ddt.ddt
class TestRenderPublicVideoXBlock(TestBasePublicVideoXBlock):
"""
Tests for the courseware.render_public_video_xblock endpoint.
"""
def get_response(self, usage_key, is_embed):
"""
Overridable method to get the response from the endpoint that is being tested.
Expand Down Expand Up @@ -3448,3 +3459,112 @@ def test_course_wide_resources(self, url_name, param, is_instructor, is_rendered
else:
assert js_match == [None, None, None]
assert css_match == [None, None, None]


@ddt.ddt
class TestBasePublicVideoXBlockView(TestBasePublicVideoXBlock):
"""Test Base Public Video XBlock View tests"""
base_block = BasePublicVideoXBlockView(request=MagicMock())

@ddt.data(
(True, True),
(True, False),
(False, True),
(False, False),
)
@ddt.unpack
@patch('lms.djangoapps.courseware.views.views.get_block_by_usage_id')
def test_get_course_and_video_block(self, is_waffle_enabled, is_public_video, mock_get_block_by_usage_id):
"""
Test that get_course_and_video_block returns course and video block.
"""

self.setup_course(enable_waffle=is_waffle_enabled)
target_video = self.video_block_public if is_public_video else self.video_block_not_public

mock_get_block_by_usage_id.return_value = (target_video, None)

# get 404 unless waffle is enabled and video is public
if is_public_video and is_waffle_enabled:
course, video_block = self.base_block.get_course_and_video_block(str(target_video.location))
assert course.id == self.course.id
assert video_block.location == target_video.location
else:
with self.assertRaisesRegex(Http404, "Video not found"):
course, video_block = self.base_block.get_course_and_video_block(str(target_video.location))


@ddt.ddt
class TestPublicVideoXBlockView(TestBasePublicVideoXBlock):
"""Test Public Video XBlock View"""
request = RequestFactory().get('/?utm_source=edx.org&utm_medium=referral&utm_campaign=video')
base_block = PublicVideoXBlockView(request=request)

def test_get_template_and_context(self):
"""
Get template and context.
"""
self.setup_course(enable_waffle=True)
fragment = MagicMock()
with patch.object(self.video_block_public, "render", return_value=fragment):
template, context = self.base_block.get_template_and_context(self.course, self.video_block_public)
assert template == 'public_video.html'
assert context['fragment'] == fragment
assert context['course'] == self.course

@ddt.data("poster", None)
def test_get_social_sharing_metadata(self, poster_url):
"""
Test that get_social_sharing_metadata returns correct metadata.
"""
self.setup_course(enable_waffle=True)
# can't mock something that doesn't exist
self.video_block_public._post = MagicMock(return_value=poster_url)

metadata = self.base_block.get_social_sharing_metadata(self.course, self.video_block_public)
assert metadata["video_title"] == self.video_block_public.display_name_with_default
assert metadata["video_description"] == f"Watch a video from the course {self.course.display_name} on edX.org"
assert metadata["video_thumbnail"] == "" if poster_url is None else poster_url

def test_get_utm_params(self):
"""
Test that get_utm_params returns correct utm params.
"""
utm_params = self.base_block.get_utm_params()
assert utm_params == {
'utm_source': 'edx.org',
'utm_medium': 'referral',
'utm_campaign': 'video',
}

def test_build_url(self):
"""
Test that build_url returns correct url.
"""
base_url = 'http://test.server'
params = {
'param1': 'value1',
'param2': 'value2',
}
utm_params = {
"utm_source": "edx.org",
}
url = self.base_block.build_url(base_url, params, utm_params)
assert url == 'http://test.server?param1=value1&param2=value2&utm_source=edx.org'


class TestPublicVideoXBlockEmbedView(TestBasePublicVideoXBlock):
"""Test Public Video XBlock Embed View"""
base_block = PublicVideoXBlockEmbedView()

def test_get_template_and_context(self):
"""
Get template and context.
"""
self.setup_course(enable_waffle=True)
fragment = MagicMock()
with patch.object(self.video_block_public, "render", return_value=fragment):
template, context = self.base_block.get_template_and_context(self.course, self.video_block_public)
assert template == 'public_video_share_embed.html'
assert context['fragment'] == fragment
assert context['course'] == self.course

0 comments on commit 5c47374

Please sign in to comment.