spec/grape/api/custom_validations_spec.rb in grape-1.7.1 vs spec/grape/api/custom_validations_spec.rb in grape-1.8.0
- old
+ new
@@ -1,50 +1,19 @@
# frozen_string_literal: true
+require 'shared/deprecated_class_examples'
+
describe Grape::Validations do
- context 'deprecated Grape::Validations::Base' do
- subject do
- Class.new(Grape::API) do
- params do
- requires :text, validator_with_old_base: true
- end
- get do
- end
- end
+ describe 'Grape::Validations::Base' do
+ let(:deprecated_class) do
+ Class.new(Grape::Validations::Base)
end
- let(:validator_with_old_base) do
- Class.new(Grape::Validations::Base) do
- def validate_param!(_attr_name, _params)
- true
- end
- end
- end
-
- before do
- described_class.register_validator('validator_with_old_base', validator_with_old_base)
- allow(Warning).to receive(:warn)
- end
-
- after do
- described_class.deregister_validator('validator_with_old_base')
- end
-
- def app
- subject
- end
-
- it 'puts a deprecation warning' do
- expect(Warning).to receive(:warn) do |message|
- expect(message).to include('`Grape::Validations::Base` is deprecated')
- end
-
- get '/'
- end
+ it_behaves_like 'deprecated class'
end
- context 'using a custom length validator' do
+ describe 'using a custom length validator' do
subject do
Class.new(Grape::API) do
params do
requires :text, default_length: 140
end
@@ -62,23 +31,20 @@
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long")
end
end
end
+ let(:app) { Rack::Builder.new(subject) }
before do
described_class.register_validator('default_length', default_length_validator)
end
after do
described_class.deregister_validator('default_length')
end
- def app
- subject
- end
-
it 'under 140 characters' do
get '/', text: 'abc'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'bacon'
end
@@ -94,11 +60,11 @@
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'bacon'
end
end
- context 'using a custom body-only validator' do
+ describe 'using a custom body-only validator' do
subject do
Class.new(Grape::API) do
params do
requires :text, in_body: true
end
@@ -113,23 +79,20 @@
def validate(request)
validate!(request.env['api.request.body'])
end
end
end
+ let(:app) { Rack::Builder.new(subject) }
before do
described_class.register_validator('in_body', in_body_validator)
end
after do
described_class.deregister_validator('in_body')
end
- def app
- subject
- end
-
it 'allows field in body' do
get '/', text: 'abc'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'bacon'
end
@@ -139,11 +102,11 @@
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'text is missing'
end
end
- context 'using a custom validator with message_key' do
+ describe 'using a custom validator with message_key' do
subject do
Class.new(Grape::API) do
params do
requires :text, with_message_key: true
end
@@ -158,31 +121,28 @@
def validate_param!(attr_name, _params)
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: :presence)
end
end
end
+ let(:app) { Rack::Builder.new(subject) }
before do
described_class.register_validator('with_message_key', message_key_validator)
end
after do
described_class.deregister_validator('with_message_key')
end
- def app
- subject
- end
-
it 'fails with message' do
get '/', text: 'foobar'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'text is missing'
end
end
- context 'using a custom request/param validator' do
+ describe 'using a custom request/param validator' do
subject do
Class.new(Grape::API) do
params do
optional :admin_field, type: String, admin: true
optional :non_admin_field, type: String
@@ -206,20 +166,17 @@
# as an example get a token from request and check if it's admin or not
raise Grape::Exceptions::Validation.new(params: @attrs, message: 'Can not set Admin only field.') unless request.headers['X-Access-Token'] == 'admin'
end
end
end
+ let(:app) { Rack::Builder.new(subject) }
before do
described_class.register_validator('admin', admin_validator)
end
after do
described_class.deregister_validator('admin')
- end
-
- def app
- subject
end
it 'fail when non-admin user sets an admin field' do
get '/', admin_field: 'tester', non_admin_field: 'toaster'
expect(last_response.status).to eq 400