Exemplo n.º 1
0
    def test_create_static_asset(self):
        """
        Validate that we can create assets.
        """
        basename = "blah.txt"
        file_contents = b"hello\n"
        with tempfile.TemporaryFile() as temp:
            temp.write(file_contents)
            test_file = File(temp, name=basename)
            asset = api.create_static_asset(self.course.id, test_file)
            self.addCleanup(default_storage.delete, asset.asset)
            self.assertFalse(test_file.closed)
            self.assertEqual(asset.course, self.course)
            self.assertEqual(static_asset_basepath(asset, basename), asset.asset.name)

            # Close file and assert that exception is raised.
            test_file.close()
            test_file.name = "new_name"
            with self.assertRaises(ValueError):
                asset = api.create_static_asset(self.course.id, test_file)
        self.assertEqual(file_contents, asset.asset.read())
Exemplo n.º 2
0
    def setUp(self):
        super(TestExport, self).setUp()

        # Add some LearningResources on top of the default to make things
        # interesting.
        tarball_file = self.get_course_single_tarball()
        import_file(tarball_file, self.repo.id, self.user.id)

        # Add a resource with a '/' in the title and too many characters.
        course = self.repo.course_set.first()
        resource = create_resource(
            course=course,
            resource_type=LearningResourceType.objects.first().name,
            title="//x"*300,
            content_xml="",
            mpath="",
            url_name=None,
            parent=None,
            dpath=''
        )
        update_description_path(resource)

        # Add static assets.
        with TemporaryFile() as temp1:
            with TemporaryFile() as temp2:
                temp1.write(b"file1")
                temp2.write(b"file2")
                file1 = File(temp1, name="iamafile1.txt")
                file2 = File(temp2, name="iamafile2.txt")

                asset1 = create_static_asset(course.id, file1)
                resource.static_assets.add(asset1)

                # If url_name is missing we should just use id in filename.
                resource.url_name = None
                resource.save()

                asset2 = create_static_asset(course.id, file2)
                self.resource.static_assets.add(asset2)
Exemplo n.º 3
0
    def test_create_static_asset(self):
        """
        Validate that we can create assets.
        """
        basename = 'blah.txt'
        file_contents = b'hello\n'
        with tempfile.TemporaryFile() as temp:
            temp.write(file_contents)
            test_file = File(temp, name=basename)
            asset = api.create_static_asset(self.course.id, test_file)
            self.addCleanup(default_storage.delete, asset.asset)
            self.assertFalse(test_file.closed)
            self.assertEqual(asset.course, self.course)
            self.assertEqual(static_asset_basepath(asset, basename),
                             asset.asset.name)

            # Close file and assert that exception is raised.
            test_file.close()
            test_file.name = 'new_name'
            with self.assertRaises(ValueError):
                asset = api.create_static_asset(self.course.id, test_file)
        self.assertEqual(file_contents, asset.asset.read())
Exemplo n.º 4
0
    def setUp(self):
        super(TestExport, self).setUp()

        # Add some LearningResources on top of the default to make things
        # interesting.
        tarball_file = self.get_course_single_tarball()
        import_file(tarball_file, self.repo.id, self.user.id)

        # Add a resource with a '/' in the title and too many characters.
        course = self.repo.course_set.first()
        resource = create_resource(
            course=course,
            resource_type=LearningResourceType.objects.first().name,
            title="//x" * 300,
            content_xml="",
            mpath="",
            url_name=None,
            parent=None,
            dpath='')
        update_description_path(resource)

        # Add static assets.
        with TemporaryFile() as temp1:
            with TemporaryFile() as temp2:
                temp1.write(b"file1")
                temp2.write(b"file2")
                file1 = File(temp1, name="iamafile1.txt")
                file2 = File(temp2, name="iamafile2.txt")

                asset1 = create_static_asset(course.id, file1)
                resource.static_assets.add(asset1)

                # If url_name is missing we should just use id in filename.
                resource.url_name = None
                resource.save()

                asset2 = create_static_asset(course.id, file2)
                self.resource.static_assets.add(asset2)
Exemplo n.º 5
0
    def test_duplicate(self):
        """
        Test that exporting resources with same file path
        is reported and handled correctly.
        """

        with TemporaryFile() as temp1:
            with TemporaryFile() as temp2:
                temp1.write(b"first")
                temp2.write(b"second")

                # Create two static assets with the same name but different
                # paths so no renaming is done on storage side.
                static_filename = "iamafile.txt"
                static_path1 = os.path.join(
                    STATIC_ASSET_BASEPATH.format(
                        org=self.resource.course.org,
                        course_number=self.resource.course.course_number,
                        run=self.resource.course.run,
                    ),
                    static_filename
                )

                file1 = File(temp1, name=static_filename)
                file2 = File(temp2, name=static_filename)

                default_storage.delete(static_path1)
                asset1 = create_static_asset(self.resource.course.id, file1)
                self.resource.static_assets.add(asset1)

                course2 = create_course(
                    "org2", self.repo.id, self.resource.course.course_number,
                    self.resource.course.run, self.user.id
                )
                resource2 = create_resource(
                    course=course2,
                    parent=None,
                    resource_type=self.resource.learning_resource_type.name,
                    title=self.resource.title,
                    dpath="",
                    mpath="",
                    content_xml="",
                    url_name=self.resource.url_name
                )
                static_path2 = os.path.join(
                    STATIC_ASSET_BASEPATH.format(
                        org=course2.org,
                        course_number=course2.course_number,
                        run=course2.run,
                    ),
                    static_filename
                )
                default_storage.delete(static_path2)
                asset2 = create_static_asset(resource2.course.id, file2)
                resource2.static_assets.add(asset2)

                # Export the resources. The second static asset should have
                # the number _1 attached to it.
                try:

                    resources = [self.resource, resource2]
                    tempdir, collision = export_resources_to_directory(
                        resources)
                    try:
                        self.assertTrue(collision)
                        assert_resource_directory(self, resources, tempdir)
                    finally:
                        rmtree(tempdir)
                finally:
                    default_storage.delete(asset1.asset.name)
                    default_storage.delete(asset2.asset.name)
Exemplo n.º 6
0
    def test_duplicate(self):
        """
        Test that exporting resources with same file path
        is reported and handled correctly.
        """

        with TemporaryFile() as temp1:
            with TemporaryFile() as temp2:
                temp1.write(b"first")
                temp2.write(b"second")

                # Create two static assets with the same name but different
                # paths so no renaming is done on storage side.
                static_filename = "iamafile.txt"
                static_path1 = os.path.join(
                    STATIC_ASSET_BASEPATH.format(
                        org=self.resource.course.org,
                        course_number=self.resource.course.course_number,
                        run=self.resource.course.run,
                    ), static_filename)

                file1 = File(temp1, name=static_filename)
                file2 = File(temp2, name=static_filename)

                default_storage.delete(static_path1)
                asset1 = create_static_asset(self.resource.course.id, file1)
                self.resource.static_assets.add(asset1)

                course2 = create_course("org2", self.repo.id,
                                        self.resource.course.course_number,
                                        self.resource.course.run, self.user.id)
                resource2 = create_resource(
                    course=course2,
                    parent=None,
                    resource_type=self.resource.learning_resource_type.name,
                    title=self.resource.title,
                    dpath="",
                    mpath="",
                    content_xml="",
                    url_name=self.resource.url_name)
                static_path2 = os.path.join(
                    STATIC_ASSET_BASEPATH.format(
                        org=course2.org,
                        course_number=course2.course_number,
                        run=course2.run,
                    ), static_filename)
                default_storage.delete(static_path2)
                asset2 = create_static_asset(resource2.course.id, file2)
                resource2.static_assets.add(asset2)

                # Export the resources. The second static asset should have
                # the number _1 attached to it.
                try:

                    resources = [self.resource, resource2]
                    tempdir, collision = export_resources_to_directory(
                        resources)
                    try:
                        self.assertTrue(collision)
                        assert_resource_directory(self, resources, tempdir)
                    finally:
                        rmtree(tempdir)
                finally:
                    default_storage.delete(asset1.asset.name)
                    default_storage.delete(asset2.asset.name)