spec/grape/validations/validators/presence_spec.rb in grape-1.6.0 vs spec/grape/validations/validators/presence_spec.rb in grape-1.6.1
- old
+ new
@@ -1,15 +1,16 @@
# frozen_string_literal: true
require 'spec_helper'
-describe Grape::Validations::PresenceValidator do
+describe Grape::Validations::Validators::PresenceValidator do
subject do
Class.new(Grape::API) do
format :json
end
end
+
def app
subject
end
context 'without validation' do
@@ -18,10 +19,11 @@
get do
'All the bacon'
end
end
end
+
it 'does not validate for any params' do
get '/bacons'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('All the bacon'.to_json)
end
@@ -36,19 +38,22 @@
get do
'Hello'
end
end
end
+
it 'requires when missing' do
get '/requires'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"email is required, email has no value"}')
end
+
it 'requires when empty' do
get '/requires', email: ''
expect(last_response.body).to eq('{"error":"email has no value, email format is invalid"}')
end
+
it 'valid when set' do
get '/requires', email: 'bob@example.com'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello'.to_json)
end
@@ -62,10 +67,11 @@
end
subject.post do
{ ret: params[:id] }
end
end
+
it 'validates id' do
post '/'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"id is missing"}')
@@ -88,20 +94,23 @@
end
subject.get do
'Hello'
end
end
+
it 'requires when missing' do
get '/'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"email is missing, email is empty"}')
end
+
it 'requires when empty' do
get '/', email: ''
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"email is empty, email is invalid"}')
end
+
it 'valid when set' do
get '/', email: 'bob@example.com'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello'.to_json)
end
@@ -122,10 +131,11 @@
end
subject.get '/multiple-requires' do
'Hello'
end
end
+
it 'validates for all defined params' do
get '/single-requires'
expect(last_response.status).to eq(400)
single_requires_error = last_response.body
@@ -142,10 +152,11 @@
end
subject.get do
'Hello'
end
end
+
it 'validates name, company' do
get '/'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"name is missing, company is missing"}')
@@ -169,10 +180,11 @@
end
subject.get '/nested' do
'Nested'
end
end
+
it 'validates nested parameters' do
get '/nested'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"user is missing, user[first_name] is missing, user[last_name] is missing"}')
@@ -201,10 +213,11 @@
end
subject.get '/nested_triple' do
'Nested triple'
end
end
+
it 'validates triple nested parameters' do
get '/nested_triple'
expect(last_response.status).to eq(400)
expect(last_response.body).to include '{"error":"admin is missing'
@@ -250,18 +263,20 @@
end
subject.get '/optional' do
'Hello optional'
end
end
+
it 'works with required' do
get '/required'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"name is missing"}')
get '/required', name: 'Bob'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello required'.to_json)
end
+
it 'works with optional' do
get '/optional'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello optional'.to_json)