library.py 2.98 KB
Newer Older
1 2 3 4 5 6
"""
Fixture to create a Content Library
"""

from opaque_keys.edx.keys import CourseKey

7
from common.test.acceptance.fixtures import STUDIO_BASE_URL
8
from common.test.acceptance.fixtures.base import FixtureError, XBlockContainerFixture
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29


class LibraryFixture(XBlockContainerFixture):
    """
    Fixture for ensuring that a library exists.

    WARNING: This fixture is NOT idempotent.  To avoid conflicts
    between tests, you should use unique library identifiers for each fixture.
    """

    def __init__(self, org, number, display_name):
        """
        Configure the library fixture to create a library with
        """
        super(LibraryFixture, self).__init__()
        self.library_info = {
            'org': org,
            'number': number,
            'display_name': display_name
        }

30
        self.display_name = display_name
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        self._library_key = None
        super(LibraryFixture, self).__init__()

    def __str__(self):
        """
        String representation of the library fixture, useful for debugging.
        """
        return "<LibraryFixture: org='{org}', number='{number}'>".format(**self.library_info)

    def install(self):
        """
        Create the library and XBlocks within the library.
        This is NOT an idempotent method; if the library already exists, this will
        raise a `FixtureError`.  You should use unique library identifiers to avoid
        conflicts between tests.
        """
        self._create_library()
        self._create_xblock_children(self.library_location, self.children)

        return self

    @property
    def library_key(self):
        """
        Get the LibraryLocator for this library, as a string.
        """
        return self._library_key

    @property
    def library_location(self):
        """
        Return the locator string for the LibraryRoot XBlock that is the root of the library hierarchy.
        """
        lib_key = CourseKey.from_string(self._library_key)
        return unicode(lib_key.make_usage_key('library', 'library'))

    def _create_library(self):
        """
        Create the library described in the fixture.
        Will fail if the library already exists.
        """
        response = self.session.post(
            STUDIO_BASE_URL + '/library/',
            data=self._encode_post_dict(self.library_info),
            headers=self.headers
        )

        if response.ok:
            self._library_key = response.json()['library_key']
        else:
            try:
                err_msg = response.json().get('ErrMsg')
            except ValueError:
                err_msg = "Unknown Error"
E. Kolpakov committed
85 86 87
            raise FixtureError("Could not create library {}. Status was {}, error was: {}".format(
                self.library_info, response.status_code, err_msg
            ))
88 89 90 91 92 93

    def create_xblock(self, parent_loc, xblock_desc):
        # Disable publishing for library XBlocks:
        xblock_desc.publish = "not-applicable"

        return super(LibraryFixture, self).create_xblock(parent_loc, xblock_desc)