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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
# frozen_string_literal: true
require 'spec_helper'
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
before do
subject.resource :bacons do
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
end
context 'with a custom validation message' do
before do
subject.resource :requires do
params do
requires :email, type: String, allow_blank: { value: false, message: 'has no value' }, regexp: { value: /^\S+$/, message: 'format is invalid' }, message: 'is required'
end
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
end
context 'with a required regexp parameter supplied in the POST body' do
before do
subject.format :json
subject.params do
requires :id, regexp: /^[0-9]+$/
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"}')
io = StringIO.new('{"id" : "a56b"}')
post '/', {}, 'rack.input' => io, 'CONTENT_TYPE' => 'application/json', 'CONTENT_LENGTH' => io.length
expect(last_response.body).to eq('{"error":"id is invalid"}')
expect(last_response.status).to eq(400)
io = StringIO.new('{"id" : 56}')
post '/', {}, 'rack.input' => io, 'CONTENT_TYPE' => 'application/json', 'CONTENT_LENGTH' => io.length
expect(last_response.body).to eq('{"ret":56}')
expect(last_response.status).to eq(201)
end
end
context 'with a required non-empty string' do
before do
subject.params do
requires :email, type: String, allow_blank: false, regexp: /^\S+$/
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
end
context 'with multiple parameters per requires' do
before do
subject.params do
requires :one, :two
end
subject.get '/single-requires' do
'Hello'
end
subject.params do
requires :one
requires :two
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
get '/multiple-requires'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq(single_requires_error)
end
end
context 'with required parameters and no type' do
before do
subject.params do
requires :name, :company
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"}')
get '/', name: 'Bob'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"company is missing"}')
get '/', name: 'Bob', company: 'TestCorp'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello'.to_json)
end
end
context 'with nested parameters' do
before do
subject.params do
requires :user, type: Hash do
requires :first_name
requires :last_name
end
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"}')
get '/nested', user: { first_name: 'Billy' }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"user[last_name] is missing"}')
get '/nested', user: { first_name: 'Billy', last_name: 'Bob' }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Nested'.to_json)
end
end
context 'with triply nested required parameters' do
before do
subject.params do
requires :admin, type: Hash do
requires :admin_name
requires :super, type: Hash do
requires :user, type: Hash do
requires :first_name
requires :last_name
end
end
end
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'
get '/nested_triple', user: { first_name: 'Billy' }
expect(last_response.status).to eq(400)
expect(last_response.body).to include '{"error":"admin is missing'
get '/nested_triple', admin: { super: { first_name: 'Billy' } }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"admin[admin_name] is missing, admin[super][user] is missing, admin[super][user][first_name] is missing, admin[super][user][last_name] is missing"}')
get '/nested_triple', super: { user: { first_name: 'Billy', last_name: 'Bob' } }
expect(last_response.status).to eq(400)
expect(last_response.body).to include '{"error":"admin is missing'
get '/nested_triple', admin: { super: { user: { first_name: 'Billy' } } }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"admin[admin_name] is missing, admin[super][user][last_name] is missing"}')
get '/nested_triple', admin: { admin_name: 'admin', super: { user: { first_name: 'Billy' } } }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"admin[super][user][last_name] is missing"}')
get '/nested_triple', admin: { admin_name: 'admin', super: { user: { first_name: 'Billy', last_name: 'Bob' } } }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Nested triple'.to_json)
end
end
context 'with reused parameter documentation once required and once optional' do
before do
docs = { name: { type: String, desc: 'some name' } }
subject.params do
requires :all, using: docs
end
subject.get '/required' do
'Hello required'
end
subject.params do
optional :all, using: docs
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)
get '/optional', name: 'Bob'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello optional'.to_json)
end
end
context 'with a custom type' do
it 'does not validate their type when it is missing' do
class CustomType
def self.parse(value)
return if value.blank?
new
end
end
subject.params do
requires :custom, type: CustomType
end
subject.get '/custom' do
'custom'
end
get 'custom'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"custom is missing"}')
get 'custom', custom: 'filled'
expect(last_response.status).to eq(200)
end
end
end
|