spec/functional_spec.rb in praxis-2.0.pre.18 vs spec/functional_spec.rb in praxis-2.0.pre.19

- old
+ new

@@ -1,22 +1,23 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'Functional specs' do - def app Praxis::Application.instance end - let(:session) { double("session", valid?: true)} + let(:session) { double('session', valid?: true) } context 'index' do - context 'with a valid request' do it 'is successful' do get '/api/clouds/1/instances?api_version=1.0', nil, 'global_session' => session expect(last_response.headers['Content-Type']).to( - eq("application/vnd.acme.instance;type=collection")) + eq('application/vnd.acme.instance;type=collection') + ) end end context 'with a path param that can not load' do it 'returns a useful error' do @@ -57,11 +58,10 @@ expect(response['name']).to eq 'ValidationError' expect(response['summary']).to eq 'Error validating request data.' expect(response['errors']).to match_array([/.*cloud_id.*is smaller than the allowed min/]) end - end context 'with a header that is invalid' do it 'returns a useful error' do get '/api/clouds/1/instances?api_version=1.0', nil, 'global_session' => session, 'HTTP_ACCOUNT_ID' => '-1' @@ -85,16 +85,16 @@ app.logger = logger end it 'fails to validate the response' do - get '/api/clouds/1/instances?response_content_type=somejunk&api_version=1.0', nil, 'HTTP_FOO' => "bar", 'global_session' => session + get '/api/clouds/1/instances?response_content_type=somejunk&api_version=1.0', nil, 'HTTP_FOO' => 'bar', 'global_session' => session expect(last_response.status).to eq(400) response = JSON.parse(last_response.body) expect(response['name']).to eq('ValidationError') - expect(response['summary']).to eq("Error validating response") + expect(response['summary']).to eq('Error validating response') expect(response['errors'].first).to match(/Bad Content-Type/) end context 'with response validation disabled' do let(:praxis_config) { double('praxis_config', validate_responses: false) } @@ -103,34 +103,33 @@ before do expect(Praxis::Application.instance.config).to receive(:praxis).and_return(praxis_config) end it 'fails to validate the response' do - expect { - get '/api/clouds/1/instances?response_content_type=somejunk&api_version=1.0',nil, 'global_session' => session - }.to_not raise_error + expect do + get '/api/clouds/1/instances?response_content_type=somejunk&api_version=1.0', nil, 'global_session' => session + end.to_not raise_error end - end end - end it 'works' do - the_body = StringIO.new("{}") # This is a funny, GET request expecting a body - get '/api/clouds/1/instances/2?junk=foo&api_version=1.0', nil,'rack.input' => the_body,'CONTENT_TYPE' => "application/json", 'global_session' => session + the_body = StringIO.new('{}') # This is a funny, GET request expecting a body + get '/api/clouds/1/instances/2?junk=foo&api_version=1.0', nil, 'rack.input' => the_body, 'CONTENT_TYPE' => 'application/json', 'global_session' => session expect(last_response.status).to eq(200) expected = { - "cloud_id" => 1, - "id"=>2, - "junk"=>"foo", - "other_params"=>{ - "some_date"=>"2012-12-21T00:00:00+00:00", - "fail_filter"=>false + 'cloud_id' => 1, + 'id' => 2, + 'junk' => 'foo', + 'other_params' => { + 'some_date' => '2012-12-21T00:00:00+00:00', + 'fail_filter' => false }, - "payload"=>{ - "optional"=>"not given"} + 'payload' => { + 'optional' => 'not given' + } } expect(JSON.parse(last_response.body)).to eq(expected) headers = last_response.headers @@ -142,13 +141,12 @@ get '/api/clouds/1/instances/2?junk=foo&api_version=1.0&fail_filter=true', nil, 'global_session' => session expect(last_response.status).to eq(401) end context 'bulk_create multipart' do - let(:instance) { Instance.example } - let(:instance_json) { JSON.pretty_generate(instance.render(fields: {id: true, name: true})) } + let(:instance_json) { JSON.pretty_generate(instance.render(fields: { id: true, name: true })) } let(:form) do form_data = MIME::Multipart::FormData.new entity = MIME::Text.new(instance_json) form_data.add entity, instance.id.to_s @@ -168,19 +166,18 @@ response_id = instance_part.name expect(response_id).to eq(instance.id.to_s) instance_headers = instance_part.headers expect(instance_headers['Status']).to eq('201') - expect(instance_headers['Location']).to match(%r|/clouds/.*/instances/.*|) + expect(instance_headers['Location']).to match(%r{/clouds/.*/instances/.*}) response_instance = JSON.parse(instance_part.body) - expect(response_instance["key"]).to eq(instance.id) - expect(response_instance["value"].values).to eq(instance.render(fields: {id: true, name: true}).values) + expect(response_instance['key']).to eq(instance.id) + expect(response_instance['value'].values).to eq(instance.render(fields: { id: true, name: true }).values) end end - context 'attach_file' do let(:form) do form_data = MIME::Multipart::FormData.new destination_path = MIME::Text.new('/etc/defaults') @@ -227,13 +224,12 @@ it 'returns an error' do post '/api/clouds/1/instances/2/files?api_version=1.0', body, 'CONTENT_TYPE' => content_type, 'global_session' => session response = JSON.parse(last_response.body) expect(response['name']).to eq('ValidationError') - expect(response['errors']).to eq(["Attribute $.payload.destination_path is required"]) + expect(response['errors']).to eq(['Attribute $.payload.destination_path is required']) end - end context 'with an extra key in the form' do let(:form) do form_data = MIME::Multipart::FormData.new @@ -255,45 +251,42 @@ subject(:response) { JSON.parse(last_response.body) } before do post '/api/clouds/1/instances/2/files?api_version=1.0', body, 'CONTENT_TYPE' => content_type, 'global_session' => session end - its(:keys){ should eq(['destination_path','name','filename','type','contents','options'])} - its(['options']){ should eq({"extra_thing"=>"I am extra"})} + its(:keys) { should eq(%w[destination_path name filename type contents options]) } + its(['options']) { should eq({ 'extra_thing' => 'I am extra' }) } end - end - context 'not found and API versions' do context 'when no version is specified' do it 'it tells you which available api versions would match' do - get '/api/clouds/1/instances/2?junk=foo',nil, 'global_session' => session + get '/api/clouds/1/instances/2?junk=foo', nil, 'global_session' => session expect(last_response.status).to eq(404) - expect(last_response.headers["Content-Type"]).to eq("text/plain") - expect(last_response.body).to eq("NotFound. Your request did not specify an API version. Available versions = \"1.0\".") + expect(last_response.headers['Content-Type']).to eq('text/plain') + expect(last_response.body).to eq('NotFound. Your request did not specify an API version. Available versions = "1.0".') end it 'it just gives you a simple not found when nothing would have matched' do get '/foobar?junk=foo', nil, 'global_session' => session expect(last_response.status).to eq(404) - expect(last_response.headers["Content-Type"]).to eq("text/plain") - expect(last_response.body).to eq("NotFound") + expect(last_response.headers['Content-Type']).to eq('text/plain') + expect(last_response.body).to eq('NotFound') end end context 'when some version is specified, but wrong' do it 'it tells you which possible correcte api versions exist' do get '/api/clouds/1/instances/2?junk=foo&api_version=50.0', nil, 'global_session' => session expect(last_response.status).to eq(404) - expect(last_response.headers["Content-Type"]).to eq("text/plain") - expect(last_response.body).to eq("NotFound. Your request specified API version = \"50.0\". Available versions = \"1.0\".") + expect(last_response.headers['Content-Type']).to eq('text/plain') + expect(last_response.body).to eq('NotFound. Your request specified API version = "50.0". Available versions = "1.0".') end end - end context 'volumes' do before do header 'X-Api-Version', '1.0' @@ -302,11 +295,11 @@ context 'when no authorization header is passed' do it 'works as expected' do get '/api/clouds/1/volumes/123?junk=stuff', nil, 'global_session' => session expect(last_response.status).to eq(200) expect(Volume.load(last_response.body).validate).to be_empty - expect(last_response.headers["Content-Type"]).to eq("application/vnd.acme.volume") + expect(last_response.headers['Content-Type']).to eq('application/vnd.acme.volume') end end context 'when an authorization header is passed' do it 'returns 401 when it does not match "secret" ' do get '/api/clouds/1/volumes/123?junk=stuff', nil, 'HTTP_AUTHORIZATION' => 'foobar', 'global_session' => session @@ -315,11 +308,10 @@ end it 'succeeds as expected when it matches "secret" ' do get '/api/clouds/1/volumes/123?junk=stuff', nil, 'HTTP_AUTHORIZATION' => 'the secret', 'global_session' => session expect(last_response.status).to eq(200) end - end context 'index action with no args defined' do it 'dispatches successfully' do get '/api/clouds/1/volumes', nil, 'HTTP_AUTHORIZATION' => 'the secret', 'global_session' => session @@ -327,20 +319,19 @@ end end end context 'wildcard verb routing' do - let(:content_type){ 'application/json' } + let(:content_type) { 'application/json' } it 'can terminate instances with POST' do post '/api/clouds/23/instances/1/terminate?api_version=1.0', nil, 'CONTENT_TYPE' => content_type, 'global_session' => session expect(last_response.status).to eq(200) end it 'can terminate instances with DELETE' do post '/api/clouds/23/instances/1/terminate?api_version=1.0', nil, 'CONTENT_TYPE' => content_type, 'global_session' => session expect(last_response.status).to eq(200) end - end context 'route options' do it 'reach the endpoint that does not match the except clause' do get '/api/clouds/23/otherinstances/_action/test?api_version=1.0', nil, 'global_session' => session @@ -379,11 +370,10 @@ expect(body['errors']).to_not be_empty end end context 'update' do - let(:body) { JSON.pretty_generate(request_payload) } let(:content_type) { 'application/json' } before do patch '/api/clouds/1/instances/3?api_version=1.0', body, 'CONTENT_TYPE' => content_type, 'global_session' => session @@ -397,31 +387,29 @@ it { should_not have_key('name') } it { should_not have_key('root_volume') } end context 'with a provided name' do - let(:request_payload) { {name: 'MyInstance'} } + let(:request_payload) { { name: 'MyInstance' } } its(['name']) { should eq('MyInstance') } it { should_not have_key('root_volume') } end context 'with an explicitly-nil root_volme' do - let(:request_payload) { {name: 'MyInstance', root_volume: nil} } + let(:request_payload) { { name: 'MyInstance', root_volume: nil } } its(['name']) { should eq('MyInstance') } its(['root_volume']) { should be(nil) } end context 'with an invalid name' do - let(:request_payload) { {name: 'Invalid Name'} } + let(:request_payload) { { name: 'Invalid Name' } } its(['name']) { should eq 'ValidationError' } its(['summary']) { should eq 'Error validating response' } its(['errors']) { should match_array [/\$\.name value \(Invalid Name\) does not match regexp/] } it 'returns a validation error' do expect(last_response.status).to eq(400) end end - end - end