Skip to content

Commit

Permalink
Properly test JSON responses and fix wrong JSON string
Browse files Browse the repository at this point in the history
- Parse JSON response instead of searching for a substring.
- Decode 'X-Broker-Api-Originating-Identity' header value and check
  contained 'user_id' instead of comparing two base64 encoded strings.
- Add missing '}' to JSON string.
  • Loading branch information
philippthun committed Sep 22, 2021
1 parent 4867074 commit 963e568
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@

it 'sends the broker the X-Broker-Api-Originating-Identity header' do
user = VCAP::CloudController::User.make
base64_encoded_user_id = Base64.strict_encode64("{\"user_id\":\"#{user.guid}\"}")

get("/v2/service_bindings/#{@binding_guid}/parameters",
{}.to_json,
headers_for(user, scopes: %w(cloud_controller.admin)))

expect(
a_request(:get, %r{/v2/service_instances/#{@service_instance_guid}/service_bindings/[[:alnum:]-]+}).with do |req|
req.headers['X-Broker-Api-Originating-Identity'] == "cloudfoundry #{base64_encoded_user_id}"
m = req.headers['X-Broker-Api-Originating-Identity'].match /(?<platform>\S+) (?<value>\S+)/
value = MultiJson.load(Base64.strict_decode64(m[:value]))
m[:platform] == 'cloudfoundry' && value['user_id'] == user.guid
end
).to have_been_made
end
Expand Down Expand Up @@ -177,7 +178,7 @@
async_bind_service(status: 202)

service_binding = VCAP::CloudController::ServiceBinding.find(guid: @binding_guid)
stub_request(:get, service_binding_url(service_binding)).to_return(status: 200, body: '{"credentials": {"foo": true}')
stub_request(:get, service_binding_url(service_binding)).to_return(status: 200, body: '{"credentials": {"foo": true}}')

Delayed::Worker.new.work_off

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'broker_api_v2.11_spec.rb' => '99e61dc50ceb635b09b3bd16901a4fa6',
'broker_api_v2.12_spec.rb' => '4023dffdcaae014556dcdba9f7d206bb',
'broker_api_v2.13_spec.rb' => '573bbe3234c33aeccb1f02399dffdfe5',
'broker_api_v2.14_spec.rb' => 'a1e7485793ba1916ea2f4080943530a5',
'broker_api_v2.14_spec.rb' => 'a9fbd1d91bcc9c33c054c7854be1ab5a',
'broker_api_v2.15_spec.rb' => 'c575fd37bc6dc8df4f773719ccef3288',
}
end
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/controllers/runtime/routes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ module VCAP::CloudController
post '/v2/routes?generate_port=true', MultiJson.dump(req), headers_for(user)

expect(last_response.status).to eq(201)
expect(last_response.body).to include("\"port\": #{generated_port}")
expect(MultiJson.load(last_response.body)['entity']['port']).to eq(generated_port)
expect(last_response.headers).not_to include('X-CF-Warnings')
end
end
Expand All @@ -617,7 +617,7 @@ module VCAP::CloudController
post '/v2/routes?generate_port=true', MultiJson.dump(req), headers_for(user)

expect(last_response.status).to eq(201)
expect(last_response.body).to include("\"port\": #{generated_port}")
expect(MultiJson.load(last_response.body)['entity']['port']).to eq(generated_port)
expect(last_response.headers).to include('X-Cf-Warnings')
expect(last_response.headers['X-Cf-Warnings']).to include(port_override_warning)
end
Expand All @@ -640,7 +640,7 @@ module VCAP::CloudController
post '/v2/routes?generate_port=true', MultiJson.dump(req), headers_for(user)

expect(last_response.status).to eq(201)
expect(last_response.body).to include("\"port\": #{generated_port + 1}")
expect(MultiJson.load(last_response.body)['entity']['port']).to eq(generated_port + 1)
expect(last_response.headers).not_to include('X-Cf-Warnings')
end
end
Expand Down Expand Up @@ -734,7 +734,7 @@ module VCAP::CloudController
post '/v2/routes?generate_port=false', MultiJson.dump(req), headers_for(user)

expect(last_response.status).to eq(201)
expect(last_response.body).to include("\"port\": #{port}")
expect(MultiJson.load(last_response.body)['entity']['port']).to eq(port)
end
end
end
Expand Down

0 comments on commit 963e568

Please sign in to comment.