1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
# frozen_string_literal: true
require 'spec_helper'
describe Grape::Validations::Validators::ExceptValuesValidator do
module ValidationsSpec
class ExceptValuesModel
DEFAULT_EXCEPTS = %w[invalid-type1 invalid-type2 invalid-type3].freeze
class << self
attr_accessor :excepts
def excepts
@excepts ||= []
[DEFAULT_EXCEPTS + @excepts].flatten.uniq
end
end
end
TEST_CASES = {
req_except: {
requires: { except_values: ExceptValuesModel.excepts },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'invalid-type3', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json }
]
},
req_except_hash: {
requires: { except_values: { value: ExceptValuesModel.excepts } },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'invalid-type3', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json }
]
},
req_except_custom_message: {
requires: { except_values: { value: ExceptValuesModel.excepts, message: 'is not allowed' } },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type is not allowed' }.to_json },
{ value: 'invalid-type3', rc: 400, body: { error: 'type is not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json }
]
},
req_except_no_value: {
requires: { except_values: { message: 'is not allowed' } },
tests: [
{ value: 'invalid-type1', rc: 200, body: { type: 'invalid-type1' }.to_json }
]
},
req_except_empty: {
requires: { except_values: [] },
tests: [
{ value: 'invalid-type1', rc: 200, body: { type: 'invalid-type1' }.to_json }
]
},
req_except_lambda: {
requires: { except_values: -> { ExceptValuesModel.excepts } },
add_excepts: ['invalid-type4'],
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'invalid-type4', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json }
]
},
req_except_lambda_custom_message: {
requires: { except_values: { value: -> { ExceptValuesModel.excepts }, message: 'is not allowed' } },
add_excepts: ['invalid-type4'],
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type is not allowed' }.to_json },
{ value: 'invalid-type4', rc: 400, body: { error: 'type is not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json }
]
},
opt_except_default: {
optional: { except_values: ExceptValuesModel.excepts, default: 'valid-type2' },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'invalid-type3', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json },
{ rc: 200, body: { type: 'valid-type2' }.to_json }
]
},
opt_except_lambda_default: {
optional: { except_values: -> { ExceptValuesModel.excepts }, default: 'valid-type2' },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'invalid-type3', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 'valid-type', rc: 200, body: { type: 'valid-type' }.to_json },
{ rc: 200, body: { type: 'valid-type2' }.to_json }
]
},
req_except_type_coerce: {
requires: { type: Integer, except_values: [10, 11] },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type is invalid' }.to_json },
{ value: 11, rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: '11', rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: '3', rc: 200, body: { type: 3 }.to_json },
{ value: 3, rc: 200, body: { type: 3 }.to_json }
]
},
opt_except_type_coerce_default: {
optional: { type: Integer, except_values: [10, 11], default: 12 },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type is invalid' }.to_json },
{ value: 10, rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: '3', rc: 200, body: { type: 3 }.to_json },
{ value: 3, rc: 200, body: { type: 3 }.to_json },
{ rc: 200, body: { type: 12 }.to_json }
]
},
opt_except_array_type_coerce_default: {
optional: { type: Array[Integer], except_values: [10, 11], default: 12 },
tests: [
{ value: 'invalid-type1', rc: 400, body: { error: 'type is invalid' }.to_json },
{ value: 10, rc: 400, body: { error: 'type is invalid' }.to_json },
{ value: [10], rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: ['3'], rc: 200, body: { type: [3] }.to_json },
{ value: [3], rc: 200, body: { type: [3] }.to_json },
{ rc: 200, body: { type: 12 }.to_json }
]
},
req_except_range: {
optional: { type: Integer, except_values: 10..12 },
tests: [
{ value: 11, rc: 400, body: { error: 'type has a value not allowed' }.to_json },
{ value: 13, rc: 200, body: { type: 13 }.to_json }
]
}
}.freeze
module ExceptValidatorSpec
class API < Grape::API
default_format :json
TEST_CASES.each_with_index do |(k, v), _i|
params do
requires :type, v[:requires] if v.key? :requires
optional :type, v[:optional] if v.key? :optional
end
get k do
{ type: params[:type] }
end
end
end
end
end
it 'raises IncompatibleOptionValues on a default value in exclude' do
subject = Class.new(Grape::API)
expect do
subject.params do
optional :type, except_values: ValidationsSpec::ExceptValuesModel.excepts,
default: ValidationsSpec::ExceptValuesModel.excepts.sample
end
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
it 'raises IncompatibleOptionValues when a default array has excluded values' do
subject = Class.new(Grape::API)
expect do
subject.params do
optional :type, type: Array[Integer],
except_values: 10..12,
default: [8, 9, 10]
end
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
it 'raises IncompatibleOptionValues when type is incompatible with values array' do
subject = Class.new(Grape::API)
expect do
subject.params { optional :type, except_values: %w[valid-type1 valid-type2 valid-type3], type: Symbol }
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
def app
ValidationsSpec::ExceptValidatorSpec::API
end
ValidationsSpec::TEST_CASES.each_with_index do |(k, v), i|
v[:tests].each do |t|
it "#{i}: #{k} - #{t[:value]}" do
ValidationsSpec::ExceptValuesModel.excepts = v[:add_excepts] if v.key? :add_excepts
body = {}
body[:type] = t[:value] if t.key? :value
get k.to_s, **body
expect(last_response.status).to eq t[:rc]
expect(last_response.body).to eq t[:body]
ValidationsSpec::ExceptValuesModel.excepts = nil
end
end
end
end
|