def test_publish_dev_with_tag(self, enqueuer): brl = BRLBlock('owner/user/block/branch') store = Mock(MongoServerStore) store.read_block_permissions = Mock( return_value=ElementPermissions(brl, private=False)) user = Mock() user.blocks = {} store.read_user = Mock(return_value=user) block = Mock(Block) block.add_publication.return_value = (['mock_id'], [], [], []) block.deltas = [] ensure = Security('authUser', store) ensure.check_read_block = Mock(return_value=True) ensure.check_create_block = Mock(return_value=True) ensure.check_write_block = Mock(return_value=True) ensure.check_publish_block = Mock(return_value=True) store.read_block.return_value = block store.read_published_cells.return_value = {} p = PublishService(store, 'authUser') p.security = ensure pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.deptable = BlockVersionTable() p.publish(pack)
def test_publish_dev_with_tag(self, enqueuer): brl = BRLBlock('owner/user/block/branch') store = Mock(MongoServerStore) store.read_block_permissions = Mock(return_value=ElementPermissions(brl, private=False)) user = Mock() user.blocks = {} store.read_user = Mock(return_value=user) block = Mock(Block) block.add_publication.return_value = (['mock_id'], [], [], []) block.deltas = [] ensure = Security('authUser', store) ensure.check_read_block = Mock(return_value=True) ensure.check_create_block = Mock(return_value=True) ensure.check_write_block = Mock(return_value=True) ensure.check_publish_block = Mock(return_value=True) store.read_block.return_value = block store.read_published_cells.return_value = {} p = PublishService(store, 'authUser') p.security = ensure pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.deptable = BlockVersionTable() p.publish(pack)
def test_publish_no_contents(self, enqueuer): brl = BRLBlock('owner/user/block/branch') store = Mock(MongoServerStore) store.read_block_permissions = Mock(return_value=ElementPermissions(brl, private=False)) p = PublishService(store, 'authUser') pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('hola')) pack.versiontag = 'mytag' with self.assertRaisesRegexp(PublishException, 'DEV'): p.publish(pack)
def test_publish_no_contents(self, enqueuer): brl = BRLBlock('owner/user/block/branch') store = Mock(MongoServerStore) store.read_block_permissions = Mock( return_value=ElementPermissions(brl, private=False)) p = PublishService(store, 'authUser') pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('hola')) pack.versiontag = 'mytag' with self.assertRaisesRegexp(PublishException, 'DEV'): p.publish(pack)
def get_version_info_test(self): store = Mock(GenericServerStore) # Block doesn't exist and user is other p = PublishService(store, 'any_user') block_brl = BRLBlock("user/user/theblock/master") store.read_block_permissions = Mock(side_effect=NotInStoreException()) block_info = p.get_block_info(block_brl) self.assertFalse(block_info.can_write) # Now block exists store.read_block_permissions = Mock(return_value=ElementPermissions(block_brl)) # Block exists and user is the authorized one p = PublishService(store, 'theuser') block_brl = BRLBlock("theuser/theuser/theblock/master") block_info = p.get_block_info(block_brl) self.assertTrue(block_info.can_write) # No authorized write to an existing block p = PublishService(store, 'wronguser') user = Mock(User) user.administrators = Permissions() store.read_user = Mock(return_value=user) block_info = p.get_block_info(block_brl) self.assertFalse(block_info.can_write) # Authorized user with an existing block p = PublishService(store, 'theuser') block_brl = BRLBlock("theuser/theuser/theblock/master") block_info = p.get_block_info(block_brl) self.assertTrue(block_info.can_write)
def test_publish(self, enqueuer): brl = BRLBlock('owner/user/block/branch') # moduleID=BlockID(UserID(123),456) store = Mock(MongoServerStore) store.read_block_permissions = Mock(return_value=ElementPermissions(brl, private=False)) user = User("owner") user.numeric_id = 1 user.blocks = {} store.read_user = Mock(return_value=user) block = Mock(Block) block.last_version.return_value = Mock(BlockVersion) block.add_publication.return_value = (Mock(list), Mock(list), Mock(list), Mock(list)) block.deltas = [] ensure = Security('authUser', store) ensure.check_create_block = Mock(return_value=True) ensure.check_write_block = Mock(return_value=True) ensure.check_read_block = Mock(return_value=True) store.read_block.return_value = block store.read_published_cells.return_value = {} p = PublishService(store, 'authUser') p.security = ensure pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('hola')) pack.cells.append(SimpleCell('user/block/r2.h')) pack.contents['r2.h'] = Content(id_=None, load=Blob('hola')) pack.cells.append(SimpleCell('user/block/r3.h')) pack.contents['r3.h'] = Content(id_=None, load=Blob('hola')) pack.deptable = BlockVersionTable() p.publish(pack) block.add_publication.assert_called_once_with(pack, p.auth_user) store.update_block.assert_called_once_with(block) self.assertEqual(1, store.create_published_cells.call_count) self.assertEqual(1, store.create_published_contents.call_count) # Check sizes self.assertEquals(user.blocks_bytes, 12) # 12 bytes "hola" * 3 # Publish again, see the size incremented pack._bytes = None # Lazy computed p.publish(pack) self.assertEquals(user.blocks_bytes, 24) # 24 bytes: "hola" * 3 * 2 publications # Max size exceeded for user user.max_workspace_size = 25 self.assertRaises(ForbiddenException, p.publish, pack) # Try to publish only 1 byte pack._bytes = None # Lazy computed pack.cells = [] pack.contents = {} pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('h')) p.publish(pack)
def test_publish_rejected(self): store = Mock(MongoServerStore) user = Mock() user.blocks = {} store.read_user = Mock(return_value=user) block = Mock(Block) block.add_publication.return_value = (Mock(), Mock()) store.read_block.return_value = block brl = BRLBlock('user/user/block/branch') p = PublishService(store, 'authUser') pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('hola')) pack.deptable = BlockVersionTable() with self.assertRaises(ForbiddenException): p.publish(pack)
def setUp(self): self.conn.drop_database(self._testMethodName) self.store = MongoServerStore(self.conn, self._testMethodName) self.mother = ModelCreator(self.store) self.user = self.mother.make_test_user() self.p = PublishService(self.store, self.user.ID) self.brl = BRLBlock('%s/%s/block/master' % (self.user.ID, self.user.ID)) pack = PublishRequest(BlockVersion(self.brl, -1)) pack.cells.append(SimpleCell('%s/block/r1.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r2.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r3.h' % self.user.ID)) pack.contents['r1.h'] = Content(id_=None, load=Blob('r1')) pack.contents['r2.h'] = Content(id_=None, load=Blob('r2')) pack.contents['r3.h'] = Content(id_=None, load=Blob('r3')) pack.deptable = BlockVersionTable() self.pack = pack self.cell_collection = self.store.db[ GenericServerStore.PUBLISHED_CELL_ST] self.content_collection = self.store.db[ GenericServerStore.PUBLISHED_CONTENT_ST] self.block_collection = self.store.db[GenericServerStore.BLOCK_ST]
def setUp(self): self.conn.drop_database(self._testMethodName) self.store = MongoServerStore(self.conn, self._testMethodName) self.mother = ModelCreator(self.store) self.user = self.mother.make_test_user() self.p = PublishService(self.store, self.user.ID) self.brl = BRLBlock('%s/%s/block/master' % (self.user.ID, self.user.ID)) pack = PublishRequest(BlockVersion(self.brl, -1)) pack.cells.append(SimpleCell('%s/block/r1.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r2.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r3.h' % self.user.ID)) pack.contents['r1.h'] = Content(id_=None, load=Blob('r1')) pack.contents['r2.h'] = Content(id_=None, load=Blob('r2')) pack.contents['r3.h'] = Content(id_=None, load=Blob('r3')) pack.deptable = BlockVersionTable() self.pack = pack self.cell_collection = self.store.db[GenericServerStore.PUBLISHED_CELL_ST] self.content_collection = self.store.db[GenericServerStore.PUBLISHED_CONTENT_ST] self.block_collection = self.store.db[GenericServerStore.BLOCK_ST]
class PublishTransactionTest(TestWithMongo): _multiprocess_shared_ = True def setUp(self): self.conn.drop_database(self._testMethodName) self.store = MongoServerStore(self.conn, self._testMethodName) self.mother = ModelCreator(self.store) self.user = self.mother.make_test_user() self.p = PublishService(self.store, self.user.ID) self.brl = BRLBlock('%s/%s/block/master' % (self.user.ID, self.user.ID)) pack = PublishRequest(BlockVersion(self.brl, -1)) pack.cells.append(SimpleCell('%s/block/r1.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r2.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r3.h' % self.user.ID)) pack.contents['r1.h'] = Content(id_=None, load=Blob('r1')) pack.contents['r2.h'] = Content(id_=None, load=Blob('r2')) pack.contents['r3.h'] = Content(id_=None, load=Blob('r3')) pack.deptable = BlockVersionTable() self.pack = pack self.cell_collection = self.store.db[ GenericServerStore.PUBLISHED_CELL_ST] self.content_collection = self.store.db[ GenericServerStore.PUBLISHED_CONTENT_ST] self.block_collection = self.store.db[GenericServerStore.BLOCK_ST] def tearDown(self): print 'tearDown %s' % self._testMethodName self.conn.drop_database(self._testMethodName) def test_publish_success(self): self.p.publish(self.pack) blocks = list(self.block_collection.find()) cells = list(self.cell_collection.find()) contents = list(self.content_collection.find()) self.assertEqual(3, len(contents)) self.assertEqual(3, len(cells)) self.assertEquals(1, len(blocks)) self.assertNotransactionPending() def test_roll_back(self): self.p._write_resources_to_db = Mock( side_effect=Exception('Branches update failed')) cells_no = len(list(self.cell_collection.find())) contents_no = len(list(self.content_collection.find())) #blocks_no = len(list(self.block_collection.find())) #old_block = self.store.read_block(self.brl) with self.assertRaises(ServerInternalErrorException): self.p.publish(self.pack) # TODO: Test that block creation is rolled back, now it isnt # new_block = self.store.read_block(self.brl) #self.assertEqual(old_block, new_block) cells = list(self.cell_collection.find()) contents = list(self.content_collection.find()) #blocks = list(self.block_collection.find()) self.assertEqual(cells_no, len(cells)) self.assertEqual(contents_no, len(contents)) #self.assertEqual(blocks_no, len(blocks)) self.assertNotransactionPending() def assertNotransactionPending(self): dbcol = self.store.db[self.store.BLOCK_TRANSACTIONS] transaction = dbcol.find_one({'_id': str(self.brl)}) self.assertTrue(transaction is None)
def get_block_info(self, brl_block): ''' Read the block and get a BlockInfo object''' p = PublishService(self._store, self._auth_user) return p.get_block_info(brl_block)
def publish(self, publish_request): ''' Publish in bii server''' p = PublishService(self._store, self._auth_user) return p.publish(publish_request)
class PublishTransactionTest(TestWithMongo): _multiprocess_shared_ = True def setUp(self): self.conn.drop_database(self._testMethodName) self.store = MongoServerStore(self.conn, self._testMethodName) self.mother = ModelCreator(self.store) self.user = self.mother.make_test_user() self.p = PublishService(self.store, self.user.ID) self.brl = BRLBlock('%s/%s/block/master' % (self.user.ID, self.user.ID)) pack = PublishRequest(BlockVersion(self.brl, -1)) pack.cells.append(SimpleCell('%s/block/r1.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r2.h' % self.user.ID)) pack.cells.append(SimpleCell('%s/block/r3.h' % self.user.ID)) pack.contents['r1.h'] = Content(id_=None, load=Blob('r1')) pack.contents['r2.h'] = Content(id_=None, load=Blob('r2')) pack.contents['r3.h'] = Content(id_=None, load=Blob('r3')) pack.deptable = BlockVersionTable() self.pack = pack self.cell_collection = self.store.db[GenericServerStore.PUBLISHED_CELL_ST] self.content_collection = self.store.db[GenericServerStore.PUBLISHED_CONTENT_ST] self.block_collection = self.store.db[GenericServerStore.BLOCK_ST] def tearDown(self): print 'tearDown %s' % self._testMethodName self.conn.drop_database(self._testMethodName) def test_publish_success(self): self.p.publish(self.pack) blocks = list(self.block_collection.find()) cells = list(self.cell_collection.find()) contents = list(self.content_collection.find()) self.assertEqual(3, len(contents)) self.assertEqual(3, len(cells)) self.assertEquals(1, len(blocks)) self.assertNotransactionPending() def test_roll_back(self): self.p._write_resources_to_db = Mock(side_effect=Exception('Branches update failed')) cells_no = len(list(self.cell_collection.find())) contents_no = len(list(self.content_collection.find())) #blocks_no = len(list(self.block_collection.find())) #old_block = self.store.read_block(self.brl) with self.assertRaises(ServerInternalErrorException): self.p.publish(self.pack) # TODO: Test that block creation is rolled back, now it isnt # new_block = self.store.read_block(self.brl) #self.assertEqual(old_block, new_block) cells = list(self.cell_collection.find()) contents = list(self.content_collection.find()) #blocks = list(self.block_collection.find()) self.assertEqual(cells_no, len(cells)) self.assertEqual(contents_no, len(contents)) #self.assertEqual(blocks_no, len(blocks)) self.assertNotransactionPending() def assertNotransactionPending(self): dbcol = self.store.db[self.store.BLOCK_TRANSACTIONS] transaction = dbcol.find_one({'_id': str(self.brl)}) self.assertTrue(transaction is None)
def test_publish(self, enqueuer): brl = BRLBlock('owner/user/block/branch') # moduleID=BlockID(UserID(123),456) store = Mock(MongoServerStore) store.read_block_permissions = Mock( return_value=ElementPermissions(brl, private=False)) user = User("owner") user.numeric_id = 1 user.blocks = {} store.read_user = Mock(return_value=user) block = Mock(Block) block.last_version.return_value = Mock(BlockVersion) block.add_publication.return_value = (Mock(list), Mock(list), Mock(list), Mock(list)) block.deltas = [] ensure = Security('authUser', store) ensure.check_create_block = Mock(return_value=True) ensure.check_write_block = Mock(return_value=True) ensure.check_read_block = Mock(return_value=True) store.read_block.return_value = block store.read_published_cells.return_value = {} p = PublishService(store, 'authUser') p.security = ensure pack = PublishRequest(BlockVersion(brl, -1)) pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('hola')) pack.cells.append(SimpleCell('user/block/r2.h')) pack.contents['r2.h'] = Content(id_=None, load=Blob('hola')) pack.cells.append(SimpleCell('user/block/r3.h')) pack.contents['r3.h'] = Content(id_=None, load=Blob('hola')) pack.deptable = BlockVersionTable() p.publish(pack) block.add_publication.assert_called_once_with(pack, p.auth_user) store.update_block.assert_called_once_with(block) self.assertEqual(1, store.create_published_cells.call_count) self.assertEqual(1, store.create_published_contents.call_count) # Check sizes self.assertEquals(user.blocks_bytes, 12) # 12 bytes "hola" * 3 # Publish again, see the size incremented pack._bytes = None # Lazy computed p.publish(pack) self.assertEquals(user.blocks_bytes, 24) # 24 bytes: "hola" * 3 * 2 publications # Max size exceeded for user user.max_workspace_size = 25 self.assertRaises(ForbiddenException, p.publish, pack) # Try to publish only 1 byte pack._bytes = None # Lazy computed pack.cells = [] pack.contents = {} pack.cells.append(SimpleCell('user/block/r1.h')) pack.contents['r1.h'] = Content(id_=None, load=Blob('h')) p.publish(pack)
def get_version_info_test(self): store = Mock(GenericServerStore) # Block doesn't exist and user is other p = PublishService(store, 'any_user') block_brl = BRLBlock("user/user/theblock/master") store.read_block_permissions = Mock(side_effect=NotInStoreException()) block_info = p.get_block_info(block_brl) self.assertFalse(block_info.can_write) # Now block exists store.read_block_permissions = Mock( return_value=ElementPermissions(block_brl)) # Block exists and user is the authorized one p = PublishService(store, 'theuser') block_brl = BRLBlock("theuser/theuser/theblock/master") block_info = p.get_block_info(block_brl) self.assertTrue(block_info.can_write) # No authorized write to an existing block p = PublishService(store, 'wronguser') user = Mock(User) user.administrators = Permissions() store.read_user = Mock(return_value=user) block_info = p.get_block_info(block_brl) self.assertFalse(block_info.can_write) # Authorized user with an existing block p = PublishService(store, 'theuser') block_brl = BRLBlock("theuser/theuser/theblock/master") block_info = p.get_block_info(block_brl) self.assertTrue(block_info.can_write)