diff --git a/cms/djangoapps/contentstore/tests/test_assets.py b/cms/djangoapps/contentstore/tests/test_assets.py index 2f158cfda6..9add306d1d 100644 --- a/cms/djangoapps/contentstore/tests/test_assets.py +++ b/cms/djangoapps/contentstore/tests/test_assets.py @@ -2,7 +2,12 @@ Unit tests for the asset upload endpoint. """ +import os import json +import shutil +import tarfile +import tempfile +from subprocess import call from datetime import datetime from io import BytesIO from pytz import UTC @@ -70,6 +75,78 @@ class UploadTestCase(CourseTestCase): resp = self.client.get(self.url) self.assertEquals(resp.status_code, 405) +class ImportTestCase(CourseTestCase): + """ + Unit tests for importing a course + """ + + def setUp(self): + super(ImportTestCase, self).setUp() + self.url = reverse("import_course", kwargs={ + 'org': self.course.location.org, + 'course': self.course.location.course, + 'name': self.course.location.name, + }) + self.content_dir = tempfile.mkdtemp() + + def touch(name): + """ Equivalent to shell's 'touch'""" + with file(name, 'a'): + os.utime(name, None) + + # Create tar test files + good_dir = tempfile.mkdtemp(dir=self.content_dir) + os.makedirs(os.path.join(good_dir, "course")) + with open(os.path.join(good_dir, "course.xml") , "w+") as f: + f.write('') + + with open(os.path.join(good_dir, "course", "2013_Spring.xml"), "w+") as f: + f.write('') + + + self.good_tar = os.path.join(self.content_dir, "good.tar.gz") + with tarfile.open(self.good_tar, "w:gz") as gtar: + gtar.add(good_dir) + + bad_dir = tempfile.mkdtemp(dir=self.content_dir) + touch(os.path.join(bad_dir, "bad.xml")) + self.bad_tar = os.path.join(self.content_dir, "bad.tar.gz") + with tarfile.open(self.bad_tar, "w:gz") as btar: + btar.add(bad_dir) + + def tearDown(self): + shutil.rmtree(self.content_dir) + + def test_no_coursexml(self): + """ + Check that the response for a tar.gz import without a course.xml is + correct. + """ + with open(self.bad_tar) as btar: + resp = self.client.post( + self.url, + { + "name": self.bad_tar, + "course-data": [btar] + }) + self.assertEquals(resp.status_code, 415) + + def test_with_coursexml(self): + """ + Check that the response for a tar.gz import with a course.xml is + correct. + """ + with open(self.good_tar) as gtar: + resp = self.client.post( + self.url, + { + "name": self.good_tar, + "course-data": [gtar] + }) + self.assert2XX(resp.status_code) + + + class AssetsToJsonTestCase(TestCase): """