<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import mock <EOL> from copy import deepcopy <EOL> from viewfinder . backend . base import util <EOL> from viewfinder . backend . db . accounting import Accounting <EOL> from viewfinder . backend . db . db_client import DBKey <EOL> from viewfinder . backend . db . follower import Follower <EOL> from viewfinder . backend . db . operation import Operation <EOL> from viewfinder . backend . db . viewpoint import Viewpoint <EOL> from viewfinder . backend . www . test import service_base_test <EOL> class RemoveViewpointTestCase ( service_base_test . ServiceBaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( RemoveViewpointTestCase , self ) . setUp ( ) <EOL> self . _CreateSimpleTestAssets ( ) <EOL> self . _existing_vp_id , existing_ep_ids = self . _tester . ShareNew ( self . _cookie , <EOL> [ ( self . _episode_id2 , self . _photo_ids2 ) ] , <EOL> [ self . _user2 . user_id ] , <EOL> ** self . _CreateViewpointDict ( self . _cookie ) ) <EOL> self . _new_vp_id , new_ep_ids = self . _tester . ShareNew ( self . _cookie , <EOL> [ ( self . _episode_id , self . _photo_ids ) ] , <EOL> [ self . _user2 . user_id ] , <EOL> ** self . _CreateViewpointDict ( self . _cookie ) ) <EOL> self . _existing_ep_id = existing_ep_ids [ <NUM_LIT:0> ] <EOL> self . _new_ep_id = new_ep_ids [ <NUM_LIT:0> ] <EOL> image_data = '<STR_LIT>' <EOL> self . _tester . PutPhotoImage ( self . _cookie , <EOL> self . _new_ep_id , <EOL> self . _photo_ids [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' , <EOL> image_data , <EOL> content_md5 = util . ComputeMD5Base64 ( image_data ) ) <EOL> def testRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _UpdateOrAllocateDBObject ( Follower , user_id = self . _user . user_id , <EOL> viewpoint_id = self . _new_vp_id , labels = [ ] ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie , self . _new_vp_id ) <EOL> follower = self . _RunAsync ( Follower . Query , self . _client , self . _user . user_id , self . _new_vp_id , None ) <EOL> self . assertTrue ( follower . IsRemoved ( ) ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie , self . _new_vp_id ) <EOL> def testAccessAfterRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _UpdateOrAllocateDBObject ( Follower , user_id = self . _user . user_id , <EOL> viewpoint_id = self . _new_vp_id , labels = [ ] ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie , self . _new_vp_id ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . AddFollowers , <EOL> self . _cookie , <EOL> self . _new_vp_id , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , self . _tester . PostComment , self . _cookie , self . _new_vp_id , "<STR_LIT>" ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , self . _tester . HidePhotos , self . _cookie , [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . Unshare , <EOL> self . _cookie , <EOL> self . _new_vp_id , <EOL> [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , self . _tester . SavePhotos , self . _cookie , [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . ShareExisting , <EOL> self . _cookie , <EOL> self . _new_vp_id , <EOL> [ ( self . _existing_ep_id , self . _photo_ids2 [ : <NUM_LIT:1> ] ) ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . ShareExisting , <EOL> self . _cookie , <EOL> self . _existing_vp_id , <EOL> [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . ShareNew , <EOL> self . _cookie , <EOL> [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] , <EOL> [ self . _user2 . user_id ] , <EOL> ** self . _CreateViewpointDict ( self . _cookie ) ) <EOL> response = self . _tester . GetPhotoImage ( self . _cookie , self . _new_ep_id , self . _photo_ids [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response . code , <NUM_LIT> ) <EOL> image_data = '<STR_LIT>' <EOL> response = self . _tester . PutPhotoImage ( self . _cookie , <EOL> self . _new_ep_id , <EOL> self . _photo_ids [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' , <EOL> image_data , <EOL> content_md5 = util . ComputeMD5Base64 ( image_data ) ) <EOL> self . assertEqual ( response . code , <NUM_LIT> ) <EOL> result = self . _tester . QueryViewpoints ( self . _cookie , [ self . _tester . CreateViewpointSelection ( self . _new_vp_id ) ] ) <EOL> self . assertIsNone ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get ( '<STR_LIT>' , None ) ) <EOL> self . assertIsNone ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get ( '<STR_LIT>' , None ) ) <EOL> self . assertIsNone ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get ( '<STR_LIT>' , None ) ) <EOL> self . assertIsNone ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get ( '<STR_LIT>' , None ) ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . UpdateViewpoint , <EOL> self . _cookie , <EOL> self . _new_vp_id , <EOL> title = '<STR_LIT>' ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , <EOL> self . _tester . UpdateEpisode , <EOL> self . _cookie , <EOL> self . _new_ep_id , <EOL> description = '<STR_LIT>' ) <EOL> result = self . _tester . QueryEpisodes ( self . _cookie , [ self . _tester . CreateEpisodeSelection ( self . _new_ep_id ) ] ) <EOL> self . assertEquals ( len ( result [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> def testRemoveNonExistentViewpointFails ( self ) : <EOL> """<STR_LIT>""" <EOL> vp_id = Viewpoint . ConstructViewpointId ( self . _device_ids [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , self . _tester . RemoveViewpoint , self . _cookie , vp_id ) <EOL> def testRemoveViewpointCreatedByOtherUser ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _UpdateOrAllocateDBObject ( Follower , user_id = self . _user2 . user_id , <EOL> viewpoint_id = self . _new_vp_id , labels = [ ] ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> def testRemoveViewpointThatUserIsNotFollowingFails ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaisesHttpError ( <NUM_LIT> , self . _tester . RemoveViewpoint , self . _cookie3 , self . _new_vp_id ) <EOL> def testRemoveDefaultViewpointFails ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _UpdateOrAllocateDBObject ( Follower , user_id = self . _user . user_id , <EOL> viewpoint_id = self . _user . private_vp_id , labels = [ ] ) <EOL> self . assertRaisesHttpError ( <NUM_LIT> , self . _tester . RemoveViewpoint , self . _cookie , self . _user . private_vp_id ) <EOL> def testRemoveViewpointByNonOwnerAndUnshare ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . Unshare ( self . _cookie , self . _new_vp_id , [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] ) <EOL> def testUnshareSecondLevelRemoved ( self ) : <EOL> """<STR_LIT>""" <EOL> child_vp_id , child_ep_ids = self . _tester . ShareNew ( self . _cookie2 , <EOL> [ ( self . _new_ep_id , self . _photo_ids ) ] , <EOL> [ self . _user3 . user_id ] , <EOL> ** self . _CreateViewpointDict ( self . _cookie2 ) ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie3 , child_vp_id ) <EOL> self . _tester . Unshare ( self . _cookie , self . _new_vp_id , [ ( self . _new_ep_id , self . _photo_ids [ : <NUM_LIT:1> ] ) ] ) <EOL> def testRevivalAfterRemovingMultipleFollowers ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . AddFollowers ( self . _cookie , self . _new_vp_id , [ '<STR_LIT>' ] ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie3 , self . _new_vp_id ) <EOL> self . _tester . UpdateViewpoint ( self . _cookie , self . _new_vp_id , description = '<STR_LIT>' ) <EOL> followers , _ = self . _RunAsync ( Viewpoint . QueryFollowers , self . _client , self . _new_vp_id ) <EOL> for follower in followers : <EOL> self . assertFalse ( follower . IsRemoved ( ) ) <EOL> def testUpdateViewpointAfterRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _skip_validation_for = [ '<STR_LIT>' ] <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . UpdateViewpoint ( self . _cookie , self . _new_vp_id , title = '<STR_LIT>' ) <EOL> def testUpdateEpisodeAfterRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . UpdateEpisode ( self . _cookie , self . _episode_id , description = '<STR_LIT>' ) <EOL> def testAddFollowersAfterRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . AddFollowers ( self . _cookie , self . _new_vp_id , [ '<STR_LIT>' ] ) <EOL> def testPostCommentAfterRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . PostComment ( self . _cookie , self . _new_vp_id , message = '<STR_LIT>' ) <EOL> def testShareExistingAfterRemoveViewpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . ShareExisting ( self . _cookie , self . _new_vp_id , <EOL> [ ( self . _episode_id2 , self . _photo_ids2 ) ] ) <EOL> @ mock . patch . object ( Operation , '<STR_LIT>' , True ) <EOL> def testIdempotency ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> self . _tester . RemoveViewpoint ( self . _cookie2 , self . _new_vp_id ) <EOL> def _TestRemoveViewpoint ( tester , user_cookie , request_dict ) : <EOL> """<STR_LIT>""" <EOL> validator = tester . validator <EOL> user_id , device_id = tester . GetIdsFromCookie ( user_cookie ) <EOL> request_dict = deepcopy ( request_dict ) <EOL> viewpoint_id = request_dict [ '<STR_LIT>' ] <EOL> actual_dict = tester . SendRequest ( '<STR_LIT>' , user_cookie , request_dict ) <EOL> op_dict = tester . _DeriveNotificationOpDict ( user_id , device_id , request_dict ) <EOL> request_dict [ '<STR_LIT>' ] = user_id <EOL> follower = validator . GetModelObject ( Follower , DBKey ( user_id , viewpoint_id ) ) <EOL> if not follower . IsRemoved ( ) : <EOL> labels = follower . labels . union ( [ Follower . REMOVED ] ) <EOL> validator . ValidateUpdateDBObject ( Follower , user_id = user_id , viewpoint_id = viewpoint_id , labels = labels ) <EOL> invalidate = { '<STR_LIT>' : [ { '<STR_LIT>' : viewpoint_id , <EOL> '<STR_LIT>' : True } ] } <EOL> validator . ValidateNotification ( '<STR_LIT>' , <EOL> user_id , <EOL> op_dict , <EOL> invalidate , <EOL> viewpoint_id = viewpoint_id ) <EOL> validator . ValidateViewpointAccounting ( viewpoint_id ) <EOL> tester . _CompareResponseDicts ( '<STR_LIT>' , user_id , request_dict , { } , actual_dict ) <EOL> return actual_dict </s>
