3
3
RSpec . describe OmniAuth ::Strategies ::Identity do
4
4
attr_accessor :app
5
5
6
- let ( :auth_hash ) { last_response . headers [ 'env' ] [ 'omniauth.auth' ] }
7
- let ( :identity_hash ) { last_response . headers [ 'env' ] [ 'omniauth.identity' ] }
6
+ let ( :env_hash ) { last_response . headers [ 'env' ] }
7
+ let ( :auth_hash ) { env_hash [ 'omniauth.auth' ] }
8
+ let ( :identity_hash ) { env_hash [ 'omniauth.identity' ] }
8
9
let ( :identity_options ) { { } }
9
10
let ( :anon_ar ) do
10
11
AnonymousActiveRecord . generate (
@@ -193,7 +194,7 @@ def set_app!(identity_options = {})
193
194
end
194
195
end
195
196
196
- context 'with successful creation ' do
197
+ context 'with good identity ' do
197
198
let ( :properties ) do
198
199
{
199
200
name : 'Awesome Dude' ,
@@ -209,9 +210,66 @@ def set_app!(identity_options = {})
209
210
expect ( auth_hash [ 'uid' ] ) . to match ( /\d +/ )
210
211
expect ( auth_hash [ 'provider' ] ) . to eq ( 'identity' )
211
212
end
213
+
214
+ context 'with on_validation proc' do
215
+ let ( :identity_options ) do
216
+ { model : anon_ar , on_validation : on_validation_proc }
217
+ end
218
+ let ( :on_validation_proc ) do
219
+ lambda { |_env |
220
+ false
221
+ }
222
+ end
223
+
224
+ context 'when validation fails' do
225
+ it 'does not set the env hash' do
226
+ post '/auth/identity/register' , properties
227
+ expect ( env_hash ) . to eq ( nil )
228
+ end
229
+
230
+ it 'renders registration form' do
231
+ post '/auth/identity/register' , properties
232
+ expect ( last_response . body ) . to be_include ( described_class . default_options [ :registration_form_title ] )
233
+ end
234
+
235
+ it 'displays validation failure message' do
236
+ post '/auth/identity/register' , properties
237
+ expect ( last_response . body ) . to be_include ( described_class . default_options [ :validation_failure_message ] )
238
+ end
239
+ end
240
+
241
+ context 'when validation succeeds' do
242
+ let ( :on_validation_proc ) do
243
+ lambda { |_env |
244
+ true
245
+ }
246
+ end
247
+
248
+ it 'sets the auth hash' do
249
+ post '/auth/identity/register' , properties
250
+ expect ( auth_hash [ 'uid' ] ) . to match ( /\d +/ )
251
+ expect ( auth_hash [ 'provider' ] ) . to eq ( 'identity' )
252
+ end
253
+
254
+ it 'does not render registration form' do
255
+ post '/auth/identity/register' , properties
256
+ expect ( last_response . body ) . not_to be_include ( described_class . default_options [ :registration_form_title ] )
257
+ end
258
+
259
+ it 'does not display validation failure message' do
260
+ post '/auth/identity/register' , properties
261
+ expect ( last_response . body ) . not_to be_include ( described_class . default_options [ :validation_failure_message ] )
262
+ end
263
+
264
+ it 'does not display registration failure message' do
265
+ post '/auth/identity/register' , properties
266
+ expect ( last_response . body ) . not_to be_include ( described_class . default_options [ :registration_failure_message ] )
267
+ end
268
+ end
269
+ end
212
270
end
213
271
214
- context 'with invalid identity' do
272
+ context 'with bad identity' do
215
273
let ( :properties ) do
216
274
{
217
275
name : 'Awesome Dude' ,
@@ -249,6 +307,62 @@ def set_app!(identity_options = {})
249
307
expect ( last_response . body ) . not_to be_include ( 'One or more fields were invalid' )
250
308
end
251
309
end
310
+
311
+ context 'with on_validation proc' do
312
+ let ( :identity_options ) do
313
+ { model : anon_ar , on_validation : on_validation_proc }
314
+ end
315
+ let ( :on_validation_proc ) do
316
+ lambda { |_env |
317
+ false
318
+ }
319
+ end
320
+
321
+ context 'when validation fails' do
322
+ it 'does not set the env hash' do
323
+ post '/auth/identity/register' , properties
324
+ expect ( env_hash ) . to eq ( nil )
325
+ end
326
+
327
+ it 'renders registration form' do
328
+ post '/auth/identity/register' , properties
329
+ expect ( last_response . body ) . to be_include ( described_class . default_options [ :registration_form_title ] )
330
+ end
331
+
332
+ it 'displays validation failure message' do
333
+ post '/auth/identity/register' , properties
334
+ expect ( last_response . body ) . to be_include ( described_class . default_options [ :validation_failure_message ] )
335
+ end
336
+ end
337
+
338
+ context 'when validation succeeds' do
339
+ let ( :on_validation_proc ) do
340
+ lambda { |_env |
341
+ true
342
+ }
343
+ end
344
+
345
+ it 'does not set the env hash' do
346
+ post '/auth/identity/register' , properties
347
+ expect ( env_hash ) . to eq ( nil )
348
+ end
349
+
350
+ it 'renders registration form' do
351
+ post '/auth/identity/register' , properties
352
+ expect ( last_response . body ) . to be_include ( described_class . default_options [ :registration_form_title ] )
353
+ end
354
+
355
+ it 'does not display validation failure message' do
356
+ post '/auth/identity/register' , properties
357
+ expect ( last_response . body ) . not_to be_include ( described_class . default_options [ :validation_failure_message ] )
358
+ end
359
+
360
+ it 'display registration failure message' do
361
+ post '/auth/identity/register' , properties
362
+ expect ( last_response . body ) . to be_include ( described_class . default_options [ :registration_failure_message ] )
363
+ end
364
+ end
365
+ end
252
366
end
253
367
end
254
368
end
0 commit comments