From 60947cefc96211e1287992fd97b47263f89329be Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:50:56 -0500 Subject: [PATCH 1/2] fix: studio url evaluation (#36092) --- cms/djangoapps/contentstore/tasks.py | 29 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/cms/djangoapps/contentstore/tasks.py b/cms/djangoapps/contentstore/tasks.py index ebe043b5647..a88d04fde5a 100644 --- a/cms/djangoapps/contentstore/tasks.py +++ b/cms/djangoapps/contentstore/tasks.py @@ -1126,6 +1126,14 @@ def _get_urls(content): def _is_studio_url(url): """Returns True if url is a studio url.""" + return _is_studio_url_with_base(url) or _is_studio_url_without_base(url) + +def _is_studio_url_with_base(url): + """Returns True if url is a studio url with cms base.""" + return url.startswith('http://' + settings.CMS_BASE) or url.startswith('https://' + settings.CMS_BASE) + +def _is_studio_url_without_base(url): + """Returns True if url is a studio url without cms base.""" return not url.startswith('http://') and not url.startswith('https://') def _convert_to_standard_url(url, course_key): @@ -1136,14 +1144,14 @@ def _convert_to_standard_url(url, course_key): /static/getting-started_x250.png /container/block-v1:edX+DemoX+Demo_Course+type@vertical+block@2152d4a4aadc4cb0af5256394a3d1fc7 """ - if _is_studio_url(url): + if _is_studio_url_without_base(url): if url.startswith('/static/'): processed_url = replace_static_urls(f'\"{url}\"', course_id=course_key)[1:-1] - return 'http://' + settings.CMS_BASE + processed_url + return 'https://' + settings.CMS_BASE + processed_url elif url.startswith('/'): - return 'http://' + settings.CMS_BASE + url + return 'https://' + settings.CMS_BASE + url else: - return 'http://' + settings.CMS_BASE + '/container/' + url + return 'https://' + settings.CMS_BASE + '/container/' + url else: return url @@ -1235,14 +1243,15 @@ def _filter_by_status(results): filtered_results = [] retry_list = [] for result in results: - if result['status'] is None: - retry_list.append([result['block_id'], result['url']]) - elif result['status'] == 200: + status, block_id, url = result['status'], result['block_id'], result['url'] + if status is None: + retry_list.append([block_id, url]) + elif status == 200: continue - elif result['status'] == 403 and _is_studio_url(result['url']): - filtered_results.append([result['block_id'], result['url'], True]) + elif status == 403 and _is_studio_url(url): + filtered_results.append([block_id, url, True]) else: - filtered_results.append([result['block_id'], result['url'], False]) + filtered_results.append([block_id, url, False]) return filtered_results, retry_list From 4a2c14831b3471b091d06fd5c7b2211dd9a4e3d2 Mon Sep 17 00:00:00 2001 From: Jesper Hodge Date: Tue, 14 Jan 2025 16:59:24 -0500 Subject: [PATCH 2/2] test: add test file to check API authorizations --- .../v0/tests/test_course_optimizer.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 cms/djangoapps/contentstore/rest_api/v0/tests/test_course_optimizer.py diff --git a/cms/djangoapps/contentstore/rest_api/v0/tests/test_course_optimizer.py b/cms/djangoapps/contentstore/rest_api/v0/tests/test_course_optimizer.py new file mode 100644 index 00000000000..d8227d1670d --- /dev/null +++ b/cms/djangoapps/contentstore/rest_api/v0/tests/test_course_optimizer.py @@ -0,0 +1,22 @@ +from cms.djangoapps.contentstore.tests.test_utils import AuthorizeStaffTestCase +from rest_framework import status +from django.test import TestCase +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from django.urls import reverse + +class TestCourseOptimizer(AuthorizeStaffTestCase, ModuleStoreTestCase, TestCase): + ''' + Tests for CourseOptimizer + ''' + def test_inherited(self): + # This method ensures that pytest recognizes this class as containing tests + pass + + def make_request(self, course_id=None, data=None): + return self.client.get(self.get_url(course_id), data) + + def get_url(self, course_key): + return reverse( + 'cms.djangoapps.contentstore:v0:link_check_status', + kwargs={'course_id': 'course-v1:someOrg+someCourse+someRun'} + )