Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to validation and coercion: Fix #211 and force order of operations for presence and coercion. #212

Merged
merged 5 commits into from
Jul 30, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rewrote coercion spec to be more robust and to cover additional test …
…cases

Minor style changes and an update to the describe block
  • Loading branch information
adamgotterer committed Jul 28, 2012
commit 61fdcb78926d8060aa7023fba3d83f4770b2bebc
183 changes: 94 additions & 89 deletions spec/grape/validations/coerce_spec.rb
Original file line number Diff line number Diff line change
@@ -1,111 +1,116 @@
require 'spec_helper'

describe Grape::Validations::CoerceValidator do
module ValidationsSpec
module CoerceValidatorSpec
class User
include Virtus
attribute :id, Integer
attribute :name, String
end
subject { Class.new(Grape::API) }
def app; subject end

class API < Grape::API
default_format :json
describe 'coerce' do
it 'error on malformed input' do
subject.params { requires :int, :type => Integer }
subject.get '/single' do 'int works'; end

params do
requires :int, :coerce => Integer
end
get '/single' do
end
get '/single', :int => '43a'
last_response.status.should == 400
last_response.body.should == 'invalid parameter: int'

params do
requires :ids, :type => Array[Integer]
end
get '/arr' do
end
get '/single', :int => '43'
last_response.status.should == 200
last_response.body.should == 'int works'
end

params do
requires :user, :type => ValidationsSpec::CoerceValidatorSpec::User
end
get '/user' do
end
it 'error on malformed input (Array)' do
subject.params { requires :ids, :type => Array[Integer] }
subject.get '/array' do 'array int works'; end

params do
requires :int, :coerce => Integer
optional :int2, :coerce => Integer
optional :arr, :coerce => Array[Integer]
optional :bool, :coerce => Array[Boolean]
end
get '/coerce' do
{
:int => params[:int].class,
:arr => params[:arr] ? params[:arr][0].class : nil,
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
}
end
params do
requires :uploaded_file, :type => Rack::Multipart::UploadedFile
end
post '/file' do
{
:dpx_file => params[:uploaded_file]
}
get 'array', { :ids => ['1', '2', 'az'] }
last_response.status.should == 400
last_response.body.should == 'invalid parameter: ids'

get 'array', { :ids => ['1', '2', '890'] }
last_response.status.should == 200
last_response.body.should == 'array int works'
end

context 'complex objects' do
module CoerceValidatorSpec
class User
include Virtus
attribute :id, Integer
attribute :name, String
end
end
end
end

def app
ValidationsSpec::CoerceValidatorSpec::API
end
it 'error on malformed input for complex objects' do
subject.params { requires :user, :type => CoerceValidatorSpec::User }
subject.get '/user' do 'complex works'; end

it "should return an error on malformed input" do
get '/single', :int => "43a"
last_response.status.should == 400
get '/user', :user => "32"
last_response.status.should == 400
last_response.body.should == 'invalid parameter: user'

get '/single', :int => "43"
last_response.status.should == 200
end
get '/user', :user => { :id => 32, :name => 'Bob' }
last_response.status.should == 200
last_response.body.should == 'complex works'
end
end

it "should return an error on malformed input (array)" do
get '/arr', :ids => ["1", "2", "az"]
last_response.status.should == 400
context 'coerces' do
it 'Integer' do
subject.params { requires :int, :coerce => Integer }
subject.get '/int' do params[:int].class; end

get '/arr', :ids => ["1", "2", "890"]
last_response.status.should == 200
end
get '/int', { :int => "45" }
last_response.status.should == 200
last_response.body.should == 'Fixnum'
end

it "should return an error on malformed input (complex object)" do
# this request does raise an error inside Virtus
get '/user', :user => "32"
last_response.status.should == 400
it 'Array of Integers' do
subject.params { requires :arry, :coerce => Array[Integer] }
subject.get '/array' do params[:arry][0].class; end

get '/user', :user => { :id => 32, :name => "Bob"}
last_response.status.should == 200
end
get '/array', { :arry => [ '1', '2', '3' ] }
last_response.status.should == 200
last_response.body.should == 'Fixnum'
end

it 'should coerce inputs' do
get('/coerce', :int => "43", :int2 => "42")
last_response.status.should == 200
ret = MultiJson.load(last_response.body)
ret["int"].should == "Fixnum"

get('/coerce', :int => "40", :int2 => "42", :arr => ["1","20","3"], :bool => [1, 0])
# last_response.body.should == ""
last_response.status.should == 200
ret = MultiJson.load(last_response.body)
ret["int"].should == "Fixnum"
ret["arr"].should == "Fixnum"
ret["bool"].should == true
end
it 'Array of Bools' do
subject.params { requires :arry, :coerce => Array[Virtus::Attribute::Boolean] }
subject.get '/array' do params[:arry][0].class; end

it 'should not return an error when an optional parameter is nil' do
get('/coerce', :int => "40")
last_response.status.should == 200
end
get 'array', { :arry => [1, 0] }
last_response.status.should == 200
last_response.body.should == 'TrueClass'
end

it 'Bool' do
subject.params { requires :bool, :coerce => Virtus::Attribute::Boolean }
subject.get '/bool' do params[:bool].class; end

get '/bool', { :bool => 1 }
last_response.status.should == 200
last_response.body.should == 'TrueClass'

get '/bool', { :bool => 0 }
last_response.status.should == 200
last_response.body.should == 'FalseClass'

get '/bool', { :bool => 'false' }
last_response.status.should == 200
last_response.body.should == 'FalseClass'

get '/bool', { :bool => 'true' }
last_response.status.should == 200
last_response.body.should == 'TrueClass'
end

it 'file' do
subject.params { requires :file, :coerce => Rack::Multipart::UploadedFile }
subject.post '/upload' do params[:file].filename; end

it 'should coerce a file' do
post('/file', :uploaded_file => Rack::Test::UploadedFile.new(__FILE__))
last_response.status.should == 201
post '/upload', { :file => Rack::Test::UploadedFile.new(__FILE__) }
last_response.status.should == 201
last_response.body.should == File.basename(__FILE__).to_s
end
end
end
end
4 changes: 1 addition & 3 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'spec_helper'



describe Grape::API do
describe Grape::Validations do
subject { Class.new(Grape::API) }
def app; subject end

Expand Down