From 8663cc2eb9801c7c8e984d3b65fc2ae21a3a5542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=86=D0=B2=D0=B0=D0=BD=20=D0=9D=D1=94=D0=B4=D1=94=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=96=D1=86=D0=B5=D0=B2?= Date: Thu, 11 Apr 2024 16:11:18 +0300 Subject: [PATCH] fix: fix tests --- cms/djangoapps/contentstore/utils.py | 53 +++++++++++++++++++ .../outline/tests/test_view.py | 14 ++--- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 0b33e88a2a65..d14944436a7e 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -2234,3 +2234,56 @@ def send_course_update_notification(course_key, content, user): audience_filters={}, ) COURSE_NOTIFICATION_REQUESTED.send_event(course_notification_data=notification_data) + + +def get_xblock_validation_messages(xblock): + """ + Retrieves validation messages for a given xblock. + Args: + xblock: The xblock object to validate. + Returns: + list: A list of validation error messages. + """ + validation_json = xblock.validate().to_json() + return validation_json['messages'] + + +def get_xblock_render_error(request, xblock): + """ + Checks if there are any rendering errors for a given block and return these. + Args: + request: WSGI request object + xblock: The xblock object to rendering. + Returns: + str: Error message which happened while rendering of xblock. + """ + from cms.djangoapps.contentstore.views.preview import _load_preview_block + from xmodule.studio_editable import has_author_view + from xmodule.x_module import AUTHOR_VIEW, STUDENT_VIEW + + def get_xblock_render_context(request, block): + """ + Return a dict of the data needs for render of each block. + """ + can_edit = has_studio_write_access(request.user, block.usage_key.course_key) + + return { + "is_unit_page": False, + "can_edit": can_edit, + "root_xblock": xblock, + "reorderable_items": set(), + "paging": None, + "force_render": None, + "item_url": "/container/{block.location}", + "tags_count_map": {}, + } + + try: + block = _load_preview_block(request, xblock) + preview_view = AUTHOR_VIEW if has_author_view(block) else STUDENT_VIEW + render_context = get_xblock_render_context(request, block) + block.render(preview_view, render_context) + except Exception as exc: # pylint: disable=broad-except + return str(exc) + + return "" diff --git a/lms/djangoapps/course_home_api/outline/tests/test_view.py b/lms/djangoapps/course_home_api/outline/tests/test_view.py index 797cf4cca442..4f40d9a80192 100644 --- a/lms/djangoapps/course_home_api/outline/tests/test_view.py +++ b/lms/djangoapps/course_home_api/outline/tests/test_view.py @@ -467,7 +467,7 @@ def __init__(self, *args, **kwargs): def setUp(self): super().setUp() - self.url = reverse('course-home:course-sidebar-blocks', args=[self.course.id]) + self.url = reverse('course-home:course-navigation', args=[self.course.id]) def update_course_and_overview(self): """ @@ -573,7 +573,7 @@ def test_get_unknown_course(self): """ Test that the API returns a 404 when the course is not found. """ - url = reverse('course-home:course-sidebar-blocks', args=['course-v1:unknown+course+2T2020']) + url = reverse('course-home:course-navigation', args=['course-v1:unknown+course+2T2020']) response = self.client.get(url) assert response.status_code == 404 @@ -624,7 +624,7 @@ def test_proctored_exam(self, mock_summary): 'suggested_icon': 'fa-foo-bar', } - url = reverse('course-home:course-sidebar-blocks', args=[course.id]) + url = reverse('course-home:course-navigation', args=[course.id]) response = self.client.get(url) assert response.status_code == 200 @@ -715,7 +715,7 @@ def test_empty_blocks_complete(self): """ self.add_blocks_to_course() CourseEnrollment.enroll(self.user, self.course.id) - url = reverse('course-home:course-sidebar-blocks', args=[self.course.id]) + url = reverse('course-home:course-navigation', args=[self.course.id]) response = self.client.get(url) assert response.status_code == 200 @@ -731,7 +731,7 @@ def test_blocks_complete_with_problem(self, problem_complete): CourseEnrollment.enroll(self.user, self.course.id) self.create_completion(problem, int(problem_complete)) - response = self.client.get(reverse('course-home:course-sidebar-blocks', args=[self.course.id])) + response = self.client.get(reverse('course-home:course-navigation', args=[self.course.id])) sequence_data = response.data['blocks'][str(self.sequential.location)] vertical_data = response.data['blocks'][str(self.vertical.location)] @@ -750,7 +750,7 @@ def test_blocks_completion_stat(self): CourseEnrollment.enroll(self.user, self.course.id) self.create_completion(completed_problem, 1) self.create_completion(uncompleted_problem, 0) - response = self.client.get(reverse('course-home:course-sidebar-blocks', args=[self.course.id])) + response = self.client.get(reverse('course-home:course-navigation', args=[self.course.id])) expected_sequence_completion_stat = { 'completion': 0, @@ -779,7 +779,7 @@ def test_blocks_completion_stat_all_problem_completed(self): CourseEnrollment.enroll(self.user, self.course.id) self.create_completion(problem1, 1) self.create_completion(problem2, 1) - response = self.client.get(reverse('course-home:course-sidebar-blocks', args=[self.course.id])) + response = self.client.get(reverse('course-home:course-navigation', args=[self.course.id])) expected_sequence_completion_stat = { 'completion': 1,