|
| 1 | +require 'spec_helper' |
| 2 | +require 'actions/service_credential_binding_key_create' |
| 3 | +require 'support/shared_examples/v3_service_binding_create' |
| 4 | + |
| 5 | +module VCAP::CloudController |
| 6 | + module V3 |
| 7 | + RSpec.describe ServiceCredentialBindingKeyCreate do |
| 8 | + subject(:action) { described_class.new() } |
| 9 | + |
| 10 | + let(:space) { Space.make } |
| 11 | + let(:binding_details) {} |
| 12 | + let(:name) { 'test-key'} |
| 13 | + |
| 14 | + describe '#precursor' do |
| 15 | + RSpec.shared_examples 'the credential binding precursor' do |
| 16 | + it 'returns a service credential binding precursor' do |
| 17 | + binding = action.precursor(service_instance, name) |
| 18 | + |
| 19 | + expect(binding).to be |
| 20 | + expect(binding).to eq(ServiceKey.where(guid: binding.guid).first) |
| 21 | + expect(binding.service_instance).to eq(service_instance) |
| 22 | + expect(binding.name).to eq(name) |
| 23 | + expect(binding.credentials).to be_empty |
| 24 | + end |
| 25 | + |
| 26 | + it 'raises an error when a key with same name already exists' do |
| 27 | + binding = ServiceKey.make(service_instance: service_instance) |
| 28 | + expect { action.precursor(service_instance, binding.name) }.to raise_error( |
| 29 | + ServiceCredentialBindingKeyCreate::UnprocessableCreate, |
| 30 | + "The binding name is invalid. Key binding names must be unique. The service instance already has a key binding with name '#{binding.name}'." |
| 31 | + ) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'user-provided service instance' do |
| 36 | + let(:service_instance) { UserProvidedServiceInstance.make() } |
| 37 | + |
| 38 | + it 'raises error' do |
| 39 | + expect { action.precursor(service_instance, name) }.to raise_error( |
| 40 | + ServiceCredentialBindingKeyCreate::UnprocessableCreate, |
| 41 | + "Service credential bindings of type 'key' are not supported for user-provided service instances." |
| 42 | + ) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context 'managed service instance' do |
| 47 | + let(:service_instance) { ManagedServiceInstance.make(space: space) } |
| 48 | + |
| 49 | + context 'when plan is not bindable' do |
| 50 | + before do |
| 51 | + service_instance.service_plan.update(bindable: false) |
| 52 | + end |
| 53 | + |
| 54 | + it 'raises an error' do |
| 55 | + expect { action.precursor(service_instance, name) }.to raise_error( |
| 56 | + ServiceCredentialBindingKeyCreate::UnprocessableCreate, |
| 57 | + 'Service plan does not allow bindings.' |
| 58 | + ) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context 'when plan is not available' do |
| 63 | + before do |
| 64 | + service_instance.service_plan.update(active: false) |
| 65 | + end |
| 66 | + |
| 67 | + it 'raises an error' do |
| 68 | + expect { action.precursor(service_instance, name) }.to raise_error( |
| 69 | + ServiceCredentialBindingKeyCreate::UnprocessableCreate, |
| 70 | + 'Service plan is not available.' |
| 71 | + ) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'when there is an operation in progress for the service instance' do |
| 76 | + it 'raises an error' do |
| 77 | + service_instance.save_with_new_operation({}, { type: 'tacos', state: 'in progress' }) |
| 78 | + |
| 79 | + expect { |
| 80 | + action.precursor(service_instance, name) |
| 81 | + }.to raise_error( |
| 82 | + ServiceCredentialBindingKeyCreate::UnprocessableCreate, |
| 83 | + 'There is an operation in progress for the service instance.' |
| 84 | + ) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'when successful' do |
| 89 | + it_behaves_like 'the credential binding precursor' |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments