OSDN Git Service

fix test
[pettanr/pettanr.git] / spec / controllers / scrolls_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #スクロール
4 describe ScrollsController do
5   before do
6     @admin =FactoryGirl.create :admin
7     @user_yas = FactoryGirl.create :user_yas_with_owner
8     @user_rom = FactoryGirl.create :user_rom
9     @author_yas =  @user_yas.author
10     
11     @my_model_class =  Scroll   #操作モデル
12   end
13
14 if Manifest.manifest.magic_numbers['run_mode'] == 1
15   describe '一覧表示に於いて' do
16     before do
17       factory_contents :scroll_hello, @author_yas
18       @query = { }
19       @query_format_json = @query.merge(:format => :json)
20     end
21     
22     context '正常系' do
23       before do
24         sign_in @user_yas
25       end
26       context '共通処理' do
27         before do
28           get :index, @query
29         end
30         it 'コントローラのマニフェストから操作モデルを導出している' do
31           set_model? @my_model_class
32         end
33         it 'コントローラのマニフェストからリスト操作モデルを導出している' do
34           set_list_model? @my_model_class
35         end
36         it 'リストグループのマニフェストからリストgetter(新着リスト)を導出している' do
37           set_list? 'scroll', 'public'
38         end
39         it '@itemsにリストを取得している' do
40           expect(assigns(:items).size).to be >= 3
41         end
42       end
43       context 'html形式' do
44         before do
45           get :index, @query
46         end
47         it 'ステータス OKを返して、ファイラーを描画している' do
48           return_ok?
49           render_filer?
50         end
51         it '@filerにファイラーを取得している' do
52           let_filer?
53         end
54       end
55       context 'json形式' do
56         before do
57           get :index, @query_format_json
58         end
59         it 'ステータス OKを返して、jsonデータを返している' do
60           return_ok?
61           render_json?
62         end
63         it '@filerにファイラーをセットしていない' do
64           expect(assigns(:filer)).to be nil
65         end
66       end
67     end
68     context '件数取得' do
69       context 'html形式' do
70         it '必要ないので実装していない' do
71           sign_in @user_yas
72           get :count, @query
73           expect(response.status).to eq(406)
74         end
75       end
76       context 'json形式' do
77         it 'ステータス OKを返して、jsonデータを返している' do
78           sign_in @user_yas
79           get :count, @query_format_json
80           return_ok?
81           render_json?
82         end
83       end
84       context 'ゲスト(サインインしていない)' do
85         it '通常通り応答する' do
86           get :count, @query_format_json
87           return_ok?
88           render_json?
89         end
90       end
91     end
92     context 'サインインの状態が例外的なとき' do
93       it 'ユーザではなく管理者でも通常通り応答している' do
94         sign_in @admin
95         get :index, @query
96         return_ok?
97         get :index, @query_format_json
98         render_json?
99       end
100       it '創作活動してないROM専の読者にも通常通り応答している' do
101         sign_in @user_rom
102         get :index, @query
103         return_ok?
104         get :index, @query_format_json
105         render_json?
106       end
107       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
108         get :index, @query
109         redirect? sign_in_url
110         get :index, @query_format_json
111         render_unauthorized?
112       end
113     end
114   end
115   describe '作家フィルタ一覧及びカウント' do
116     before do
117       factory_contents :scroll_hello, @author_yas
118       @query = {:id => @author_yas.id}
119       @query_format_json = @query.merge(:format => :json)
120     end
121     
122     context '正常系' do
123       before do
124         sign_in @user_yas
125       end
126       context '共通処理' do
127         before do
128           get :by_author, @query
129         end
130         it 'コントローラのマニフェストから操作モデルを導出している' do
131           set_model? @my_model_class
132         end
133         it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do
134           set_list? 'scroll', 'by_author'
135         end
136         it '@itemsにリストを取得している' do
137           expect(assigns(:items).size).to be >= 3
138         end
139       end
140       context 'html形式' do
141         before do
142           get :by_author, @query
143         end
144         it 'ステータス OKを返して、ファイラーを描画している' do
145           return_ok?
146           render_filer?
147         end
148         it '@filerにファイラーを取得している' do
149           let_filer?
150         end
151       end
152       context 'json形式' do
153         before do
154           get :by_author, @query_format_json
155         end
156         it 'ステータス OKを返して、jsonデータを返している' do
157           return_ok?
158           render_json?
159         end
160         it '@filerにファイラーをセットしていない' do
161           expect(assigns(:filer)).to be nil
162         end
163       end
164     end
165     context '件数取得' do
166       context 'html形式' do
167         it '必要ないので実装していない' do
168           sign_in @user_yas
169           get :count_by_author, @query
170           expect(response.status).to eq(406)
171         end
172       end
173       context 'json形式' do
174         it 'jsonデータを返す' do
175           sign_in @user_yas
176           get :count_by_author, @query_format_json
177           return_ok?
178           render_json?
179         end
180       end
181       context 'ゲスト(サインインしていない)' do
182         it '通常通り応答している' do
183           get :count_by_author, @query_format_json
184           return_ok?
185           render_json?
186         end
187       end
188     end
189     context 'サインインの状態が例外的なとき' do
190       it 'ユーザではなく管理者でも通常通り応答している' do
191         sign_in @admin
192         get :by_author, @query
193         return_ok?
194         get :by_author, @query_format_json
195         render_json?
196       end
197       it '創作活動してないROM専の読者にも通常通り応答している' do
198         sign_in @user_rom
199         return_ok?
200         get :by_author, @query_format_json
201         render_json?
202       end
203       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
204         get :by_author, @query
205         redirect? sign_in_url
206         get :by_author, @query_format_json
207         render_unauthorized?
208       end
209     end
210   end
211   
212   describe 'コマフィルタ一覧及びカウント' do
213     before do
214       @panel = FactoryGirl.create :panel_hello_with_scroll_panels, author_id: @author_yas.id
215       @query = {:id => @panel.id}
216       @query_format_json = @query.merge(:format => :json)
217     end
218     
219     context '正常系' do
220       before do
221         sign_in @user_yas
222       end
223       context '共通処理' do
224         before do
225           get :by_panel, @query
226         end
227         it 'コントローラのマニフェストから操作モデルを導出している' do
228           set_model? @my_model_class
229         end
230         it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do
231           set_list? 'scroll', 'by_panel'
232         end
233         it '@itemsにリストを取得している' do
234           expect(assigns(:items).size).to be >= 3
235         end
236       end
237       context 'html形式' do
238         before do
239           get :by_panel, @query
240         end
241         it 'ステータス OKを返して、ファイラーを描画している' do
242           return_ok?
243           render_filer?
244         end
245         it '@filerにファイラーを取得している' do
246           let_filer?
247         end
248       end
249       context 'json形式' do
250         before do
251           get :by_panel, @query_format_json
252         end
253         it 'ステータス OKを返して、jsonデータを返している' do
254           return_ok?
255           render_json?
256         end
257         it '@filerにファイラーをセットしていない' do
258           expect(assigns(:filer)).to be nil
259         end
260       end
261     end
262     context '件数取得' do
263       context 'html形式' do
264         it '必要ないので実装していない' do
265           sign_in @user_yas
266           get :count_by_panel, @query
267           expect(response.status).to eq(406)
268         end
269       end
270       context 'json形式' do
271         it 'jsonデータを返す' do
272           sign_in @user_yas
273           get :count_by_panel, @query_format_json
274           return_ok?
275           render_json?
276         end
277       end
278       context 'ゲスト(サインインしていない)' do
279         it '通常通り応答している' do
280           get :count_by_panel, @query_format_json
281           return_ok?
282           render_json?
283         end
284       end
285     end
286     context 'サインインの状態が例外的なとき' do
287       it 'ユーザではなく管理者でも通常通り応答している' do
288         sign_in @admin
289         get :by_panel, @query
290         return_ok?
291         get :by_panel, @query_format_json
292         render_json?
293       end
294       it '創作活動してないROM専の読者にも通常通り応答している' do
295         sign_in @user_rom
296         return_ok?
297         get :by_panel, @query_format_json
298         render_json?
299       end
300       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
301         get :by_panel, @query
302         redirect? sign_in_url
303         get :by_panel, @query_format_json
304         render_unauthorized?
305       end
306     end
307   end
308   
309   describe '単体表示に於いて' do
310     before do
311       @item =  FactoryGirl.create :scroll_hello, author: @author_yas
312       @item_id = @item.id
313       @query = {:id => @item_id}
314       @query_format_json = @query.merge(:format => :json)
315       @query_format_prof = @query.merge(:format => :prof)
316     end
317     context '正常系' do
318       before do
319         sign_in @user_yas
320       end
321       context '正常系の共通処理' do
322         before do
323           get :show, @query
324         end
325         it 'コントローラのマニフェストから操作モデルを導出している' do
326           set_model? @my_model_class
327         end
328         it '@itemにリクエストしたアイテムを取得している' do
329           expect(assigns(:item)).to eq(@item)
330         end
331       end
332       context 'html形式' do
333         before do
334           get :show, @query
335         end
336         it 'showテンプレートを描画している' do
337           return_ok?
338           expect(response).to render_template("show")
339         end
340         it 'プロファイラーは必要ないので@profilerに用意していない' do
341           expect(assigns(:profiler)).to be nil
342         end
343       end
344       context 'json形式' do
345         before do
346           get :show, @query_format_json
347         end
348         it 'jsonデータを返す' do
349           return_ok?
350           render_json?
351         end
352         it 'プロファイラーは必要ないので@profilerに用意していない' do
353           expect(assigns(:profiler)).to be nil
354         end
355       end
356       context 'prof形式' do
357         before do
358           get :show, @query_format_prof
359         end
360         it 'プロファイラーテンプレートを描画している' do
361           return_ok?
362           render_profiler?
363         end
364         it '@profilerにプロファイラーを取得している' do
365           let_profiler?
366         end
367       end
368     end
369     context 'サインインの状態が例外的なとき' do
370       it 'ユーザではなく管理者でも通常通り応答している' do
371         sign_in @admin
372         get :show, @query
373         return_ok?
374         get :show, @query_format_json
375         render_json?
376       end
377       it '創作活動してないROM専の読者にも通常通り応答している' do
378         sign_in @user_rom
379         get :show, @query
380         return_ok?
381         get :show, @query_format_json
382         render_json?
383       end
384       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
385         get :show, @query
386         redirect? sign_in_url
387         get :show, @query_format_json
388         render_unauthorized?
389       end
390     end
391   end
392   
393   describe 'プレイリストに於いて' do
394     before do
395       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
396       @item_id = @item.id
397       @query = {:id => @item_id}
398       @query_format_json = @query.merge(:format => :json)
399     end
400     context '正常系' do
401       before do
402         sign_in @user_yas
403       end
404       context '共通処理' do
405         before do
406           get :play, @query
407         end
408         it 'コントローラのマニフェストから操作モデルを導出している' do
409           set_model? @my_model_class
410         end
411         it '@itemにリクエストしたスクロールを取得している' do
412           expect(assigns(:item)).to eq(@item)
413         end
414         it 'コントローラのマニフェストからリスト操作モデルを導出している' do
415           # リクエストの対象はスクロールであるが、返されるのはスクコマのリストである。
416           # くれぐれも間違えないように。 
417           set_list_model? ScrollPanel
418         end
419         it 'リストグループのマニフェストからリストgetter(プレイリスト)を導出している' do
420           set_list? 'scroll_panel', 'play'
421         end
422         it '@itemsにプレイリストを取得している' do
423           set_play_list? ScrollPanel
424         end
425       end
426       context 'html形式' do
427         before do
428           get :play, @query
429         end
430         it 'playテンプレートを描画している' do
431           return_ok?
432           expect(response).to render_template("play")
433         end
434       end
435       context 'json形式' do
436         before do
437           get :play, @query_format_json
438         end
439         it 'jsonデータを返す' do
440           return_ok?
441           render_json?
442         end
443       end
444     end
445     context 'サインインの状態が例外的なとき' do
446       it 'ユーザではなく管理者でも通常通り応答している' do
447         sign_in @admin
448         get :play, @query
449         return_ok?
450         get :play, @query_format_json
451         render_json?
452       end
453       it '創作活動してないROM専の読者にも通常通り応答している' do
454         sign_in @user_rom
455         get :play, @query
456         return_ok?
457         get :play, @query_format_json
458         render_json?
459       end
460       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
461         get :play, @query
462         redirect? sign_in_url
463         get :play, @query_format_json
464         render_unauthorized?
465       end
466     end
467   end
468   
469   describe '新規作成フォーム表示に於いて' do
470     before do
471       @query = { }
472       @query_format_json = @query.merge(:format => :json)
473     end
474     context '正常系' do
475       before do
476         sign_in @user_yas
477       end
478       context '共通処理' do
479         before do
480           get :new, @query
481         end
482         it 'コントローラのマニフェストから操作モデルを導出している' do
483           set_model? @my_model_class
484         end
485         it '@itemに新規データを用意している' do
486           @item = assigns(:item)
487           expect(@item.is_a?(@my_model_class)).to be true
488           expect(@item.new_record?).to be true
489         end
490         it 'ブーストしている' do
491           boost? []
492         end
493       end
494       context '問い合わせ' do
495         it 'スクロールモデルにデフォルト値補充を依頼している' do
496           allow_any_instance_of(@my_model_class).to receive(:supply_default)
497           expect_any_instance_of(@my_model_class).to receive(:supply_default)
498           get :new, @query
499         end
500       end
501       context 'html形式' do
502         before do
503           get :new, @query
504         end
505         it 'formテンプレートを描画している' do
506           return_ok?
507           render_form?
508         end
509         it '@formに入力フォーム(Bucket)を取得している' do
510           set_bucket? assigns(:item)
511         end
512       end
513       context 'json形式' do
514         before do
515           get :new, @query_format_json
516         end
517         it 'jsonデータを返す' do
518           return_ok?
519           render_json?
520         end
521         it '入力フォームは必要ないので@formに用意していない' do
522           expect(assigns(:form)).to be nil
523         end
524       end
525     end
526     context 'サインインの状態が例外的なとき' do
527       before do
528       end
529       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
530         sign_in @user_rom
531         get :new, @query
532         redirect? new_author_path
533         announce_regist_author_for_json :get, @query_format_json, :new
534       end
535       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
536         get :new, @query
537         redirect? sign_in_url
538         get :new, @query_format_json
539         render_unauthorized?
540       end
541       it 'ユーザではなく管理者でもサインインページに遷移している' do
542         sign_in @admin
543         get :new, @query
544         redirect? sign_in_url
545         get :new, @query_format_json
546         render_unauthorized?
547       end
548     end
549   end
550
551   describe '新規作成に於いて' do
552     before do
553       @attributes = FactoryGirl.attributes_for(:scroll_hello, author_id: @author_yas.id)
554       @attr = {:scroll => @attributes}
555       @attr_format_json = @attr.merge(:format => :json)
556     end
557     context '正常系' do
558       before do
559         sign_in @user_yas
560       end
561       context '共通処理' do
562         before do
563           allow_save @my_model_class
564           post :create, @attr
565         end
566         it 'コントローラのマニフェストから操作モデルを導出している' do
567           set_model? @my_model_class
568         end
569         it '@itemに新規アイテムを用意している' do
570           @item = assigns(:item)
571           expect(@item.is_a?(@my_model_class)).to be true
572         end
573       end
574       context 'html形式' do
575         before do
576           post :create, @attr
577         end
578         it '作成されたアイテムの表示ページへ遷移している' do
579           redirect? @my_model_class.last
580         end
581       end
582       context 'json形式' do
583         before do
584           post :create, @attr_format_json
585         end
586         it 'ステータスコード200 OKを返して, 作成されたアイテムをjsonデータで返す' do
587           return_ok?
588           render_json?
589         end
590       end
591       context 'db' do
592         it '作成されている' do
593           expect {
594             post :create, @attr
595           }.to change {@my_model_class.count}.by(1)
596         end
597       end
598     end
599     context '検証、保存に失敗したとき' do
600       before do
601         sign_in @user_yas
602         reject_save @my_model_class
603       end
604       context 'html形式' do
605         before do
606           post :create, @attr
607         end
608         it 'ステータスコード200 OKを返して, formテンプレートを描画している' do
609           return_ok?
610           render_form?
611         end
612         it '@formに入力フォーム(Bucket)を取得している' do
613           set_bucket? assigns(:item)
614         end
615       end
616       context 'json形式' do
617         before do
618           post :create, @attr_format_json
619         end
620         it '処理不能を返している' do
621           render_unprocessable?
622         end
623       end
624     end
625     context 'サインインの状態が例外的なとき' do
626       before do
627         allow_save @my_model_class
628       end
629       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
630         sign_in @user_rom
631         post :create, @attr
632         redirect? new_author_path
633         announce_regist_author_for_json :post, @attr_format_json, :create
634       end
635       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
636         post :create, @attr
637         redirect? sign_in_url
638         post :create, @attr_format_json
639         render_unauthorized?
640       end
641       it 'ユーザではなく管理者でもサインインページに遷移している' do
642         sign_in @admin
643         post :create, @attr
644         redirect? sign_in_url
645         post :create, @attr_format_json
646         render_unauthorized?
647       end
648     end
649   end
650
651   describe '編集フォーム表示に於いて' do
652     before do
653       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
654       @item_id = @item.id
655       @query = {:id => @item_id}
656       @query_format_json = @query.merge(:format => :json)
657     end
658     context '正常系' do
659       before do
660         sign_in @user_yas
661       end
662       context '共通処理' do
663         before do
664           get :edit, @query
665         end
666         it 'コントローラのマニフェストから操作モデルを導出している' do
667           set_model? @my_model_class
668         end
669         it '@itemにリクエストしたアイテムを取得している' do
670           expect(assigns(:item).is_a?(@my_model_class)).to be true
671           expect(assigns(:item)).to eq(@item)
672         end
673         it 'ブーストしている' do
674           boost? []
675         end
676       end
677       context 'html形式' do
678         before do
679           get :edit, @query
680         end
681         it 'formテンプレートを描画している' do
682           return_ok?
683           render_form?
684         end
685         it '@formに入力フォーム(Bucket)を取得している' do
686           set_bucket? assigns(:item)
687         end
688       end
689       context 'json形式' do
690         before do
691           get :edit, @query_format_json
692         end
693         it 'jsonデータを返す' do
694           return_ok?
695           render_json?
696         end
697         it '入力フォームは必要ないので@formに用意していない' do
698           expect(assigns(:form)).to be nil
699         end
700       end
701     end
702     context 'サインインの状態が例外的なとき' do
703       before do
704         allow_save @my_model_class
705       end
706       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
707         sign_in @user_rom
708         get :edit, @query
709         redirect? new_author_path
710         announce_regist_author_for_json :get, @query_format_json, :edit
711       end
712       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
713         post :edit, @query
714         redirect? sign_in_url
715         post :edit, @query_format_json
716         render_unauthorized?
717       end
718       it 'ユーザではなく管理者でもサインインページに遷移している' do
719         sign_in @admin
720         post :edit, @query
721         redirect? sign_in_url
722         post :edit, @query_format_json
723         render_unauthorized?
724       end
725     end
726   end
727
728   describe '更新に於いて' do
729     before do
730       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
731       @item_id = @item.id
732       @attributes = FactoryGirl.attributes_for(:scroll_hidden, author_id: @author_yas.id)
733       @attr = {:id => @item_id, :scroll => @attributes}
734       @attr_format_json = @attr.merge(:format => :json)
735     end
736     context '正常系' do
737       before do
738         sign_in @user_yas
739       end
740       context '共通処理' do
741         before do
742           allow_save @my_model_class
743           put :update, @attr
744         end
745         it 'コントローラのマニフェストから操作モデルを導出している' do
746           set_model? @my_model_class
747         end
748         it '@itemにリクエストしたアイテムを取得している' do
749           expect(assigns(:item).is_a?(@my_model_class)).to be true
750           expect(assigns(:item)).to eq(@item)
751         end
752       end
753       context 'html形式' do
754         before do
755           allow_save @my_model_class
756           put :update, @attr
757         end
758         it 'リクエストしたアイテムの表示ページへ遷移している' do
759           redirect? @item
760         end
761       end
762       context 'json形式' do
763         before do
764           allow_save @my_model_class
765           put :update, @attr_format_json
766         end
767         it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do
768           return_ok?
769           render_complated?
770         end
771       end
772       context 'db' do
773         it '@itemの件数に変わりはない' do
774           expect {
775             put :update, @attr
776           }.to change {@my_model_class.count}.by(0)
777         end
778         it '@itemを更新している' do
779           put :update, @attr
780           # dbから取得しなおす
781           updated_item = @my_model_class.find @item_id
782           # 変化を確認できるカラムはモデルごとに違うので注意
783           expect(updated_item.visible).to eq(0)
784         end
785       end
786     end
787     context '検証、保存に失敗したとき' do
788       before do
789         sign_in @user_yas
790         reject_save @my_model_class
791       end
792       context 'html形式' do
793         before do
794           put :update, @attr
795         end
796         it 'ステータスコード200 OKを返して, formテンプレートを描画している' do
797           return_ok?
798           render_form?
799         end
800         it '@formに入力フォーム(Bucket)を取得している' do
801           set_bucket? assigns(:item)
802         end
803       end
804       context 'json形式' do
805         before do
806           put :update, @attr_format_json
807         end
808         it '処理不能を返している' do
809           render_unprocessable?
810         end
811         it '入力フォームは必要ないので@formに用意していない' do
812           expect(assigns(:form)).to be nil
813         end
814       end
815     end
816     context 'サインインの状態が例外的なとき' do
817       before do
818         allow_save @my_model_class
819       end
820       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
821         sign_in @user_rom
822         put :update, @attr
823         redirect? new_author_path
824         announce_regist_author_for_json :put, @attr_format_json, :update
825       end
826       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
827         put :update, @attr
828         redirect? sign_in_url
829         put :update, @attr_format_json
830         render_unauthorized?
831       end
832       it 'ユーザではなく管理者でもサインインページに遷移している' do
833         sign_in @admin
834         put :update, @attr
835         redirect? sign_in_url
836         put :update, @attr_format_json
837         render_unauthorized?
838       end
839     end
840   end
841
842   describe '削除に於いて' do
843     before do
844       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
845       @item_id = @item.id
846       @attr = {:id => @item_id}
847       @attr_format_json = @attr.merge(:format => :json)
848     end
849     context '正常系' do
850       before do
851         sign_in @user_yas
852       end
853       context '共通処理' do
854         before do
855           allow_destroy @my_model_class
856           delete :destroy, @attr
857         end
858         it 'コントローラのマニフェストから操作モデルを導出している' do
859           set_model? @my_model_class
860         end
861         it '@itemにリクエストしたアイテムを取得している' do
862           expect(assigns(:item).is_a?(@my_model_class)).to be true
863           expect(assigns(:item)).to eq(@item)
864         end
865       end
866       context 'html形式' do
867         before do
868           allow_destroy @my_model_class
869           delete :destroy, @attr
870         end
871         it 'マイスクロールの一覧ページへ遷移している' do
872           redirect? '/home/scrolls'
873         end
874       end
875       context 'json形式' do
876         before do
877           allow_destroy @my_model_class
878           delete :destroy, @attr_format_json
879         end
880         it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do
881           expect(response).to be_success
882           render_complated?
883         end
884       end
885       context 'db' do
886         it '削除されている' do
887           expect {
888             delete :destroy, @attr
889           }.to change {@my_model_class.count}.by(-1)
890         end
891         it 'ぶら下がるリーフも削除されている' do
892           expect {
893             delete :destroy, @attr
894           }.to change {ScrollPanel.count}.from(4).to(0)
895         end
896       end
897     end
898     context '検証、保存に失敗したとき' do
899       before do
900         sign_in @user_yas
901         reject_destroy @my_model_class
902       end
903       context 'html形式' do
904         before do
905           delete :destroy, @attr
906         end
907         it 'リクエストしたアイテムの詳細ページへ遷移している' do
908           redirect? @item
909         end
910       end
911       context 'json形式' do
912         before do
913           delete :destroy, @attr_format_json
914         end
915         it '処理不能を返している' do
916           render_unprocessable?
917         end
918       end
919     end
920     context 'サインインの状態が例外的なとき' do
921       before do
922         allow_destroy @my_model_class
923       end
924       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
925         sign_in @user_rom
926         delete :destroy, @attr
927         redirect? new_author_path
928         announce_regist_author_for_json :delete, @attr_format_json, :destroy
929       end
930       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
931         delete :destroy, @attr
932         redirect? sign_in_url
933         delete :destroy, @attr_format_json
934         render_unauthorized?
935       end
936       it 'ユーザではなく管理者でもサインインページに遷移している' do
937         sign_in @admin
938         delete :destroy, @attr
939         redirect? sign_in_url
940         delete :destroy, @attr_format_json
941         render_unauthorized?
942       end
943     end
944   end
945
946 else
947   describe '一覧表示に於いて' do
948     before do
949       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
950       Scroll.stub(:list).and_return([@scroll, @scroll, @scroll])
951       sign_in @user
952     end
953     context '正常系の共通処理' do
954       it 'ステータスコード200 OKを返す' do
955         get :index
956         response.should be_success 
957       end
958       context 'html形式' do
959         it 'indexテンプレートを描画する' do
960           get :index
961           response.should render_template("index")
962         end
963       end
964       context 'json形式' do
965         it 'jsonデータを返す' do
966           get :index, :format => :json
967           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
968         end
969       end
970     end
971     context 'ユーザ権限がないとき' do
972       before do
973         sign_out @user
974       end
975       it 'ステータスコード200 OKを返す' do
976         get :index
977         response.should be_success 
978       end
979       context 'html形式' do
980         it 'indexテンプレートを描画する' do
981           get :index
982           response.should render_template("index")
983         end
984       end
985       context 'json形式' do
986         it 'jsonデータを返す' do
987           get :index, :format => :json
988           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
989         end
990       end
991     end
992   end
993   
994   describe '単体表示に於いて' do
995     before do
996       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id, :title => 'normal'
997       Scroll.stub(:show).and_return(@scroll)
998       sign_in @user
999     end
1000     context '正常系の共通処理' do
1001       it 'ステータスコード200 OKを返す' do
1002         get :show, :id => @scroll.id
1003         response.should be_success
1004       end
1005       context 'html形式' do
1006         it 'showテンプレートを描画する' do
1007           get :show, :id => @scroll.id
1008           response.should render_template("show")
1009         end
1010       end
1011       context 'json形式' do
1012         it 'jsonデータを返す' do
1013           get :show, :id => @scroll.id, :format => :json
1014           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1015         end
1016       end
1017     end
1018     context 'ユーザ権限がないとき' do
1019       before do
1020         sign_out @user
1021       end
1022       it 'ステータスコード200 OKを返す' do
1023         get :show, :id => @scroll.id
1024         response.should be_success
1025       end
1026       context 'html形式' do
1027         it 'showテンプレートを描画する' do
1028           get :show, :id => @scroll.id
1029           response.should render_template("show")
1030         end
1031       end
1032       context 'json形式' do
1033         it 'jsonデータを返す' do
1034           get :show, :id => @scroll.id, :format => :json
1035           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1036         end
1037       end
1038     end
1039   end
1040   describe 'スクロール数取得に於いて' do
1041     before do
1042       Scroll.should_receive(:visible_count).and_return(3)
1043       sign_out @user
1044     end
1045     context '正常系の共通処理' do
1046       it 'ステータスコード200 OKを返す' do
1047         get :count, :format => :json
1048         response.should be_success 
1049       end
1050       context 'json形式' do
1051         it 'jsonデータを返す' do
1052           get :count, :format => :json
1053           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1054         end
1055       end
1056     end
1057   end
1058
1059   describe '新規作成フォーム表示に於いて' do
1060     before do
1061       sign_in @user
1062     end
1063     context '正常系の共通処理' do
1064       it 'ステータスコード200 OKを返す' do
1065         get :new
1066         response.should be_success 
1067       end
1068       context 'html形式' do
1069         it 'newテンプレートを描画する' do
1070           get :new
1071           response.should render_template("new")
1072         end
1073       end
1074       context 'js形式' do
1075         it 'new.jsテンプレートを描画する' do
1076           get :new, :format => :js
1077           response.should render_template("new")
1078         end
1079       end
1080       context 'json形式' do
1081         it 'jsonデータを返す' do
1082           get :new, :format => :json
1083           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1084         end
1085       end
1086     end
1087     context 'ユーザ権限がないとき' do
1088       before do
1089         sign_out @user
1090       end
1091       context 'html形式' do
1092         it 'ステータスコード302 Foundを返す' do
1093           get :new
1094           response.status.should eq 302
1095         end
1096         it 'サインインページへ遷移する' do
1097           get :new
1098           response.body.should redirect_to '/users/sign_in'
1099         end
1100       end
1101       context 'js形式' do
1102         it 'ステータスコード401 Unauthorizedを返す' do
1103           get :new, :format => :js
1104           response.status.should eq 401
1105         end
1106         it '応答メッセージにUnauthorizedを返す' do
1107           get :new, :format => :js
1108           response.message.should match(/Unauthorized/)
1109         end
1110       end
1111       context 'json形式' do
1112         it 'ステータスコード401 Unauthorizedを返す' do
1113           get :new, :format => :json
1114           response.status.should eq 401
1115         end
1116         it '応答メッセージにUnauthorizedを返す' do
1117           get :new, :format => :json
1118           response.message.should match(/Unauthorized/)
1119         end
1120       end
1121     end
1122   end
1123
1124   describe '新規作成に於いて' do
1125     before do
1126       sign_in @user
1127       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'normal')
1128     end
1129     context '正常系の共通処理' do
1130       context 'html形式' do
1131         it 'ステータスコード302 Foundを返す' do
1132           Scroll.any_instance.stub(:save).and_return(true)
1133           post :create, :scroll => @attr
1134           response.status.should eq 302
1135         end
1136         it '作成されたスクロールの表示ページへ遷移する' do
1137 #          Scroll.any_instance.stub(:save).and_return(true)
1138           post :create, :scroll => @attr
1139           response.should redirect_to(Scroll.last)
1140         end
1141       end
1142       context 'json形式' do
1143         it 'ステータスコード200 OKを返す' do
1144 #          Scroll.any_instance.stub(:save).and_return(true)
1145           post :create, :scroll => @attr, :format => :json
1146           response.should be_success 
1147         end
1148         it '作成されたスクロールをjsonデータで返す' do
1149           post :create, :scroll => @attr, :format => :json
1150           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1151         end
1152       end
1153     end
1154     context 'ユーザ権限がないとき' do
1155       before do
1156         sign_out @user
1157       end
1158       context 'html形式' do
1159         it 'ステータスコード302 Foundを返す' do
1160           post :create, :scroll => @attr
1161           response.status.should eq 302
1162         end
1163         it 'サインインページへ遷移する' do
1164           post :create, :scroll => @attr
1165           response.body.should redirect_to '/users/sign_in'
1166         end
1167       end
1168       context 'json形式' do
1169         it 'ステータスコード401 Unauthorizedを返す' do
1170           post :create, :scroll => @attr, :format => :json
1171           response.status.should eq 401
1172         end
1173         it '応答メッセージにUnauthorizedを返す' do
1174           post :create, :scroll => @attr, :format => :json
1175           response.message.should match(/Unauthorized/)
1176         end
1177       end
1178     end
1179   end
1180
1181   describe '編集フォーム表示に於いて' do
1182     before do
1183       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
1184       sign_in @user
1185       Scroll.stub(:edit).and_return(@scroll)
1186     end
1187     context '正常系の共通処理' do
1188       it 'ステータスコード200 OKを返す' do
1189         get :edit, :id => @scroll.id
1190         response.should be_success 
1191       end
1192       context 'html形式' do
1193         it 'editテンプレートを描画する' do
1194           get :edit, :id => @scroll.id
1195           response.should render_template("edit")
1196         end
1197       end
1198       context 'js形式' do
1199         it 'edit.jsテンプレートを描画する' do
1200           get :edit, :id => @scroll.id, :format => :js
1201           response.should render_template("edit")
1202         end
1203       end
1204     end
1205     context 'ユーザ権限がないとき' do
1206       before do
1207         sign_out @user
1208       end
1209       context 'html形式' do
1210         it 'ステータスコード302 Foundを返す' do
1211           get :edit, :id => @scroll.id
1212           response.status.should eq 302
1213         end
1214         it 'サインインページへ遷移する' do
1215           get :edit, :id => @scroll.id
1216           response.body.should redirect_to '/users/sign_in'
1217         end
1218       end
1219       context 'js形式' do
1220         it 'ステータスコード401 Unauthorizedを返す' do
1221           get :edit, :id => @scroll.id, :format => :js
1222           response.status.should eq 401
1223         end
1224         it '応答メッセージにUnauthorizedを返す' do
1225           get :edit, :id => @scroll.id, :format => :js
1226           response.message.should match(/Unauthorized/)
1227         end
1228       end
1229     end
1230   end
1231
1232   describe '更新に於いて' do
1233     before do
1234       @scroll = FactoryGirl.create :scroll, :author => @author
1235       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'updated title', :visible => 0)
1236       sign_in @user
1237     end
1238     context '正常系の共通処理' do
1239       context 'html形式' do
1240         it 'ステータスコード302 Foundを返す' do
1241           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1242           put :update, :id => @scroll.id, :scroll => @attr
1243           response.status.should eq 302
1244         end
1245         it '更新されたスクロールの表示ページへ遷移する' do
1246           put :update, :id => @scroll.id, :scroll => @attr
1247           response.should redirect_to(@scroll)
1248         end
1249       end
1250       context 'json形式' do
1251         it 'ステータスコード200 OKを返す' do
1252           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1253           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1254           response.should be_success 
1255         end
1256         it 'ページ本体は特に返さない' do
1257           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1258           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1259           response.body.should match /./
1260         end
1261       end
1262     end
1263     context 'ユーザ権限がないとき' do
1264       before do
1265         sign_out @user
1266       end
1267       it 'ステータスコード302 Foundを返す' do
1268         put :update, :id => @scroll.id, :scroll => @attr
1269         response.status.should eq 302
1270       end
1271       context 'html形式' do
1272         it 'サインインページへ遷移する' do
1273           put :update, :id => @scroll.id, :scroll => @attr
1274           response.body.should redirect_to '/users/sign_in'
1275         end
1276       end
1277     end
1278   end
1279
1280   describe '削除に於いて' do
1281     before do
1282       @scroll = FactoryGirl.create :scroll, :author => @author
1283       sign_in @user
1284     end
1285     context '正常系の共通処理' do
1286       context 'html形式' do
1287         before do
1288           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1289         end
1290         it 'ステータスコード302 Foundを返す' do
1291           delete :destroy, :id => @scroll.id
1292           response.status.should eq 302
1293         end
1294         it 'マイスクロールの一覧ページへ遷移する' do
1295           delete :destroy, :id => @scroll.id
1296           response.should redirect_to('/home/scroll')
1297         end
1298       end
1299       context 'json形式' do
1300         before do
1301           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1302         end
1303         it 'ステータスコード200 OKを返す' do
1304           delete :destroy, :id => @scroll.id, :format => :json
1305           response.should be_success 
1306         end
1307         it 'ページ本体は特に返さない' do
1308           delete :destroy, :id => @scroll.id, :format => :json
1309           response.body.should match /./
1310         end
1311       end
1312     end
1313     context 'ユーザ権限がないとき' do
1314       before do
1315         sign_out @user
1316       end
1317       it 'ステータスコード302 Foundを返す' do
1318         delete :destroy, :id => @scroll.id
1319         response.status.should eq 302
1320       end
1321       context 'html形式' do
1322         it 'サインインページへ遷移する' do
1323           delete :destroy, :id => @scroll.id
1324           response.body.should redirect_to '/users/sign_in'
1325         end
1326       end
1327       context 'json形式' do
1328         it '応答メッセージにUnauthorizedを返す' do
1329           delete :destroy, :id => @scroll.id, :format => :json
1330           response.message.should match(/Unauthorized/)
1331         end
1332       end
1333     end
1334   end
1335
1336 end
1337
1338 end