def test_static_import_integration(self): """ Do integration test to validate course import with static assets. """ original_count = Course.objects.count() tarball_file = self.get_course_single_tarball() import_file( tarball_file, self.repo.id, self.user.id) self.assertEqual( Course.objects.count(), original_count + 1, ) self.assertFalse(os.path.exists(tarball_file)) course = Course.objects.all().exclude(id=self.course.id) self.assertEqual(course.count(), 1) assets = StaticAsset.objects.filter(course=course) for asset in assets: self.addCleanup(default_storage.delete, asset.asset) self.assertEqual(assets.count(), self.toy_asset_count) for asset in assets: base_path = static_asset_basepath(asset, '') self.assertIn( asset.asset.name.replace(base_path, ''), [ 'test.txt', 'subdir/subtext.txt', 'subs_CCxmtcICYNc.srt.sjson', 'essays_x250.png', 'webGLDemo.css', ] )
def test_static_asset_filename_length(self): """ Tests that user can use django FileField long up to FILE_PATH_MAX_LENGTH characters """ temp_dir_path = mkdtemp() self.addCleanup(rmtree, temp_dir_path) # test with small file name file_path = create_dumb_file(temp_dir_path, 10) with open(file_path) as file_handle: StaticAsset.objects.create(course=self.course, asset=File(file_handle)) # test with file name of exactly the max length base_path = static_asset_basepath(MockAsset(self.course), '') file_path = create_dumb_file( temp_dir_path, ( # max file size FILE_PATH_MAX_LENGTH - # minus the length of the base path of the Django storage and # the base path of the file location temporary dir len('/'.join([base_path, temp_dir_path])) - # minus 1 for the extra "/" to joint the paths 1)) with open(file_path) as file_handle: StaticAsset.objects.create(course=self.course, asset=File(file_handle)) # test with file name of more the max length file_path = create_dumb_file(temp_dir_path, FILE_PATH_MAX_LENGTH + 1) with open(file_path) as file_handle: self.assertRaises( FilePathLengthException, lambda: StaticAsset.objects.create(course=self.course, asset=File(file_handle)))
def test_import_static_recurse(self): """ Verify walking a folder of assets and verifying they get added """ temp_dir_path = mkdtemp() self.addCleanup(rmtree, temp_dir_path) basename = 'blah.txt' file_contents = 'hello\n' # Create folder and additional directory to verify recursion. subdir_name = 'testdir' os.mkdir(os.path.join(temp_dir_path, subdir_name)) with open( os.path.join(temp_dir_path, subdir_name, basename), 'w' ) as temp: temp.write(file_contents) # All setup, now import import_static_assets(self.course, temp_dir_path) assets = StaticAsset.objects.filter(course=self.course) self.assertEqual(assets.count(), 1) asset = assets[0] dummy = mock.MagicMock() dummy.course = self.course self.assertEqual( asset.asset.name, static_asset_basepath(dummy, os.path.join(subdir_name, basename)) ) self.addCleanup(default_storage.delete, asset.asset)
def test_static_import_integration(self): """ Do integration test to validate course import with static assets. """ original_count = Course.objects.count() tarball_file = self.get_course_single_tarball() import_file( tarball_file, self.repo.id, self.user.id) self.assertEqual( Course.objects.count(), original_count + 1, ) self.assertFalse(os.path.exists(tarball_file)) course = Course.objects.all().exclude(id=self.course.id) self.assertEqual(course.count(), 1) assets = StaticAsset.objects.filter(course=course) for asset in assets: self.addCleanup(default_storage.delete, asset.asset) self.assertEqual(assets.count(), self.toy_asset_count) for asset in assets: base_path = static_asset_basepath(asset, '') self.assertIn( asset.asset.name.replace(base_path, ''), [ 'test.txt', 'subdir/subtext.txt', 'subs_CCxmtcICYNc.srt.sjson', 'essays_x250.png', 'webGLDemo.css', ] )
def test_import_static_recurse(self): """ Verify walking a folder of assets and verifying they get added """ temp_dir_path = mkdtemp() self.addCleanup(rmtree, temp_dir_path) basename = 'blah.txt' file_contents = 'hello\n' # Create folder and additional directory to verify recursion. subdir_name = 'testdir' os.mkdir(os.path.join(temp_dir_path, subdir_name)) with open( os.path.join(temp_dir_path, subdir_name, basename), 'w' ) as temp: temp.write(file_contents) # All setup, now import import_static_assets(self.course, temp_dir_path) assets = StaticAsset.objects.filter(course=self.course) self.assertEqual(assets.count(), 1) asset = assets[0] dummy = mock.MagicMock() dummy.course = self.course self.assertEqual( asset.asset.name, static_asset_basepath(dummy, os.path.join(subdir_name, basename)) ) self.addCleanup(default_storage.delete, asset.asset)
def test_static_asset_basepath(self): """Verify we are setting the path we expect""" filename = 'asdf/asdf.txt' asset = MagicMock() asset.course.org = 'hi' asset.course.course_number = '1' asset.course.run = 'runnow' path = static_asset_basepath(asset, filename) self.assertEqual(path, 'assets/hi/1/runnow/asdf/asdf.txt')
def test_static_asset_basepath(self): """Verify we are setting the path we expect""" filename = "asdf/asdf.txt" asset = MagicMock() asset.course.org = "hi" asset.course.course_number = "1" asset.course.run = "runnow" path = static_asset_basepath(asset, filename) self.assertEqual(path, "assets/hi/1/runnow/asdf/asdf.txt")
def test_static_asset_basepath(self): """Verify we are setting the path we expect""" filename = 'asdf/asdf.txt' asset = MagicMock() asset.course.org = 'hi' asset.course.course_number = '1' asset.course.run = 'runnow' path = static_asset_basepath(asset, filename) self.assertEqual( path, 'assets/hi/1/runnow/asdf/asdf.txt' )
def test_static_asset_filename_length(self): """ Tests that user can use django FileField long up to FILE_PATH_MAX_LENGTH characters """ temp_dir_path = mkdtemp() self.addCleanup(rmtree, temp_dir_path) # test with small file name file_path = create_dumb_file(temp_dir_path, 10) with open(file_path) as file_handle: StaticAsset.objects.create( course=self.course, asset=File(file_handle) ) # test with file name of exactly the max length base_path = static_asset_basepath(MockAsset(self.course), '') file_path = create_dumb_file( temp_dir_path, ( # max file size FILE_PATH_MAX_LENGTH - # minus the length of the base path of the Django storage and # the base path of the file location temporary dir len('/'.join([base_path, temp_dir_path])) - # minus 1 for the extra "/" to joint the paths 1 ) ) with open(file_path) as file_handle: StaticAsset.objects.create( course=self.course, asset=File(file_handle) ) # test with file name of more the max length file_path = create_dumb_file(temp_dir_path, FILE_PATH_MAX_LENGTH+1) with open(file_path) as file_handle: self.assertRaises( FilePathLengthException, lambda: StaticAsset.objects.create( course=self.course, asset=File(file_handle) ) )
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())
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())