|
| 1 | +""" |
| 2 | +Tests for content library backup (zip export) utilities. |
| 3 | +""" |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import shutil |
| 7 | +import tempfile |
| 8 | +import zipfile |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +import pytest |
| 12 | +from django.test import TestCase |
| 13 | +from opaque_keys.edx.locator import LibraryLocatorV2 |
| 14 | +from path import Path |
| 15 | + |
| 16 | +from openedx.core.djangoapps.content_libraries.api.backup import extract_library_v2_zip_to_dir |
| 17 | + |
| 18 | + |
| 19 | +LIBRARY_KEY = LibraryLocatorV2(org='TestOrg', slug='test-lib') |
| 20 | + |
| 21 | + |
| 22 | +class TestExtractLibraryV2ZipToDir(TestCase): |
| 23 | + """ |
| 24 | + Tests for ``extract_library_v2_zip_to_dir``. |
| 25 | + """ |
| 26 | + |
| 27 | + def _make_zip_in_temp_dir(self, contents=None): |
| 28 | + """ |
| 29 | + Helper: create a real temp dir + zip file and return (temp_dir_path, zip_path). |
| 30 | + ``contents`` is a dict of {filename: bytes} to write into the zip. |
| 31 | + """ |
| 32 | + temp_dir = Path(tempfile.mkdtemp()) |
| 33 | + zip_path = str(temp_dir / 'library.zip') |
| 34 | + with zipfile.ZipFile(zip_path, 'w') as zf: |
| 35 | + for name, data in (contents or {'data.xml': b'<library/>'}).items(): |
| 36 | + zf.writestr(name, data) |
| 37 | + return temp_dir, zip_path |
| 38 | + |
| 39 | + @patch('openedx.core.djangoapps.content_libraries.api.backup.get_user_model') |
| 40 | + @patch('openedx.core.djangoapps.content_libraries.api.backup.create_library_v2_zip') |
| 41 | + def test_successful_extraction(self, mock_create_zip, mock_get_user_model): |
| 42 | + """ |
| 43 | + On a successful call the function should: |
| 44 | + - resolve the username to a user object via the user model, |
| 45 | + - pass that user object to ``create_library_v2_zip``, |
| 46 | + - create the target directory if it does not already exist, |
| 47 | + - extract the zip contents into <root_dir>/<library_dir>, |
| 48 | + - clean up the temporary zip directory. |
| 49 | + """ |
| 50 | + root_dir = Path(tempfile.mkdtemp()) |
| 51 | + temp_zip_dir, zip_path = self._make_zip_in_temp_dir({'content.xml': b'<lib/>'}) |
| 52 | + mock_create_zip.return_value = (temp_zip_dir, zip_path) |
| 53 | + mock_user = MagicMock() |
| 54 | + mock_get_user_model.return_value.objects.filter.return_value.first.return_value = mock_user |
| 55 | + |
| 56 | + try: |
| 57 | + target = root_dir / 'my-library' |
| 58 | + assert not target.exists(), "Target dir should not exist before the call" |
| 59 | + |
| 60 | + extract_library_v2_zip_to_dir(LIBRARY_KEY, str(root_dir), 'my-library', user='testuser') |
| 61 | + |
| 62 | + mock_get_user_model.return_value.objects.filter.assert_called_once_with(username='testuser') |
| 63 | + mock_create_zip.assert_called_once_with(LIBRARY_KEY, mock_user) |
| 64 | + assert target.isdir(), "Target dir should have been created" |
| 65 | + assert (target / 'content.xml').exists(), "Zip content should be extracted" |
| 66 | + assert not temp_zip_dir.exists(), "Temp zip dir should have been removed" |
| 67 | + finally: |
| 68 | + shutil.rmtree(root_dir, ignore_errors=True) |
| 69 | + shutil.rmtree(temp_zip_dir, ignore_errors=True) |
| 70 | + |
| 71 | + @patch('openedx.core.djangoapps.content_libraries.api.backup.get_user_model') |
| 72 | + @patch('openedx.core.djangoapps.content_libraries.api.backup.create_library_v2_zip') |
| 73 | + def test_temp_dir_cleaned_up_even_on_extraction_error(self, mock_create_zip, mock_get_user_model): |
| 74 | + """ |
| 75 | + The temporary directory must be cleaned up even when extraction raises. |
| 76 | + """ |
| 77 | + root_dir = Path(tempfile.mkdtemp()) |
| 78 | + temp_zip_dir, zip_path = self._make_zip_in_temp_dir() |
| 79 | + mock_create_zip.return_value = (temp_zip_dir, zip_path) |
| 80 | + mock_get_user_model.return_value.objects.filter.return_value.first.return_value = None |
| 81 | + |
| 82 | + try: |
| 83 | + with patch('zipfile.ZipFile.extractall', side_effect=OSError('disk full')): |
| 84 | + with pytest.raises(OSError): |
| 85 | + extract_library_v2_zip_to_dir(LIBRARY_KEY, str(root_dir), 'my-library', user=None) |
| 86 | + assert not temp_zip_dir.exists(), "Temp dir should be cleaned up on error" |
| 87 | + finally: |
| 88 | + shutil.rmtree(root_dir, ignore_errors=True) |
| 89 | + shutil.rmtree(temp_zip_dir, ignore_errors=True) |
0 commit comments