Skip to content

Commit

Permalink
feat: Add test for detecting broken and locked links
Browse files Browse the repository at this point in the history
  • Loading branch information
mfarhan943 committed Feb 20, 2025
1 parent 7dd4a09 commit 8d3c29c
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions cms/djangoapps/contentstore/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,69 @@ def setUp(self):
)
self.mock_urls = [
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@1", "http://example.com/valid"],
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@2", "http://example.com/invalid"]
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@2", "http://example.com/invalid"],
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@3", f'http://{settings.CMS_BASE}/locked'],
]
self.expected_file_contents = [
['block-v1:edX+DemoX+Demo_Course+type@vertical+block@1', 'http://example.com/valid', False],
['block-v1:edX+DemoX+Demo_Course+type@vertical+block@2', 'http://example.com/invalid', False]
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@2", "http://example.com/invalid", False],
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@3", f"http://{settings.CMS_BASE}/locked", True],
]

@mock.patch('cms.djangoapps.contentstore.tasks.UserTaskArtifact', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks._scan_course_for_links')
@mock.patch('cms.djangoapps.contentstore.tasks._save_broken_links_file', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks._write_broken_links_to_file', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks._validate_urls_access_in_batches', autospec=True)
def test_check_broken_links_stores_broken_and_locked_urls(
self,
mock_validate_urls,
mock_write_broken_links_to_file,
mock_save_broken_links_file,
mock_scan_course_for_links,
mock_user_task_artifact
):
'''
The test verifies that the check_broken_links task correctly
stores broken or locked URLs in the course.
The expected behavior is that the after scanning the course,
validating the URLs, and filtering the results, the task stores the results in a
JSON file.
Note that this test mocks all validation functions and therefore
does not test link validation or any of its support functions.
'''
mock_user = UserFactory.create(username='student', password='password')
mock_course_key_string = "course-v1:edX+DemoX+Demo_Course"
mock_task = MockCourseLinkCheckTask()
mock_scan_course_for_links.return_value = self.mock_urls
mock_validate_urls.return_value = [
{
"block_id": "block-v1:edX+DemoX+Demo_Course+type@vertical+block@1",
"url": "http://example.com/valid",
"status": 200,
},
{
"block_id": "block-v1:edX+DemoX+Demo_Course+type@vertical+block@2",
"url": "http://example.com/invalid",
"status": 400,
},
{
"block_id": "block-v1:edX+DemoX+Demo_Course+type@vertical+block@3",
"url": f"http://{settings.CMS_BASE}/locked",
"status": 403,
},
]

_check_broken_links(mock_task, mock_user.id, mock_course_key_string, 'en') # pylint: disable=no-value-for-parameter

# Check that UserTaskArtifact was called with the correct arguments
mock_user_task_artifact.assert_called_once_with(status=mock.ANY, name='BrokenLinks')

# Check that the correct links are written to the file
mock_write_broken_links_to_file.assert_called_once_with(self.expected_file_contents, mock.ANY)

# Check that _save_broken_links_file was called with the correct arguments
mock_save_broken_links_file.assert_called_once_with(mock_user_task_artifact.return_value, mock.ANY)

def test_hash_tags_stripped_from_url_lists(self):
NUM_HASH_TAG_LINES = 2
url_list = '''
Expand Down

0 comments on commit 8d3c29c

Please sign in to comment.