fortress 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,528 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/application'
3
+ require 'fixtures/controllers'
4
+
5
+ describe GuitarsController, type: :controller do
6
+ before { @flash_error = 'You are not authorised to access this page.' }
7
+
8
+ it 'should have a before filter `:prevent_access!`' do
9
+ before_filters = subject._process_action_callbacks.map do |callback|
10
+ callback.filter if callback.kind == :before
11
+ end.compact
12
+
13
+ expect(before_filters).to include(:prevent_access!)
14
+ end
15
+
16
+ context 'without allowing any actions' do
17
+ before { Fortress::Mechanism.initialize_authorisations }
18
+ describe 'GET index' do
19
+ it 'should redirect to the root_url and set a flash error message' do
20
+ get :index
21
+
22
+ expect(response).to redirect_to(root_url)
23
+ expect(flash[:error]).to eql(@flash_error)
24
+ end
25
+ end
26
+ describe 'GET show' do
27
+ it 'should redirect to the root_url and set a flash error message' do
28
+ get :show, id: 1
29
+
30
+ expect(response).to redirect_to(root_url)
31
+ expect(flash[:error]).to eql(@flash_error)
32
+ end
33
+ end
34
+ describe 'GET new' do
35
+ it 'should redirect to the root_url and set a flash error message' do
36
+ get :new
37
+
38
+ expect(response).to redirect_to(root_url)
39
+ expect(flash[:error]).to eql(@flash_error)
40
+ end
41
+ end
42
+ describe 'POST create' do
43
+ it 'should redirect to the root_url and set a flash error message' do
44
+ post :create
45
+
46
+ expect(response).to redirect_to(root_url)
47
+ expect(flash[:error]).to eql(@flash_error)
48
+ end
49
+ end
50
+ describe 'GET edit' do
51
+ it 'should redirect to the root_url and set a flash error message' do
52
+ post :edit, id: 1
53
+
54
+ expect(response).to redirect_to(root_url)
55
+ expect(flash[:error]).to eql(@flash_error)
56
+ end
57
+ end
58
+ describe 'PUT update' do
59
+ it 'should redirect to the root_url and set a flash error message' do
60
+ put :update, id: 1
61
+
62
+ expect(response).to redirect_to(root_url)
63
+ expect(flash[:error]).to eql(@flash_error)
64
+ end
65
+ end
66
+ describe 'PATCH update' do
67
+ it 'should redirect to the root_url and set a flash error message' do
68
+ patch :update, id: 1
69
+
70
+ expect(response).to redirect_to(root_url)
71
+ expect(flash[:error]).to eql(@flash_error)
72
+ end
73
+ end
74
+ describe 'POST destroy' do
75
+ it 'should redirect to the root_url and set a flash error message' do
76
+ post :destroy, id: 1
77
+
78
+ expect(response).to redirect_to(root_url)
79
+ expect(flash[:error]).to eql(@flash_error)
80
+ end
81
+ end
82
+ end
83
+
84
+ context 'allowing the index action only' do
85
+ before do
86
+ Fortress::Mechanism.initialize_authorisations
87
+ GuitarsController.fortress_allow :index
88
+ end
89
+ describe 'GET index' do
90
+ it 'should return a 200 HTTP code' do
91
+ get :index
92
+
93
+ expect(response).to_not redirect_to(root_url)
94
+ expect(flash[:error]).to be_nil
95
+ expect(response).to have_http_status(:ok)
96
+ end
97
+ end
98
+ describe 'GET show' do
99
+ it 'should redirect to the root_url and set a flash error message' do
100
+ get :show, id: 1
101
+
102
+ expect(response).to redirect_to(root_url)
103
+ expect(flash[:error]).to eql(@flash_error)
104
+ end
105
+ end
106
+ describe 'GET new' do
107
+ it 'should redirect to the root_url and set a flash error message' do
108
+ get :new
109
+
110
+ expect(response).to redirect_to(root_url)
111
+ expect(flash[:error]).to eql(@flash_error)
112
+ end
113
+ end
114
+ describe 'POST create' do
115
+ it 'should redirect to the root_url and set a flash error message' do
116
+ post :create
117
+
118
+ expect(response).to redirect_to(root_url)
119
+ expect(flash[:error]).to eql(@flash_error)
120
+ end
121
+ end
122
+ describe 'GET edit' do
123
+ it 'should redirect to the root_url and set a flash error message' do
124
+ post :edit, id: 1
125
+
126
+ expect(response).to redirect_to(root_url)
127
+ expect(flash[:error]).to eql(@flash_error)
128
+ end
129
+ end
130
+ describe 'PUT update' do
131
+ it 'should redirect to the root_url and set a flash error message' do
132
+ put :update, id: 1
133
+
134
+ expect(response).to redirect_to(root_url)
135
+ expect(flash[:error]).to eql(@flash_error)
136
+ end
137
+ end
138
+ describe 'PATCH update' do
139
+ it 'should redirect to the root_url and set a flash error message' do
140
+ patch :update, id: 1
141
+
142
+ expect(response).to redirect_to(root_url)
143
+ expect(flash[:error]).to eql(@flash_error)
144
+ end
145
+ end
146
+ describe 'POST destroy' do
147
+ it 'should redirect to the root_url and set a flash error message' do
148
+ post :destroy, id: 1
149
+
150
+ expect(response).to redirect_to(root_url)
151
+ expect(flash[:error]).to eql(@flash_error)
152
+ end
153
+ end
154
+ end
155
+
156
+ context 'allowing the index and show (using an Array) action only' do
157
+ before do
158
+ Fortress::Mechanism.initialize_authorisations
159
+ GuitarsController.fortress_allow [:index, :show]
160
+ end
161
+ describe 'GET index' do
162
+ it 'should return a 200 HTTP code' do
163
+ get :index
164
+
165
+ expect(response).to_not redirect_to(root_url)
166
+ expect(flash[:error]).to be_nil
167
+ expect(response).to have_http_status(:ok)
168
+ end
169
+ end
170
+ describe 'GET show' do
171
+ it 'should redirect to the root_url and set a flash error message' do
172
+ get :show, id: 1
173
+
174
+ expect(response).to_not be_redirect
175
+ expect(flash[:error]).to be_nil
176
+ expect(response).to have_http_status(:ok)
177
+ end
178
+ end
179
+ describe 'GET new' do
180
+ it 'should redirect to the root_url and set a flash error message' do
181
+ get :new
182
+
183
+ expect(response).to redirect_to(root_url)
184
+ expect(flash[:error]).to eql(@flash_error)
185
+ end
186
+ end
187
+ describe 'POST create' do
188
+ it 'should redirect to the root_url and set a flash error message' do
189
+ post :create
190
+
191
+ expect(response).to redirect_to(root_url)
192
+ expect(flash[:error]).to eql(@flash_error)
193
+ end
194
+ end
195
+ describe 'GET edit' do
196
+ it 'should redirect to the root_url and set a flash error message' do
197
+ post :edit, id: 1
198
+
199
+ expect(response).to redirect_to(root_url)
200
+ expect(flash[:error]).to eql(@flash_error)
201
+ end
202
+ end
203
+ describe 'PUT update' do
204
+ it 'should redirect to the root_url and set a flash error message' do
205
+ put :update, id: 1
206
+
207
+ expect(response).to redirect_to(root_url)
208
+ expect(flash[:error]).to eql(@flash_error)
209
+ end
210
+ end
211
+ describe 'PATCH update' do
212
+ it 'should redirect to the root_url and set a flash error message' do
213
+ patch :update, id: 1
214
+
215
+ expect(response).to redirect_to(root_url)
216
+ expect(flash[:error]).to eql(@flash_error)
217
+ end
218
+ end
219
+ describe 'POST destroy' do
220
+ it 'should redirect to the root_url and set a flash error message' do
221
+ post :destroy, id: 1
222
+
223
+ expect(response).to redirect_to(root_url)
224
+ expect(flash[:error]).to eql(@flash_error)
225
+ end
226
+ end
227
+ end
228
+
229
+ context 'allowing all actions using `:all`' do
230
+ before do
231
+ Fortress::Mechanism.initialize_authorisations
232
+ GuitarsController.fortress_allow :all
233
+ end
234
+ describe 'GET index' do
235
+ it 'should return a 200 HTTP code' do
236
+ get :index
237
+
238
+ expect(response).to_not redirect_to(root_url)
239
+ expect(flash[:error]).to be_nil
240
+ expect(response).to have_http_status(:ok)
241
+ end
242
+ end
243
+ describe 'GET show' do
244
+ it 'should return a 200 HTTP code' do
245
+ get :show, id: 1
246
+
247
+ expect(response).to_not be_redirect
248
+ expect(flash[:error]).to be_nil
249
+ expect(response).to have_http_status(:ok)
250
+ end
251
+ end
252
+ describe 'GET new' do
253
+ it 'should return a 200 HTTP code' do
254
+ get :new
255
+
256
+ expect(response).to_not redirect_to(root_url)
257
+ expect(flash[:error]).to be_nil
258
+ expect(response).to have_http_status(:ok)
259
+ end
260
+ end
261
+ describe 'POST create' do
262
+ it 'should return a 200 HTTP code' do
263
+ post :create
264
+
265
+ expect(response).to_not redirect_to(root_url)
266
+ expect(flash[:error]).to be_nil
267
+ expect(response).to have_http_status(:ok)
268
+ end
269
+ end
270
+ describe 'GET edit' do
271
+ it 'should return a 200 HTTP code' do
272
+ post :edit, id: 1
273
+
274
+ expect(response).to_not redirect_to(root_url)
275
+ expect(flash[:error]).to be_nil
276
+ expect(response).to have_http_status(:ok)
277
+ end
278
+ end
279
+ describe 'PUT update' do
280
+ it 'should return a 200 HTTP code' do
281
+ put :update, id: 1
282
+
283
+ expect(response).to_not redirect_to(root_url)
284
+ expect(flash[:error]).to be_nil
285
+ expect(response).to have_http_status(:ok)
286
+ end
287
+ end
288
+ describe 'PATCH update' do
289
+ it 'should return a 200 HTTP code' do
290
+ patch :update, id: 1
291
+
292
+ expect(response).to_not redirect_to(root_url)
293
+ expect(flash[:error]).to be_nil
294
+ expect(response).to have_http_status(:ok)
295
+ end
296
+ end
297
+ describe 'POST destroy' do
298
+ it 'should return a 200 HTTP code' do
299
+ post :destroy, id: 1
300
+
301
+ expect(response).to_not redirect_to(root_url)
302
+ expect(flash[:error]).to be_nil
303
+ expect(response).to have_http_status(:ok)
304
+ end
305
+ end
306
+ end
307
+
308
+ context 'allowing all actions excepted the create action' do
309
+ before do
310
+ Fortress::Mechanism.initialize_authorisations
311
+ GuitarsController.fortress_allow :all, except: :create
312
+ end
313
+ describe 'GET index' do
314
+ it 'should return a 200 HTTP code' do
315
+ get :index
316
+
317
+ expect(response).to_not redirect_to(root_url)
318
+ expect(flash[:error]).to be_nil
319
+ expect(response).to have_http_status(:ok)
320
+ end
321
+ end
322
+ describe 'GET show' do
323
+ it 'should return a 200 HTTP code' do
324
+ get :show, id: 1
325
+
326
+ expect(response).to_not redirect_to(root_url)
327
+ expect(flash[:error]).to be_nil
328
+ expect(response).to have_http_status(:ok)
329
+ end
330
+ end
331
+ describe 'GET new' do
332
+ it 'should return a 200 HTTP code' do
333
+ get :new
334
+
335
+ expect(response).to_not redirect_to(root_url)
336
+ expect(flash[:error]).to be_nil
337
+ expect(response).to have_http_status(:ok)
338
+ end
339
+ end
340
+ describe 'POST create' do
341
+ it 'should redirect to the root_url and set a flash error message' do
342
+ post :create
343
+
344
+ expect(response).to redirect_to(root_url)
345
+ expect(flash[:error]).to eql(@flash_error)
346
+ end
347
+ end
348
+ describe 'GET edit' do
349
+ it 'should return a 200 HTTP code' do
350
+ post :edit, id: 1
351
+
352
+ expect(response).to_not redirect_to(root_url)
353
+ expect(flash[:error]).to be_nil
354
+ expect(response).to have_http_status(:ok)
355
+ end
356
+ end
357
+ describe 'PUT update' do
358
+ it 'should return a 200 HTTP code' do
359
+ put :update, id: 1
360
+
361
+ expect(response).to_not redirect_to(root_url)
362
+ expect(flash[:error]).to be_nil
363
+ expect(response).to have_http_status(:ok)
364
+ end
365
+ end
366
+ describe 'PATCH update' do
367
+ it 'should return a 200 HTTP code' do
368
+ patch :update, id: 1
369
+
370
+ expect(response).to_not redirect_to(root_url)
371
+ expect(flash[:error]).to be_nil
372
+ expect(response).to have_http_status(:ok)
373
+ end
374
+ end
375
+ describe 'POST destroy' do
376
+ it 'should return a 200 HTTP code' do
377
+ post :destroy, id: 1
378
+
379
+ expect(response).to_not redirect_to(root_url)
380
+ expect(flash[:error]).to be_nil
381
+ expect(response).to have_http_status(:ok)
382
+ end
383
+ end
384
+ end
385
+
386
+ context 'allowing the index action with a condition returning true' do
387
+ before do
388
+ Fortress::Mechanism.initialize_authorisations
389
+ GuitarsController.fortress_allow :index, if: true
390
+ end
391
+ describe 'GET index' do
392
+ it 'should return a 200 HTTP code' do
393
+ get :index
394
+
395
+ expect(response).to_not redirect_to(root_url)
396
+ expect(flash[:error]).to be_nil
397
+ expect(response).to have_http_status(:ok)
398
+ end
399
+ end
400
+ describe 'GET show' do
401
+ it 'should redirect to the root_url and set a flash error message' do
402
+ get :show, id: 1
403
+
404
+ expect(response).to redirect_to(root_url)
405
+ expect(flash[:error]).to eql(@flash_error)
406
+ end
407
+ end
408
+ describe 'GET new' do
409
+ it 'should redirect to the root_url and set a flash error message' do
410
+ get :new
411
+
412
+ expect(response).to redirect_to(root_url)
413
+ expect(flash[:error]).to eql(@flash_error)
414
+ end
415
+ end
416
+ describe 'POST create' do
417
+ it 'should redirect to the root_url and set a flash error message' do
418
+ post :create
419
+
420
+ expect(response).to redirect_to(root_url)
421
+ expect(flash[:error]).to eql(@flash_error)
422
+ end
423
+ end
424
+ describe 'GET edit' do
425
+ it 'should redirect to the root_url and set a flash error message' do
426
+ post :edit, id: 1
427
+
428
+ expect(response).to redirect_to(root_url)
429
+ expect(flash[:error]).to eql(@flash_error)
430
+ end
431
+ end
432
+ describe 'PUT update' do
433
+ it 'should redirect to the root_url and set a flash error message' do
434
+ put :update, id: 1
435
+
436
+ expect(response).to redirect_to(root_url)
437
+ expect(flash[:error]).to eql(@flash_error)
438
+ end
439
+ end
440
+ describe 'PATCH update' do
441
+ it 'should redirect to the root_url and set a flash error message' do
442
+ patch :update, id: 1
443
+
444
+ expect(response).to redirect_to(root_url)
445
+ expect(flash[:error]).to eql(@flash_error)
446
+ end
447
+ end
448
+ describe 'POST destroy' do
449
+ it 'should redirect to the root_url and set a flash error message' do
450
+ post :destroy, id: 1
451
+
452
+ expect(response).to redirect_to(root_url)
453
+ expect(flash[:error]).to eql(@flash_error)
454
+ end
455
+ end
456
+ end
457
+
458
+ context 'allowing the index action with a condition returning false' do
459
+ before do
460
+ Fortress::Mechanism.initialize_authorisations
461
+ GuitarsController.fortress_allow :index, if: false
462
+ end
463
+ describe 'GET index' do
464
+ it 'should redirect to the root_url and set a flash error message' do
465
+ get :index
466
+
467
+ expect(response).to redirect_to(root_url)
468
+ expect(flash[:error]).to eql(@flash_error)
469
+ end
470
+ end
471
+ describe 'GET show' do
472
+ it 'should redirect to the root_url and set a flash error message' do
473
+ get :show, id: 1
474
+
475
+ expect(response).to redirect_to(root_url)
476
+ expect(flash[:error]).to eql(@flash_error)
477
+ end
478
+ end
479
+ describe 'GET new' do
480
+ it 'should redirect to the root_url and set a flash error message' do
481
+ get :new
482
+
483
+ expect(response).to redirect_to(root_url)
484
+ expect(flash[:error]).to eql(@flash_error)
485
+ end
486
+ end
487
+ describe 'POST create' do
488
+ it 'should redirect to the root_url and set a flash error message' do
489
+ post :create
490
+
491
+ expect(response).to redirect_to(root_url)
492
+ expect(flash[:error]).to eql(@flash_error)
493
+ end
494
+ end
495
+ describe 'GET edit' do
496
+ it 'should redirect to the root_url and set a flash error message' do
497
+ post :edit, id: 1
498
+
499
+ expect(response).to redirect_to(root_url)
500
+ expect(flash[:error]).to eql(@flash_error)
501
+ end
502
+ end
503
+ describe 'PUT update' do
504
+ it 'should redirect to the root_url and set a flash error message' do
505
+ put :update, id: 1
506
+
507
+ expect(response).to redirect_to(root_url)
508
+ expect(flash[:error]).to eql(@flash_error)
509
+ end
510
+ end
511
+ describe 'PATCH update' do
512
+ it 'should redirect to the root_url and set a flash error message' do
513
+ patch :update, id: 1
514
+
515
+ expect(response).to redirect_to(root_url)
516
+ expect(flash[:error]).to eql(@flash_error)
517
+ end
518
+ end
519
+ describe 'POST destroy' do
520
+ it 'should redirect to the root_url and set a flash error message' do
521
+ post :destroy, id: 1
522
+
523
+ expect(response).to redirect_to(root_url)
524
+ expect(flash[:error]).to eql(@flash_error)
525
+ end
526
+ end
527
+ end
528
+ end