@@ -3461,6 +3461,107 @@ def check_filtered_instances(*instances)
34613461 } )
34623462 expect ( event . metadata [ 'target_space_guid' ] ) . to eq ( target_space . guid )
34633463 end
3464+
3465+ describe 'unbind from all apps in target space' do
3466+ let ( :app_1 ) { VCAP ::CloudController ::AppModel . make ( space : target_space ) }
3467+ let ( :app_2 ) { VCAP ::CloudController ::AppModel . make ( space : target_space ) }
3468+
3469+ let ( :binding_1 ) { VCAP ::CloudController ::ServiceBinding . make ( service_instance : service_instance , app : app_1 ) }
3470+ let ( :binding_2 ) { VCAP ::CloudController ::ServiceBinding . make ( service_instance : service_instance , app : app_2 ) }
3471+
3472+ context 'when bindings in shared space were deleted successfully' do
3473+ before do
3474+ stub_unbind ( binding_1 , accepts_incomplete : true , status : 200 , body : { } . to_json )
3475+ stub_unbind ( binding_2 , accepts_incomplete : true , status : 200 , body : { } . to_json )
3476+ end
3477+
3478+ it 'unbinds from all apps in target space and unshares' do
3479+ api_call . call ( space_dev_headers )
3480+
3481+ expect ( last_response . status ) . to eq ( 204 )
3482+
3483+ service_instance . reload
3484+ expect ( service_instance . shared? ) . to be_falsey
3485+ expect ( service_instance . has_bindings? ) . to be_falsey
3486+ end
3487+ end
3488+
3489+ context 'when a binding in shared space is deleted async' do
3490+ before do
3491+ stub_unbind ( binding_1 , accepts_incomplete : true , status : 202 , body : { } . to_json )
3492+ stub_unbind ( binding_2 , accepts_incomplete : true , status : 200 , body : { } . to_json )
3493+ end
3494+
3495+ it 'should respond with 502 and it does not unshare' do
3496+ api_call . call ( space_dev_headers )
3497+
3498+ expect ( last_response . status ) . to eq ( 502 )
3499+ expect ( parsed_response [ 'errors' ] ) . to include (
3500+ include (
3501+ {
3502+ 'detail' => "Unshare of service instance failed: \n \n Unshare of service instance failed because one or more bindings could not be deleted.\n \n " \
3503+ "\t The binding between an application and service instance #{ service_instance . name } in space #{ target_space . name } is being deleted asynchronously." ,
3504+ 'title' => 'CF-ServiceInstanceUnshareFailed'
3505+ } )
3506+ )
3507+
3508+ expect ( service_instance . shared? ) . to be_truthy
3509+ end
3510+ end
3511+ end
3512+
3513+ it 'responds with 404 when the instance does not exist' do
3514+ delete "/v3/service_instances/some-fake-guid/relationships/shared_spaces/#{ space_guid } " ,
3515+ nil ,
3516+ space_dev_headers
3517+
3518+ expect ( last_response ) . to have_status_code ( 404 )
3519+ expect ( parsed_response [ 'errors' ] ) . to include (
3520+ include (
3521+ {
3522+ 'detail' => 'Service instance not found' ,
3523+ 'title' => 'CF-ResourceNotFound'
3524+ } )
3525+ )
3526+ end
3527+
3528+ describe 'target space to unshare from' do
3529+ context 'when it does not exist' do
3530+ let ( :space_guid ) { 'fake-target' }
3531+
3532+ it 'responds with 422' do
3533+ api_call . call ( space_dev_headers )
3534+
3535+ expect ( last_response . status ) . to eq ( 422 )
3536+ expect ( parsed_response [ 'errors' ] ) . to include (
3537+ include (
3538+ {
3539+ 'detail' => "Unable to unshare service instance from space #{ space_guid } . " \
3540+ 'Ensure the space exists and the service instance has been shared to this space.' ,
3541+ 'title' => 'CF-UnprocessableEntity'
3542+ } )
3543+ )
3544+ end
3545+ end
3546+
3547+ context 'when instance was not shared to the space' do
3548+ let ( :space_guid ) { VCAP ::CloudController ::Space . make ( organization : org ) . guid }
3549+
3550+ it 'responds with 422' do
3551+ api_call . call ( space_dev_headers )
3552+
3553+ expect ( last_response . status ) . to eq ( 422 )
3554+ expect ( parsed_response [ 'errors' ] ) . to include (
3555+ include (
3556+ {
3557+ 'detail' => "Unable to unshare service instance from space #{ space_guid } . " \
3558+ 'Ensure the space exists and the service instance has been shared to this space.' ,
3559+ 'title' => 'CF-UnprocessableEntity'
3560+ } )
3561+ )
3562+ end
3563+ end
3564+ end
34643565 end
34653566
34663567 describe 'GET /v3/service_instances/:guid/relationships/shared_spaces' do
@@ -3763,67 +3864,4 @@ def enable_sharing!
37633864 find_or_create ( name : 'service_instance_sharing' ) { |ff | ff . enabled = true } .
37643865 update ( enabled : true )
37653866 end
3766-
3767- describe 'unrefactored' do
3768- let ( :user_email ) { 'user@email.example.com' }
3769- let ( :user_name ) { 'username' }
3770- let ( :user ) { VCAP ::CloudController ::User . make }
3771- let ( :user_header ) { headers_for ( user ) }
3772- let ( :admin_header ) { admin_headers_for ( user , email : user_email , user_name : user_name ) }
3773- let ( :space ) { VCAP ::CloudController ::Space . make }
3774- let ( :another_space ) { VCAP ::CloudController ::Space . make }
3775- let ( :target_space ) { VCAP ::CloudController ::Space . make }
3776- let ( :feature_flag ) { VCAP ::CloudController ::FeatureFlag . make ( name : 'service_instance_sharing' , enabled : false , error_message : nil ) }
3777- let! ( :annotations ) { VCAP ::CloudController ::ServiceInstanceAnnotationModel . make ( key_prefix : 'pre.fix' , key_name : 'to_delete' , value : 'value' ) }
3778- let! ( :service_instance1 ) { VCAP ::CloudController ::ManagedServiceInstance . make ( space : space , name : 'rabbitmq' ) }
3779- let! ( :service_instance2 ) { VCAP ::CloudController ::ManagedServiceInstance . make ( space : space , name : 'redis' ) }
3780- let! ( :service_instance3 ) { VCAP ::CloudController ::ManagedServiceInstance . make ( space : another_space , name : 'mysql' ) }
3781-
3782- describe 'DELETE /v3/service_instances/:guid/relationships/shared_spaces/:space-guid' do
3783- before do
3784- allow ( VCAP ::Services ::ServiceBrokers ::V2 ::Client ) . to receive ( :new ) do |*args , **kwargs , &block |
3785- FakeServiceBrokerV2Client . new ( *args , **kwargs , &block )
3786- end
3787-
3788- share_request = {
3789- 'data' => [
3790- { 'guid' => target_space . guid }
3791- ]
3792- }
3793-
3794- enable_feature_flag!
3795- post "/v3/service_instances/#{ service_instance1 . guid } /relationships/shared_spaces" , share_request . to_json , admin_header
3796- expect ( last_response . status ) . to eq ( 200 )
3797-
3798- disable_feature_flag!
3799- end
3800-
3801- it 'deletes associated bindings in target space when service instance is unshared' do
3802- process = VCAP ::CloudController ::ProcessModelFactory . make ( diego : false , space : target_space )
3803-
3804- enable_feature_flag!
3805- service_binding = VCAP ::CloudController ::ServiceBinding . make ( service_instance : service_instance1 , app : process . app , credentials : { secret : 'key' } )
3806- disable_feature_flag!
3807-
3808- get "/v2/service_bindings/#{ service_binding . guid } " , nil , admin_header
3809- expect ( last_response . status ) . to eq ( 200 )
3810-
3811- delete "/v3/service_instances/#{ service_instance1 . guid } /relationships/shared_spaces/#{ target_space . guid } " , nil , admin_header
3812- expect ( last_response . status ) . to eq ( 204 )
3813-
3814- get "/v2/service_bindings/#{ service_binding . guid } " , nil , admin_header
3815- expect ( last_response . status ) . to eq ( 404 )
3816- end
3817- end
3818-
3819- def enable_feature_flag!
3820- feature_flag . enabled = true
3821- feature_flag . save
3822- end
3823-
3824- def disable_feature_flag!
3825- feature_flag . enabled = false
3826- feature_flag . save
3827- end
3828- end
38293867end
0 commit comments