def create_module(self, path, name, shortname): """ Creates a module in the database. Will not create Things above module level, and will fail if the path is not unique. """ # find the parent (remove end slash) try: parent = self.thing_list.get(pathid=Thing.hash(path[:-1])) except Thing.DoesNotExist: self.logger.log( 'failed', 'module', 'Could not find path {0}'.format(path[:-1]) ) return except Thing.MultipleObjectsReturned: self.logger.log( 'failed', 'module', 'Path {0} was not unique'.format(path[:-1]) ) return db_module = Thing( fullname=name, type='module', parent=parent, name=shortname ) self.logger.log('insert', 'module', name) db_module.save() return db_module
def setUp(self): # make a user self.user = User( username='******', password='******', email='email' ) self.user.save() allusers = Thing( fullname='All Users', type='user', name='user' ) allusers.save() self.userthing = Thing( name='testuser', type='user', parent=allusers, fullname='A Users Calendar' ) self.userthing.save() # make tripos/api/test alltripos = Thing( fullname='All Tripos', type='tripos', name='tripos' ) alltripos.save() self.tripos = Thing( fullname='api', type='tripos', parent=alltripos, name='api' ) self.tripos.save()
class TestImport(TestCase): def setUp(self): # make a user self.user = User( username='******', password='******', email='email' ) self.user.save() allusers = Thing( fullname='All Users', type='user', name='user' ) allusers.save() self.userthing = Thing( name='testuser', type='user', parent=allusers, fullname='A Users Calendar' ) self.userthing.save() # make tripos/api/test alltripos = Thing( fullname='All Tripos', type='tripos', name='tripos' ) alltripos.save() self.tripos = Thing( fullname='api', type='tripos', parent=alltripos, name='api' ) self.tripos.save() # don't make the part yet # don't give permissions yet def tearDown(self): pass def test_xml_import_permission(self): logger = APILogger() # read the file file_full_path = os.path.join(xml_path, 'test_add.xml') with open(file_full_path) as opened_file: content = opened_file.read() # process the XML processor = processors.XMLImportProcessor(logger=logger) modules = processor.process(content) importer = importers.APIImporter(self.user, logger=logger) # try importing before the part is made for module in modules: importer.add_module_data(module) if logger.was_success(): self.fail("Imported to non-existent part") logger.clear() # make the part now self.part = Thing( fullname='test', type='part', parent=self.tripos, name='test' ) self.part.save() # try importing with no permission for module in modules: importer.add_module_data(module) if logger.was_success(): self.fail("Imported without permission") else: if logger.failed: self.fail("Failed to import data") logger.clear() # now give permissions and try again tag = ThingTag( thing=self.userthing, targetthing=self.part, annotation="admin" ) tag.save() for module in modules: importer.add_module_data(module) if not logger.was_success(): self.fail("Import failed "+logger.summary())