|
| 1 | +require 'spec_helper' |
| 2 | +require 'messages/service_plans_show_message' |
| 3 | +require 'field_message_spec_shared_examples' |
| 4 | + |
| 5 | +module VCAP::CloudController |
| 6 | + RSpec.describe ServicePlansShowMessage do |
| 7 | + describe '.from_params' do |
| 8 | + let(:params) do |
| 9 | + { |
| 10 | + 'fields' => { 'service_offering.service_broker' => 'guid,name' }, |
| 11 | + }.with_indifferent_access |
| 12 | + end |
| 13 | + |
| 14 | + it 'returns the correct ServicePlansShowMessage' do |
| 15 | + message = described_class.from_params(params) |
| 16 | + |
| 17 | + expect(message).to be_valid |
| 18 | + expect(message).to be_a(described_class) |
| 19 | + expect(message.fields).to match({ 'service_offering.service_broker': ['guid', 'name'] }) |
| 20 | + end |
| 21 | + |
| 22 | + it 'converts requested keys to symbols' do |
| 23 | + message = described_class.from_params(params) |
| 24 | + expect(message.requested?(:fields)).to be_truthy |
| 25 | + end |
| 26 | + |
| 27 | + it 'accepts an empty set' do |
| 28 | + message = described_class.from_params({}) |
| 29 | + expect(message).to be_valid |
| 30 | + end |
| 31 | + |
| 32 | + it 'does not accept arbitrary parameters' do |
| 33 | + message = described_class.from_params({ foobar: 'pants' }.with_indifferent_access) |
| 34 | + |
| 35 | + expect(message).not_to be_valid |
| 36 | + expect(message.errors[:base][0]).to include("Unknown query parameter(s): 'foobar'") |
| 37 | + end |
| 38 | + |
| 39 | + context 'fields' do |
| 40 | + it 'validates `fields` is a hash' do |
| 41 | + message = described_class.from_params({ 'fields' => 'foo' }.with_indifferent_access) |
| 42 | + expect(message).not_to be_valid |
| 43 | + expect(message.errors[:fields][0]).to include('must be an object') |
| 44 | + end |
| 45 | + |
| 46 | + it_behaves_like 'field query parameter', 'service_offering.service_broker', 'guid,name' |
| 47 | + |
| 48 | + it 'does not accept fields resources that are not allowed' do |
| 49 | + message = described_class.from_params({ 'fields' => { 'space.foo': 'name' } }) |
| 50 | + expect(message).not_to be_valid |
| 51 | + expect(message.errors[:fields]).to include( |
| 52 | + "[space.foo] valid resources are: 'service_offering.service_broker'" |
| 53 | + ) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | +end |
0 commit comments