<s> import unittest <EOL> from mock import Mock <EOL> from biicode . server . publish . publish_service import PublishService <EOL> from biicode . common . model . brl . brl_block import BRLBlock <EOL> from biicode . common . model . symbolic . block_version import BlockVersion <EOL> from biicode . common . model . cells import SimpleCell <EOL> from biicode . common . model . content import Content <EOL> from biicode . common . exception import NotInStoreException , BiiException <EOL> from biicode . server . authorize import Security <EOL> from biicode . server . model . permissions . permissions import Permissions <EOL> from biicode . server . model . permissions . element_permissions import ElementPermissions <EOL> from biicode . server . store . generic_server_store import GenericServerStore <EOL> from biicode . server . store . mongo_server_store import MongoServerStore <EOL> from biicode . server . model . block import Block <EOL> from mock import patch <EOL> from biicode . common . publish . publish_request import PublishRequest <EOL> from biicode . common . model . symbolic . block_version_table import BlockVersionTable <EOL> from biicode . server . model . user import User <EOL> from biicode . test . testing_mem_server_store import TestingMemServerStore <EOL> from biicode . server . api . bii_service import BiiService <EOL> from biicode . common . model . blob import Blob <EOL> from biicode . common . model . version_tag import DEV <EOL> from biicode . common . exception import PublishException , ForbiddenException <EOL> from biicode . server . background . enqueuer import Enqueuer <EOL> class PublishServiceTest ( unittest . TestCase ) : <EOL> def test_publish_rejected ( self ) : <EOL> store = Mock ( MongoServerStore ) <EOL> user = Mock ( ) <EOL> user . blocks = { } <EOL> store . read_user = Mock ( return_value = user ) <EOL> block = Mock ( Block ) <EOL> block . add_publication . return_value = ( Mock ( ) , Mock ( ) ) <EOL> store . read_block . return_value = block <EOL> brl = BRLBlock ( '<STR_LIT>' ) <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> pack = PublishRequest ( BlockVersion ( brl , - <NUM_LIT:1> ) ) <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> pack . deptable = BlockVersionTable ( ) <EOL> with self . assertRaises ( ForbiddenException ) : <EOL> p . publish ( pack ) <EOL> @ patch . object ( Enqueuer , "<STR_LIT>" ) <EOL> def test_publish ( self , enqueuer ) : <EOL> brl = BRLBlock ( '<STR_LIT>' ) <EOL> store = Mock ( MongoServerStore ) <EOL> store . read_block_permissions = Mock ( return_value = ElementPermissions ( brl , private = False ) ) <EOL> user = User ( "<STR_LIT>" ) <EOL> user . numeric_id = <NUM_LIT:1> <EOL> user . blocks = { } <EOL> store . read_user = Mock ( return_value = user ) <EOL> block = Mock ( Block ) <EOL> block . last_version . return_value = Mock ( BlockVersion ) <EOL> block . add_publication . return_value = ( Mock ( list ) , Mock ( list ) , Mock ( list ) , Mock ( list ) ) <EOL> block . deltas = [ ] <EOL> ensure = Security ( '<STR_LIT>' , store ) <EOL> ensure . check_create_block = Mock ( return_value = True ) <EOL> ensure . check_write_block = Mock ( return_value = True ) <EOL> ensure . check_read_block = Mock ( return_value = True ) <EOL> store . read_block . return_value = block <EOL> store . read_published_cells . return_value = { } <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> p . security = ensure <EOL> pack = PublishRequest ( BlockVersion ( brl , - <NUM_LIT:1> ) ) <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> pack . deptable = BlockVersionTable ( ) <EOL> p . publish ( pack ) <EOL> block . add_publication . assert_called_once_with ( pack , p . auth_user ) <EOL> store . update_block . assert_called_once_with ( block ) <EOL> self . assertEqual ( <NUM_LIT:1> , store . create_published_cells . call_count ) <EOL> self . assertEqual ( <NUM_LIT:1> , store . create_published_contents . call_count ) <EOL> self . assertEquals ( user . blocks_bytes , <NUM_LIT:12> ) <EOL> pack . _bytes = None <EOL> p . publish ( pack ) <EOL> self . assertEquals ( user . blocks_bytes , <NUM_LIT> ) <EOL> user . max_workspace_size = <NUM_LIT> <EOL> self . assertRaises ( ForbiddenException , p . publish , pack ) <EOL> pack . _bytes = None <EOL> pack . cells = [ ] <EOL> pack . contents = { } <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT:h>' ) ) <EOL> p . publish ( pack ) <EOL> @ patch . object ( Enqueuer , "<STR_LIT>" ) <EOL> def test_publish_no_contents ( self , enqueuer ) : <EOL> brl = BRLBlock ( '<STR_LIT>' ) <EOL> store = Mock ( MongoServerStore ) <EOL> store . read_block_permissions = Mock ( return_value = ElementPermissions ( brl , private = False ) ) <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> pack = PublishRequest ( BlockVersion ( brl , - <NUM_LIT:1> ) ) <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> pack . versiontag = '<STR_LIT>' <EOL> with self . assertRaisesRegexp ( PublishException , '<STR_LIT>' ) : <EOL> p . publish ( pack ) <EOL> @ patch . object ( Enqueuer , "<STR_LIT>" ) <EOL> def test_publish_dev_with_tag ( self , enqueuer ) : <EOL> brl = BRLBlock ( '<STR_LIT>' ) <EOL> store = Mock ( MongoServerStore ) <EOL> store . read_block_permissions = Mock ( return_value = ElementPermissions ( brl , private = False ) ) <EOL> user = Mock ( ) <EOL> user . blocks = { } <EOL> store . read_user = Mock ( return_value = user ) <EOL> block = Mock ( Block ) <EOL> block . add_publication . return_value = ( [ '<STR_LIT>' ] , [ ] , [ ] , [ ] ) <EOL> block . deltas = [ ] <EOL> ensure = Security ( '<STR_LIT>' , store ) <EOL> ensure . check_read_block = Mock ( return_value = True ) <EOL> ensure . check_create_block = Mock ( return_value = True ) <EOL> ensure . check_write_block = Mock ( return_value = True ) <EOL> ensure . check_publish_block = Mock ( return_value = True ) <EOL> store . read_block . return_value = block <EOL> store . read_published_cells . return_value = { } <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> p . security = ensure <EOL> pack = PublishRequest ( BlockVersion ( brl , - <NUM_LIT:1> ) ) <EOL> pack . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> pack . deptable = BlockVersionTable ( ) <EOL> p . publish ( pack ) <EOL> def test_publish_concurrent_modification ( self ) : <EOL> self . store = TestingMemServerStore ( ) <EOL> brl = '<STR_LIT:user>' <EOL> self . store . create_user ( User ( brl ) ) <EOL> self . service = BiiService ( self . store , brl ) <EOL> self . brl_block = BRLBlock ( '<STR_LIT>' ) <EOL> request = PublishRequest ( BlockVersion ( self . brl_block , - <NUM_LIT:1> ) ) <EOL> request . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> request . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> request . deptable = BlockVersionTable ( ) <EOL> request . tag = DEV <EOL> self . service . publish ( request ) <EOL> '''<STR_LIT>''' <EOL> self . brl_block1 = BRLBlock ( '<STR_LIT>' ) <EOL> request = PublishRequest ( BlockVersion ( self . brl_block , <NUM_LIT:0> ) ) <EOL> request . cells . append ( SimpleCell ( '<STR_LIT>' ) ) <EOL> request . contents [ '<STR_LIT>' ] = Content ( id_ = None , load = Blob ( '<STR_LIT>' ) ) <EOL> request . deptable = BlockVersionTable ( ) <EOL> with self . assertRaisesRegexp ( BiiException , '<STR_LIT>' ) : <EOL> self . service . publish ( request ) <EOL> def get_version_info_test ( self ) : <EOL> store = Mock ( GenericServerStore ) <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> block_brl = BRLBlock ( "<STR_LIT>" ) <EOL> store . read_block_permissions = Mock ( side_effect = NotInStoreException ( ) ) <EOL> block_info = p . get_block_info ( block_brl ) <EOL> self . assertFalse ( block_info . can_write ) <EOL> store . read_block_permissions = Mock ( return_value = ElementPermissions ( block_brl ) ) <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> block_brl = BRLBlock ( "<STR_LIT>" ) <EOL> block_info = p . get_block_info ( block_brl ) <EOL> self . assertTrue ( block_info . can_write ) <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> user = Mock ( User ) <EOL> user . administrators = Permissions ( ) <EOL> store . read_user = Mock ( return_value = user ) <EOL> block_info = p . get_block_info ( block_brl ) <EOL> self . assertFalse ( block_info . can_write ) <EOL> p = PublishService ( store , '<STR_LIT>' ) <EOL> block_brl = BRLBlock ( "<STR_LIT>" ) <EOL> block_info = p . get_block_info ( block_brl ) <EOL> self . assertTrue ( block_info . can_write ) </s>
