|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Grape::Validations::SameAsValidator do |
| 4 | + module ValidationsSpec |
| 5 | + module SameAsValidatorSpec |
| 6 | + class API < Grape::API |
| 7 | + params do |
| 8 | + requires :password |
| 9 | + requires :password_confirmation, same_as: :password |
| 10 | + end |
| 11 | + post do |
| 12 | + end |
| 13 | + |
| 14 | + params do |
| 15 | + requires :password |
| 16 | + requires :password_confirmation, same_as: { value: :password, message: 'not match' } |
| 17 | + end |
| 18 | + post '/custom-message' do |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + def app |
| 25 | + ValidationsSpec::SameAsValidatorSpec::API |
| 26 | + end |
| 27 | + |
| 28 | + describe '/' do |
| 29 | + context 'is the same' do |
| 30 | + it do |
| 31 | + post '/', password: '987654', password_confirmation: '987654' |
| 32 | + expect(last_response.status).to eq(201) |
| 33 | + expect(last_response.body).to eq('') |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'is not the same' do |
| 38 | + it do |
| 39 | + post '/', password: '123456', password_confirmation: 'whatever' |
| 40 | + expect(last_response.status).to eq(400) |
| 41 | + expect(last_response.body).to eq('password_confirmation is not the same as password') |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + describe '/custom-message' do |
| 47 | + context 'is the same' do |
| 48 | + it do |
| 49 | + post '/custom-message', password: '987654', password_confirmation: '987654' |
| 50 | + expect(last_response.status).to eq(201) |
| 51 | + expect(last_response.body).to eq('') |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'is not the same' do |
| 56 | + it do |
| 57 | + post '/custom-message', password: '123456', password_confirmation: 'whatever' |
| 58 | + expect(last_response.status).to eq(400) |
| 59 | + expect(last_response.body).to eq('password_confirmation not match') |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments