def test_validate_entity_parameters(self): with self.assertRaisesRegexp(utils.ValidationError, 'Invalid entity_id received: 1'): fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, 1) with self.assertRaisesRegexp(utils.ValidationError, 'Entity id cannot be empty'): fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, '') with self.assertRaisesRegexp( utils.ValidationError, 'Invalid entity_name received: ' 'invalid_name.'): fs_domain.DatastoreBackedFileSystem('invalid_name', 'exp_id')
def setUp(self): super(DatastoreBackedFileSystemUnitTests, self).setUp() self.user_email = '*****@*****.**' self.user_id = self.get_user_id_from_email(self.user_email) self.fs = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, 'eid'))
def get(self, exploration_id, asset_type, encoded_filename): """Returns an asset file. Args: exploration_id: str. The id of the exploration. asset_type: str. Type of the asset, either image or audio. encoded_filename: str. The asset filename. This string is encoded in the frontend using encodeURIComponent(). """ if not constants.DEV_MODE: raise self.PageNotFoundException try: filename = urllib.unquote(encoded_filename) file_format = filename[(filename.rfind('.') + 1):] # If the following is not cast to str, an error occurs in the wsgi # library because unicode gets used. self.response.headers['Content-Type'] = str( '%s/%s' % (asset_type, file_format)) fs = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem( fs_domain.ENTITY_TYPE_EXPLORATION, exploration_id)) raw = fs.get('%s/%s' % (asset_type, filename)) self.response.cache_control.no_cache = None self.response.cache_control.public = True self.response.cache_control.max_age = 600 self.response.write(raw) except: raise self.PageNotFoundException
def test_independence_of_file_systems(self): self.fs.commit(self.user_id, 'abc.png', 'file_contents') self.assertEqual(self.fs.get('abc.png'), 'file_contents') fs2 = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, 'eid2')) with self.assertRaisesRegexp(IOError, r'File abc\.png .* not found'): fs2.get('abc.png')
def get(self, page_context, page_identifier, asset_type, encoded_filename): """Returns an asset file. Args: page_context: str. The context of the page where the asset is required. page_identifier: str. The unique identifier for the particular context. Valid page_context: page_identifier pairs: exploration: exp_id story: story_id topic: topic_id skill: skill_id subtopic: topic_name of the topic that it is part of. asset_type: str. Type of the asset, either image or audio. encoded_filename: str. The asset filename. This string is encoded in the frontend using encodeURIComponent(). """ if not constants.DEV_MODE: raise self.PageNotFoundException try: filename = python_utils.urllib_unquote(encoded_filename) file_format = filename[(filename.rfind('.') + 1):] # If the following is not cast to str, an error occurs in the wsgi # library because unicode gets used. self.response.headers[ 'Content-Type'] = python_utils.convert_to_bytes( '%s/%s' % (asset_type, file_format)) if page_context == feconf.ENTITY_TYPE_SUBTOPIC: entity_type = feconf.ENTITY_TYPE_TOPIC topic = topic_fetchers.get_topic_by_name(page_identifier) entity_id = topic.id elif (page_context == feconf.ENTITY_TYPE_EXPLORATION or page_context == feconf.ENTITY_TYPE_SKILL or page_context == feconf.ENTITY_TYPE_TOPIC or page_context == feconf.ENTITY_TYPE_STORY): entity_type = page_context entity_id = page_identifier else: raise self.InvalidInputException fs = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem(entity_type, entity_id)) raw = fs.get('%s/%s' % (asset_type, filename)) self.response.cache_control.no_cache = None self.response.cache_control.public = True self.response.cache_control.max_age = 600 self.response.write(raw) except: raise self.PageNotFoundException
def test_save_original_and_compressed_versions_of_image(self): with open(os.path.join(feconf.TESTS_DATA_DIR, 'img.png')) as f: original_image_content = f.read() fs = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, self.EXPLORATION_ID)) self.assertEqual(fs.isfile('image/%s' % self.FILENAME), False) self.assertEqual( fs.isfile('image/%s' % self.COMPRESSED_IMAGE_FILENAME), False) self.assertEqual(fs.isfile('image/%s' % self.MICRO_IMAGE_FILENAME), False) fs_services.save_original_and_compressed_versions_of_image( self.USER, self.FILENAME, 'exploration', self.EXPLORATION_ID, original_image_content) self.assertEqual(fs.isfile('image/%s' % self.FILENAME), True) self.assertEqual( fs.isfile('image/%s' % self.COMPRESSED_IMAGE_FILENAME), True) self.assertEqual(fs.isfile('image/%s' % self.MICRO_IMAGE_FILENAME), True)
def test_listdir(self): self.fs.commit(self.user_id, 'abc.png', 'file_contents') self.fs.commit(self.user_id, 'abcd.png', 'file_contents_2') self.fs.commit(self.user_id, 'abc/abcd.png', 'file_contents_3') self.fs.commit(self.user_id, 'bcd/bcde.png', 'file_contents_4') self.assertEqual( self.fs.listdir(''), ['abc.png', 'abc/abcd.png', 'abcd.png', 'bcd/bcde.png']) self.assertEqual(self.fs.listdir('abc'), ['abc/abcd.png']) with self.assertRaisesRegexp(IOError, 'Invalid filepath'): self.fs.listdir('/abc') self.assertEqual(self.fs.listdir('fake_dir'), []) new_fs = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, 'eid2')) self.assertEqual(new_fs.listdir('assets'), [])
def test_invalid_filepaths_are_caught(self): fs = fs_domain.AbstractFileSystem( fs_domain.DatastoreBackedFileSystem(feconf.ENTITY_TYPE_EXPLORATION, 'eid')) invalid_filepaths = [ '..', '../another_exploration', '../', '/..', '/abc' ] for filepath in invalid_filepaths: with self.assertRaisesRegexp(IOError, 'Invalid filepath'): fs.isfile(filepath) with self.assertRaisesRegexp(IOError, 'Invalid filepath'): fs.open(filepath) with self.assertRaisesRegexp(IOError, 'Invalid filepath'): fs.get(filepath) with self.assertRaisesRegexp(IOError, 'Invalid filepath'): fs.commit(self.user_id, filepath, 'raw_file') with self.assertRaisesRegexp(IOError, 'Invalid filepath'): fs.delete(self.user_id, filepath) with self.assertRaisesRegexp(IOError, 'Invalid filepath'): fs.listdir(filepath)