Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/actions/service_instance_share.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def create(service_instance, target_spaces, user_audit_info)
raise CloudController::Errors::ApiError.new_from_details('ServiceShareIsDisabled', service_instance.service.label)
end

if target_spaces.include?(service_instance.space)
raise CloudController::Errors::ApiError.new_from_details('InvalidServiceInstanceSharingTargetSpace')
end

ServiceInstance.db.transaction do
target_spaces.each do |space|
service_instance.add_shared_space(space)
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/actions/service_instance_share_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ module VCAP::CloudController
end
end

context 'when source space is included in list of target spaces' do
it 'does not share with any spaces' do
expect {
service_instance_share.create(service_instance, [target_space1, service_instance.space], user_audit_info)
}.to raise_error(CloudController::Errors::ApiError,
'Service instances cannot be shared into the space where they were created')

instance = ServiceInstance.find(guid: service_instance.guid)

expect(instance.shared_spaces.length).to eq 0
end

it 'does not audit any share events' do
expect(Repositories::ServiceInstanceShareEventRepository).to_not receive(:record_share_event)

expect {
service_instance_share.create(service_instance, [target_space1, service_instance.space], user_audit_info)
}.to raise_error(CloudController::Errors::ApiError,
'Service instances cannot be shared into the space where they were created')
end
end

context 'when the service does is not shareable' do
before do
allow(service_instance).to receive(:shareable?).and_return(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ def stub_delete_and_return(status, body)
context 'when the service instance is shared' do
let(:service_instance) { ManagedServiceInstance.make }
let(:shared_to_space) { Space.make }
let(:shared_to_user) { make_developer_for_space(shared_to_space) }
let(:body) do
{
tags: []
Expand All @@ -1561,8 +1562,6 @@ def stub_delete_and_return(status, body)
end

context 'and a developer in the shared to space tries to update the instance' do
let(:shared_to_user) { make_developer_for_space(shared_to_space) }

before do
set_current_user(shared_to_user)
end
Expand All @@ -1575,6 +1574,31 @@ def stub_delete_and_return(status, body)
expect(last_response.body).to include 'You are not authorized to perform the requested action'
end
end

context 'and a developer in the shared_to space tries to rename a service instance' do
let(:service_instance) { ManagedServiceInstance.make(name: 'r1') }
let(:target_space_service_instance) { ManagedServiceInstance.make(name: 'r2', space: shared_to_space) }

before do
set_current_user(shared_to_user)
end

context 'when the name clashes with a shared service instance' do
let(:body) do
{
name: 'r1'
}.to_json
end

it 'should give the user an error' do
put "/v2/service_instances/#{target_space_service_instance.guid}", body

expect(last_response).to have_status_code 400
expect(last_response.body).to include 'CF-ServiceInstanceNameTaken'
expect(last_response.body).to include "The service instance name is taken: #{service_instance.name}"
end
end
end
end

describe 'error cases' do
Expand Down
4 changes: 4 additions & 0 deletions vendor/errors/v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1128,3 +1128,7 @@
name: RouteServiceInstanceSharingNotSupported
http_code: 400
message: "Route services cannot be shared"
390007:
name: InvalidServiceInstanceSharingTargetSpace
http_code: 422
message: 'Service instances cannot be shared into the space where they were created'