@@ -660,6 +660,99 @@ def check_filtered_instances(*instances)
660660 end
661661 end
662662
663+ describe 'GET /v3/service_instances/:guid/relationships/shared_spaces' do
664+ let ( :user_header ) { headers_for ( user ) }
665+ let ( :instance ) { VCAP ::CloudController ::ManagedServiceInstance . make ( space : space ) }
666+ let ( :other_space ) { VCAP ::CloudController ::Space . make }
667+
668+ before ( :each ) do
669+ share_service_instance ( instance , other_space )
670+ end
671+
672+ it 'returns a list of space guids where the service instance is shared to' do
673+ set_current_user_as_role ( role : 'space_developer' , org : space . organization , space : space , user : user )
674+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces" , nil , user_header
675+ expect ( last_response . status ) . to eq ( 200 )
676+
677+ expected_response = {
678+ data : [ { guid : other_space . guid } ] ,
679+ links : {
680+ self : { href : "#{ link_prefix } /v3/service_instances/#{ instance . guid } /relationships/shared_spaces" } ,
681+ }
682+ } . with_indifferent_access
683+
684+ expect ( parsed_response ) . to be_a_response_like ( expected_response )
685+ end
686+
687+ it 'respond with 404 when the user cannot read the originating space' do
688+ set_current_user_as_role ( role : 'space_developer' , org : other_space . organization , space : other_space , user : user )
689+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces" , nil , user_header
690+ expect ( last_response . status ) . to eq ( 404 )
691+ end
692+
693+ describe 'fields' do
694+ it 'can include the space name, guid and organization relationship fields' do
695+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces?fields[space]=name,guid,relationships.organization" , nil , admin_headers
696+ expect ( last_response ) . to have_status_code ( 200 )
697+
698+ r = { organization : { data : { guid : other_space . organization . guid } } }
699+ included = {
700+ spaces : [
701+ { name : other_space . name , guid : other_space . guid , relationships : r }
702+ ]
703+ }
704+
705+ expect ( { included : parsed_response [ 'included' ] } ) . to match_json_response ( { included : included } )
706+ end
707+
708+ it 'can include the organization name and guid fields through space' do
709+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces?fields[space.organization]=name,guid" , nil , admin_headers
710+ expect ( last_response ) . to have_status_code ( 200 )
711+
712+ included = {
713+ organizations : [
714+ {
715+ name : other_space . organization . name ,
716+ guid : other_space . organization . guid
717+ }
718+ ]
719+ }
720+
721+ expect ( { included : parsed_response [ 'included' ] } ) . to match_json_response ( { included : included } )
722+ end
723+
724+ it 'fails for invalid resources' do
725+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces?fields[fruit]=name" , nil , admin_headers
726+ expect ( last_response ) . to have_status_code ( 400 )
727+ expect ( parsed_response [ 'errors' ] ) . to include (
728+ include (
729+ 'detail' => "The query parameter is invalid: Fields [fruit] valid resources are: 'space', 'space.organization'"
730+ )
731+ )
732+ end
733+
734+ it 'fails for not allowed space fields' do
735+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces?fields[space]=metadata" , nil , admin_headers
736+ expect ( last_response ) . to have_status_code ( 400 )
737+ expect ( parsed_response [ 'errors' ] ) . to include (
738+ include (
739+ 'detail' => "The query parameter is invalid: Fields valid keys for 'space' are: 'name', 'guid', 'relationships.organization'"
740+ )
741+ )
742+ end
743+
744+ it 'fails for not allowed space.organization fields' do
745+ get "/v3/service_instances/#{ instance . guid } /relationships/shared_spaces?fields[space.organization]=metadata" , nil , admin_headers
746+ expect ( last_response ) . to have_status_code ( 400 )
747+ expect ( parsed_response [ 'errors' ] ) . to include (
748+ include (
749+ 'detail' => "The query parameter is invalid: Fields valid keys for 'space.organization' are: 'name', 'guid'"
750+ )
751+ )
752+ end
753+ end
754+ end
755+
663756 describe 'POST /v3/service_instances' do
664757 let ( :api_call ) { lambda { |user_headers | post '/v3/service_instances' , request_body . to_json , user_headers } }
665758 let ( :space_guid ) { space . guid }
0 commit comments