travis-api/spec/unit/responders/json_spec.rb
Piotr Sarnacki 61142c7cf6 Return 404 unless we can find API builder for resource
In order to protect us from rendering a resource simply converted to
json, without processing it with API data class, this commit changes
JSON responder behavior to render 404 if we can't find associated data
class. The only exception to that rule is when resource is already a
Hash, meaning that it was processed before - we sometimes return for
example simple Hash responses like { result: true }.

The Hash exception could allow to accidentally pass resource.as_json to
responder, but in travis-ci/travis-support@124b8b6 I disabled default
as_json method on AR::Base classes, so the risk of such mistake is
lowered.
2013-03-24 19:49:28 +01:00

55 lines
1.4 KiB
Ruby

require 'spec_helper'
module Travis::Api::App::Responders
describe Json do
class MyJson < Json
end
let(:request) { stub 'request', params: {} }
let(:endpoint) { stub 'endpoint', request: request }
let(:resource) { stub 'resource' }
let(:accept) { stub 'accept entry', version: '2', params: {} }
let(:options) { { :accept => accept} }
let(:json) { MyJson.new(endpoint, resource, options) }
context 'with resource not associated with Api data class' do
it 'returns nil result' do
json.apply.should be_false
end
end
context 'with resource being' do
context 'a Hash instance' do
let(:resource) { { foo: 'bar' } }
it 'returns resource converted to_json' do
json.expects(:halt).with({ foo: 'bar' }.to_json)
json.apply
end
end
context 'nil' do
let(:resource) { nil }
it 'responds with 404' do
json.apply?.should be_false
json.apply.should be_false
end
end
end
context 'with resource associated with Api data class' do
let(:builder) { stub 'builder', data: { foo: 'bar' } }
let(:builder_class) { stub 'builder class', new: builder }
before do
json.stubs :builder => builder_class
end
it 'returns proper data converted to json' do
json.expects(:halt).with({ foo: 'bar' }.to_json)
json.apply
end
end
end
end