OSDN Git Service

487548d86143826ee002949e28b16cefa9131e48
[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       @item =  FactoryGirl.create :scroll_hello, author: @author_yas
215       @item_id = @item.id
216       @query = {:id => @item_id}
217       @query_format_json = @query.merge(:format => :json)
218       @query_format_prof = @query.merge(:format => :prof)
219     end
220     context '正常系' do
221       before do
222         sign_in @user_yas
223       end
224       context '正常系の共通処理' do
225         before do
226           get :show, @query
227         end
228         it 'コントローラのマニフェストから操作モデルを導出している' do
229           set_model? @my_model_class
230         end
231         it '@itemにリクエストしたアイテムを取得している' do
232           expect(assigns(:item)).to eq(@item)
233         end
234       end
235       context 'html形式' do
236         before do
237           get :show, @query
238         end
239         it 'showテンプレートを描画している' do
240           return_ok?
241           expect(response).to render_template("show")
242         end
243         it 'プロファイラーは必要ないので@profilerに用意していない' do
244           expect(assigns(:profiler)).to be nil
245         end
246       end
247       context 'json形式' do
248         before do
249           get :show, @query_format_json
250         end
251         it 'jsonデータを返す' do
252           return_ok?
253           render_json?
254         end
255         it 'プロファイラーは必要ないので@profilerに用意していない' do
256           expect(assigns(:profiler)).to be nil
257         end
258       end
259       context 'prof形式' do
260         before do
261           get :show, @query_format_prof
262         end
263         it 'プロファイラーテンプレートを描画している' do
264           return_ok?
265           render_profiler?
266         end
267         it '@profilerにプロファイラーを取得している' do
268           let_profiler?
269         end
270       end
271     end
272     context 'サインインの状態が例外的なとき' do
273       it 'ユーザではなく管理者でも通常通り応答している' do
274         sign_in @admin
275         get :show, @query
276         return_ok?
277         get :show, @query_format_json
278         render_json?
279       end
280       it '創作活動してないROM専の読者にも通常通り応答している' do
281         sign_in @user_rom
282         get :show, @query
283         return_ok?
284         get :show, @query_format_json
285         render_json?
286       end
287       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
288         get :show, @query
289         redirect? sign_in_url
290         get :show, @query_format_json
291         render_unauthorized?
292       end
293     end
294   end
295   
296   describe 'プレイリストに於いて' do
297     before do
298       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
299       @item_id = @item.id
300       @query = {:id => @item_id}
301       @query_format_json = @query.merge(:format => :json)
302     end
303     context '正常系' do
304       before do
305         sign_in @user_yas
306       end
307       context '共通処理' do
308         before do
309           get :play, @query
310         end
311         it 'コントローラのマニフェストから操作モデルを導出している' do
312           set_model? @my_model_class
313         end
314         it '@itemにリクエストしたスクロールを取得している' do
315           expect(assigns(:item)).to eq(@item)
316         end
317         it 'コントローラのマニフェストからリスト操作モデルを導出している' do
318           # リクエストの対象はスクロールであるが、返されるのはスクコマのリストである。
319           # くれぐれも間違えないように。 
320           set_list_model? ScrollPanel
321         end
322         it 'リストグループのマニフェストからリストgetter(プレイリスト)を導出している' do
323           set_list? 'scroll_panel', 'play'
324         end
325         it '@itemsにプレイリストを取得している' do
326           set_play_list? ScrollPanel
327         end
328       end
329       context 'html形式' do
330         before do
331           get :play, @query
332         end
333         it 'playテンプレートを描画している' do
334           return_ok?
335           expect(response).to render_template("play")
336         end
337       end
338       context 'json形式' do
339         before do
340           get :play, @query_format_json
341         end
342         it 'jsonデータを返す' do
343           return_ok?
344           render_json?
345         end
346       end
347     end
348     context 'サインインの状態が例外的なとき' do
349       it 'ユーザではなく管理者でも通常通り応答している' do
350         sign_in @admin
351         get :play, @query
352         return_ok?
353         get :play, @query_format_json
354         render_json?
355       end
356       it '創作活動してないROM専の読者にも通常通り応答している' do
357         sign_in @user_rom
358         get :play, @query
359         return_ok?
360         get :play, @query_format_json
361         render_json?
362       end
363       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
364         get :play, @query
365         redirect? sign_in_url
366         get :play, @query_format_json
367         render_unauthorized?
368       end
369     end
370   end
371   
372   describe '新規作成フォーム表示に於いて' do
373     before do
374       @query = { }
375       @query_format_json = @query.merge(:format => :json)
376     end
377     context '正常系' do
378       before do
379         sign_in @user_yas
380       end
381       context '共通処理' do
382         before do
383           get :new, @query
384         end
385         it 'コントローラのマニフェストから操作モデルを導出している' do
386           set_model? @my_model_class
387         end
388         it '@itemに新規データを用意している' do
389           @item = assigns(:item)
390           expect(@item.is_a?(@my_model_class)).to be true
391           expect(@item.new_record?).to be true
392         end
393         it 'ブーストしている' do
394           boost? []
395         end
396       end
397       context '問い合わせ' do
398         it 'スクロールモデルにデフォルト値補充を依頼している' do
399           allow_any_instance_of(@my_model_class).to receive(:supply_default)
400           expect_any_instance_of(@my_model_class).to receive(:supply_default)
401           get :new, @query
402         end
403       end
404       context 'html形式' do
405         before do
406           get :new, @query
407         end
408         it 'formテンプレートを描画している' do
409           return_ok?
410           render_form?
411         end
412         it '@formに入力フォーム(Bucket)を取得している' do
413           set_bucket? assigns(:item)
414         end
415       end
416       context 'json形式' do
417         before do
418           get :new, @query_format_json
419         end
420         it 'jsonデータを返す' do
421           return_ok?
422           render_json?
423         end
424         it '入力フォームは必要ないので@formに用意していない' do
425           expect(assigns(:form)).to be nil
426         end
427       end
428     end
429     context 'サインインの状態が例外的なとき' do
430       before do
431       end
432       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
433         sign_in @user_rom
434         get :new, @query
435         redirect? new_author_path
436         announce_regist_author_for_json :get, @query_format_json, :new
437       end
438       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
439         get :new, @query
440         redirect? sign_in_url
441         get :new, @query_format_json
442         render_unauthorized?
443       end
444       it 'ユーザではなく管理者でもサインインページに遷移している' do
445         sign_in @admin
446         get :new, @query
447         redirect? sign_in_url
448         get :new, @query_format_json
449         render_unauthorized?
450       end
451     end
452   end
453
454   describe '新規作成に於いて' do
455     before do
456       @attributes = FactoryGirl.attributes_for(:scroll_hello, author_id: @author_yas.id)
457       @attr = {:scroll => @attributes}
458       @attr_format_json = @attr.merge(:format => :json)
459     end
460     context '正常系' do
461       before do
462         sign_in @user_yas
463       end
464       context '共通処理' do
465         before do
466           allow_save @my_model_class
467           post :create, @attr
468         end
469         it 'コントローラのマニフェストから操作モデルを導出している' do
470           set_model? @my_model_class
471         end
472         it '@itemに新規アイテムを用意している' do
473           @item = assigns(:item)
474           expect(@item.is_a?(@my_model_class)).to be true
475         end
476       end
477       context 'html形式' do
478         before do
479           post :create, @attr
480         end
481         it '作成されたアイテムの表示ページへ遷移している' do
482           redirect? @my_model_class.last
483         end
484       end
485       context 'json形式' do
486         before do
487           post :create, @attr_format_json
488         end
489         it 'ステータスコード200 OKを返して, 作成されたアイテムをjsonデータで返す' do
490           return_ok?
491           render_json?
492         end
493       end
494       context 'db' do
495         it '作成されている' do
496           expect {
497             post :create, @attr
498           }.to change {@my_model_class.count}.by(1)
499         end
500       end
501     end
502     context '検証、保存に失敗したとき' do
503       before do
504         sign_in @user_yas
505         reject_save @my_model_class
506       end
507       context 'html形式' do
508         before do
509           post :create, @attr
510         end
511         it 'ステータスコード200 OKを返して, formテンプレートを描画している' do
512           return_ok?
513           render_form?
514         end
515         it '@formに入力フォーム(Bucket)を取得している' do
516           set_bucket? assigns(:item)
517         end
518       end
519       context 'json形式' do
520         before do
521           post :create, @attr_format_json
522         end
523         it '処理不能を返している' do
524           render_unprocessable?
525         end
526       end
527     end
528     context 'サインインの状態が例外的なとき' do
529       before do
530         allow_save @my_model_class
531       end
532       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
533         sign_in @user_rom
534         post :create, @attr
535         redirect? new_author_path
536         announce_regist_author_for_json :post, @attr_format_json, :create
537       end
538       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
539         post :create, @attr
540         redirect? sign_in_url
541         post :create, @attr_format_json
542         render_unauthorized?
543       end
544       it 'ユーザではなく管理者でもサインインページに遷移している' do
545         sign_in @admin
546         post :create, @attr
547         redirect? sign_in_url
548         post :create, @attr_format_json
549         render_unauthorized?
550       end
551     end
552   end
553
554   describe '編集フォーム表示に於いて' do
555     before do
556       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
557       @item_id = @item.id
558       @query = {:id => @item_id}
559       @query_format_json = @query.merge(:format => :json)
560     end
561     context '正常系' do
562       before do
563         sign_in @user_yas
564       end
565       context '共通処理' do
566         before do
567           get :edit, @query
568         end
569         it 'コントローラのマニフェストから操作モデルを導出している' do
570           set_model? @my_model_class
571         end
572         it '@itemにリクエストしたアイテムを取得している' do
573           expect(assigns(:item).is_a?(@my_model_class)).to be true
574           expect(assigns(:item)).to eq(@item)
575         end
576         it 'ブーストしている' do
577           boost? []
578         end
579       end
580       context 'html形式' do
581         before do
582           get :edit, @query
583         end
584         it 'formテンプレートを描画している' do
585           return_ok?
586           render_form?
587         end
588         it '@formに入力フォーム(Bucket)を取得している' do
589           set_bucket? assigns(:item)
590         end
591       end
592       context 'json形式' do
593         before do
594           get :edit, @query_format_json
595         end
596         it 'jsonデータを返す' do
597           return_ok?
598           render_json?
599         end
600         it '入力フォームは必要ないので@formに用意していない' do
601           expect(assigns(:form)).to be nil
602         end
603       end
604     end
605     context 'サインインの状態が例外的なとき' do
606       before do
607         allow_save @my_model_class
608       end
609       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
610         sign_in @user_rom
611         get :edit, @query
612         redirect? new_author_path
613         announce_regist_author_for_json :get, @query_format_json, :edit
614       end
615       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
616         post :edit, @query
617         redirect? sign_in_url
618         post :edit, @query_format_json
619         render_unauthorized?
620       end
621       it 'ユーザではなく管理者でもサインインページに遷移している' do
622         sign_in @admin
623         post :edit, @query
624         redirect? sign_in_url
625         post :edit, @query_format_json
626         render_unauthorized?
627       end
628     end
629   end
630
631   describe '更新に於いて' do
632     before do
633       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
634       @item_id = @item.id
635       @attributes = FactoryGirl.attributes_for(:scroll_hidden, author_id: @author_yas.id)
636       @attr = {:id => @item_id, :scroll => @attributes}
637       @attr_format_json = @attr.merge(:format => :json)
638     end
639     context '正常系' do
640       before do
641         sign_in @user_yas
642       end
643       context '共通処理' do
644         before do
645           allow_save @my_model_class
646           put :update, @attr
647         end
648         it 'コントローラのマニフェストから操作モデルを導出している' do
649           set_model? @my_model_class
650         end
651         it '@itemにリクエストしたアイテムを取得している' do
652           expect(assigns(:item).is_a?(@my_model_class)).to be true
653           expect(assigns(:item)).to eq(@item)
654         end
655       end
656       context 'html形式' do
657         before do
658           allow_save @my_model_class
659           put :update, @attr
660         end
661         it 'リクエストしたアイテムの表示ページへ遷移している' do
662           redirect? @item
663         end
664       end
665       context 'json形式' do
666         before do
667           allow_save @my_model_class
668           put :update, @attr_format_json
669         end
670         it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do
671           return_ok?
672           render_complated?
673         end
674       end
675       context 'db' do
676         it '@itemの件数に変わりはない' do
677           expect {
678             put :update, @attr
679           }.to change {@my_model_class.count}.by(0)
680         end
681         it '@itemを更新している' do
682           put :update, @attr
683           # dbから取得しなおす
684           updated_item = @my_model_class.find @item_id
685           # 変化を確認できるカラムはモデルごとに違うので注意
686           expect(updated_item.visible).to eq(0)
687         end
688       end
689     end
690     context '検証、保存に失敗したとき' do
691       before do
692         sign_in @user_yas
693         reject_save @my_model_class
694       end
695       context 'html形式' do
696         before do
697           put :update, @attr
698         end
699         it 'ステータスコード200 OKを返して, formテンプレートを描画している' do
700           return_ok?
701           render_form?
702         end
703         it '@formに入力フォーム(Bucket)を取得している' do
704           set_bucket? assigns(:item)
705         end
706       end
707       context 'json形式' do
708         before do
709           put :update, @attr_format_json
710         end
711         it '処理不能を返している' do
712           render_unprocessable?
713         end
714         it '入力フォームは必要ないので@formに用意していない' do
715           expect(assigns(:form)).to be nil
716         end
717       end
718     end
719     context 'サインインの状態が例外的なとき' do
720       before do
721         allow_save @my_model_class
722       end
723       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
724         sign_in @user_rom
725         put :update, @attr
726         redirect? new_author_path
727         announce_regist_author_for_json :put, @attr_format_json, :update
728       end
729       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
730         put :update, @attr
731         redirect? sign_in_url
732         put :update, @attr_format_json
733         render_unauthorized?
734       end
735       it 'ユーザではなく管理者でもサインインページに遷移している' do
736         sign_in @admin
737         put :update, @attr
738         redirect? sign_in_url
739         put :update, @attr_format_json
740         render_unauthorized?
741       end
742     end
743   end
744
745   describe '削除に於いて' do
746     before do
747       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
748       @item_id = @item.id
749       @attr = {:id => @item_id}
750       @attr_format_json = @attr.merge(:format => :json)
751     end
752     context '正常系' do
753       before do
754         sign_in @user_yas
755       end
756       context '共通処理' do
757         before do
758           allow_destroy @my_model_class
759           delete :destroy, @attr
760         end
761         it 'コントローラのマニフェストから操作モデルを導出している' do
762           set_model? @my_model_class
763         end
764         it '@itemにリクエストしたアイテムを取得している' do
765           expect(assigns(:item).is_a?(@my_model_class)).to be true
766           expect(assigns(:item)).to eq(@item)
767         end
768       end
769       context 'html形式' do
770         before do
771           allow_destroy @my_model_class
772           delete :destroy, @attr
773         end
774         it 'マイスクロールの一覧ページへ遷移している' do
775           redirect? '/home/scrolls'
776         end
777       end
778       context 'json形式' do
779         before do
780           allow_destroy @my_model_class
781           delete :destroy, @attr_format_json
782         end
783         it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do
784           expect(response).to be_success
785           render_complated?
786         end
787       end
788       context 'db' do
789         it '削除されている' do
790           expect {
791             delete :destroy, @attr
792           }.to change {@my_model_class.count}.by(-1)
793         end
794         it 'ぶら下がるリーフも削除されている' do
795           expect {
796             delete :destroy, @attr
797           }.to change {ScrollPanel.count}.from(4).to(0)
798         end
799       end
800     end
801     context '検証、保存に失敗したとき' do
802       before do
803         sign_in @user_yas
804         reject_destroy @my_model_class
805       end
806       context 'html形式' do
807         before do
808           delete :destroy, @attr
809         end
810         it 'リクエストしたアイテムの詳細ページへ遷移している' do
811           redirect? @item
812         end
813       end
814       context 'json形式' do
815         before do
816           delete :destroy, @attr_format_json
817         end
818         it '処理不能を返している' do
819           render_unprocessable?
820         end
821       end
822     end
823     context 'サインインの状態が例外的なとき' do
824       before do
825         allow_destroy @my_model_class
826       end
827       it '創作活動してないROM専の読者は作家登録ページへ遷移している, 例外を発生させている' do
828         sign_in @user_rom
829         delete :destroy, @attr
830         redirect? new_author_path
831         announce_regist_author_for_json :delete, @attr_format_json, :destroy
832       end
833       it 'ゲスト(サインインしていない)はサインインページに遷移している' do
834         delete :destroy, @attr
835         redirect? sign_in_url
836         delete :destroy, @attr_format_json
837         render_unauthorized?
838       end
839       it 'ユーザではなく管理者でもサインインページに遷移している' do
840         sign_in @admin
841         delete :destroy, @attr
842         redirect? sign_in_url
843         delete :destroy, @attr_format_json
844         render_unauthorized?
845       end
846     end
847   end
848
849 else
850   describe '一覧表示に於いて' do
851     before do
852       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
853       Scroll.stub(:list).and_return([@scroll, @scroll, @scroll])
854       sign_in @user
855     end
856     context '正常系の共通処理' do
857       it 'ステータスコード200 OKを返す' do
858         get :index
859         response.should be_success 
860       end
861       context 'html形式' do
862         it 'indexテンプレートを描画する' do
863           get :index
864           response.should render_template("index")
865         end
866       end
867       context 'json形式' do
868         it 'jsonデータを返す' do
869           get :index, :format => :json
870           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
871         end
872       end
873     end
874     context 'ユーザ権限がないとき' do
875       before do
876         sign_out @user
877       end
878       it 'ステータスコード200 OKを返す' do
879         get :index
880         response.should be_success 
881       end
882       context 'html形式' do
883         it 'indexテンプレートを描画する' do
884           get :index
885           response.should render_template("index")
886         end
887       end
888       context 'json形式' do
889         it 'jsonデータを返す' do
890           get :index, :format => :json
891           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
892         end
893       end
894     end
895   end
896   
897   describe '単体表示に於いて' do
898     before do
899       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id, :title => 'normal'
900       Scroll.stub(:show).and_return(@scroll)
901       sign_in @user
902     end
903     context '正常系の共通処理' do
904       it 'ステータスコード200 OKを返す' do
905         get :show, :id => @scroll.id
906         response.should be_success
907       end
908       context 'html形式' do
909         it 'showテンプレートを描画する' do
910           get :show, :id => @scroll.id
911           response.should render_template("show")
912         end
913       end
914       context 'json形式' do
915         it 'jsonデータを返す' do
916           get :show, :id => @scroll.id, :format => :json
917           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
918         end
919       end
920     end
921     context 'ユーザ権限がないとき' do
922       before do
923         sign_out @user
924       end
925       it 'ステータスコード200 OKを返す' do
926         get :show, :id => @scroll.id
927         response.should be_success
928       end
929       context 'html形式' do
930         it 'showテンプレートを描画する' do
931           get :show, :id => @scroll.id
932           response.should render_template("show")
933         end
934       end
935       context 'json形式' do
936         it 'jsonデータを返す' do
937           get :show, :id => @scroll.id, :format => :json
938           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
939         end
940       end
941     end
942   end
943   describe 'スクロール数取得に於いて' do
944     before do
945       Scroll.should_receive(:visible_count).and_return(3)
946       sign_out @user
947     end
948     context '正常系の共通処理' do
949       it 'ステータスコード200 OKを返す' do
950         get :count, :format => :json
951         response.should be_success 
952       end
953       context 'json形式' do
954         it 'jsonデータを返す' do
955           get :count, :format => :json
956           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
957         end
958       end
959     end
960   end
961
962   describe '新規作成フォーム表示に於いて' do
963     before do
964       sign_in @user
965     end
966     context '正常系の共通処理' do
967       it 'ステータスコード200 OKを返す' do
968         get :new
969         response.should be_success 
970       end
971       context 'html形式' do
972         it 'newテンプレートを描画する' do
973           get :new
974           response.should render_template("new")
975         end
976       end
977       context 'js形式' do
978         it 'new.jsテンプレートを描画する' do
979           get :new, :format => :js
980           response.should render_template("new")
981         end
982       end
983       context 'json形式' do
984         it 'jsonデータを返す' do
985           get :new, :format => :json
986           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
987         end
988       end
989     end
990     context 'ユーザ権限がないとき' do
991       before do
992         sign_out @user
993       end
994       context 'html形式' do
995         it 'ステータスコード302 Foundを返す' do
996           get :new
997           response.status.should eq 302
998         end
999         it 'サインインページへ遷移する' do
1000           get :new
1001           response.body.should redirect_to '/users/sign_in'
1002         end
1003       end
1004       context 'js形式' do
1005         it 'ステータスコード401 Unauthorizedを返す' do
1006           get :new, :format => :js
1007           response.status.should eq 401
1008         end
1009         it '応答メッセージにUnauthorizedを返す' do
1010           get :new, :format => :js
1011           response.message.should match(/Unauthorized/)
1012         end
1013       end
1014       context 'json形式' do
1015         it 'ステータスコード401 Unauthorizedを返す' do
1016           get :new, :format => :json
1017           response.status.should eq 401
1018         end
1019         it '応答メッセージにUnauthorizedを返す' do
1020           get :new, :format => :json
1021           response.message.should match(/Unauthorized/)
1022         end
1023       end
1024     end
1025   end
1026
1027   describe '新規作成に於いて' do
1028     before do
1029       sign_in @user
1030       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'normal')
1031     end
1032     context '正常系の共通処理' do
1033       context 'html形式' do
1034         it 'ステータスコード302 Foundを返す' do
1035           Scroll.any_instance.stub(:save).and_return(true)
1036           post :create, :scroll => @attr
1037           response.status.should eq 302
1038         end
1039         it '作成されたスクロールの表示ページへ遷移する' do
1040 #          Scroll.any_instance.stub(:save).and_return(true)
1041           post :create, :scroll => @attr
1042           response.should redirect_to(Scroll.last)
1043         end
1044       end
1045       context 'json形式' do
1046         it 'ステータスコード200 OKを返す' do
1047 #          Scroll.any_instance.stub(:save).and_return(true)
1048           post :create, :scroll => @attr, :format => :json
1049           response.should be_success 
1050         end
1051         it '作成されたスクロールをjsonデータで返す' do
1052           post :create, :scroll => @attr, :format => :json
1053           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1054         end
1055       end
1056     end
1057     context 'ユーザ権限がないとき' do
1058       before do
1059         sign_out @user
1060       end
1061       context 'html形式' do
1062         it 'ステータスコード302 Foundを返す' do
1063           post :create, :scroll => @attr
1064           response.status.should eq 302
1065         end
1066         it 'サインインページへ遷移する' do
1067           post :create, :scroll => @attr
1068           response.body.should redirect_to '/users/sign_in'
1069         end
1070       end
1071       context 'json形式' do
1072         it 'ステータスコード401 Unauthorizedを返す' do
1073           post :create, :scroll => @attr, :format => :json
1074           response.status.should eq 401
1075         end
1076         it '応答メッセージにUnauthorizedを返す' do
1077           post :create, :scroll => @attr, :format => :json
1078           response.message.should match(/Unauthorized/)
1079         end
1080       end
1081     end
1082   end
1083
1084   describe '編集フォーム表示に於いて' do
1085     before do
1086       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
1087       sign_in @user
1088       Scroll.stub(:edit).and_return(@scroll)
1089     end
1090     context '正常系の共通処理' do
1091       it 'ステータスコード200 OKを返す' do
1092         get :edit, :id => @scroll.id
1093         response.should be_success 
1094       end
1095       context 'html形式' do
1096         it 'editテンプレートを描画する' do
1097           get :edit, :id => @scroll.id
1098           response.should render_template("edit")
1099         end
1100       end
1101       context 'js形式' do
1102         it 'edit.jsテンプレートを描画する' do
1103           get :edit, :id => @scroll.id, :format => :js
1104           response.should render_template("edit")
1105         end
1106       end
1107     end
1108     context 'ユーザ権限がないとき' do
1109       before do
1110         sign_out @user
1111       end
1112       context 'html形式' do
1113         it 'ステータスコード302 Foundを返す' do
1114           get :edit, :id => @scroll.id
1115           response.status.should eq 302
1116         end
1117         it 'サインインページへ遷移する' do
1118           get :edit, :id => @scroll.id
1119           response.body.should redirect_to '/users/sign_in'
1120         end
1121       end
1122       context 'js形式' do
1123         it 'ステータスコード401 Unauthorizedを返す' do
1124           get :edit, :id => @scroll.id, :format => :js
1125           response.status.should eq 401
1126         end
1127         it '応答メッセージにUnauthorizedを返す' do
1128           get :edit, :id => @scroll.id, :format => :js
1129           response.message.should match(/Unauthorized/)
1130         end
1131       end
1132     end
1133   end
1134
1135   describe '更新に於いて' do
1136     before do
1137       @scroll = FactoryGirl.create :scroll, :author => @author
1138       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'updated title', :visible => 0)
1139       sign_in @user
1140     end
1141     context '正常系の共通処理' do
1142       context 'html形式' do
1143         it 'ステータスコード302 Foundを返す' do
1144           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1145           put :update, :id => @scroll.id, :scroll => @attr
1146           response.status.should eq 302
1147         end
1148         it '更新されたスクロールの表示ページへ遷移する' do
1149           put :update, :id => @scroll.id, :scroll => @attr
1150           response.should redirect_to(@scroll)
1151         end
1152       end
1153       context 'json形式' do
1154         it 'ステータスコード200 OKを返す' do
1155           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1156           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1157           response.should be_success 
1158         end
1159         it 'ページ本体は特に返さない' do
1160           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1161           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1162           response.body.should match /./
1163         end
1164       end
1165     end
1166     context 'ユーザ権限がないとき' do
1167       before do
1168         sign_out @user
1169       end
1170       it 'ステータスコード302 Foundを返す' do
1171         put :update, :id => @scroll.id, :scroll => @attr
1172         response.status.should eq 302
1173       end
1174       context 'html形式' do
1175         it 'サインインページへ遷移する' do
1176           put :update, :id => @scroll.id, :scroll => @attr
1177           response.body.should redirect_to '/users/sign_in'
1178         end
1179       end
1180     end
1181   end
1182
1183   describe '削除に於いて' do
1184     before do
1185       @scroll = FactoryGirl.create :scroll, :author => @author
1186       sign_in @user
1187     end
1188     context '正常系の共通処理' do
1189       context 'html形式' do
1190         before do
1191           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1192         end
1193         it 'ステータスコード302 Foundを返す' do
1194           delete :destroy, :id => @scroll.id
1195           response.status.should eq 302
1196         end
1197         it 'マイスクロールの一覧ページへ遷移する' do
1198           delete :destroy, :id => @scroll.id
1199           response.should redirect_to('/home/scroll')
1200         end
1201       end
1202       context 'json形式' do
1203         before do
1204           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1205         end
1206         it 'ステータスコード200 OKを返す' do
1207           delete :destroy, :id => @scroll.id, :format => :json
1208           response.should be_success 
1209         end
1210         it 'ページ本体は特に返さない' do
1211           delete :destroy, :id => @scroll.id, :format => :json
1212           response.body.should match /./
1213         end
1214       end
1215     end
1216     context 'ユーザ権限がないとき' do
1217       before do
1218         sign_out @user
1219       end
1220       it 'ステータスコード302 Foundを返す' do
1221         delete :destroy, :id => @scroll.id
1222         response.status.should eq 302
1223       end
1224       context 'html形式' do
1225         it 'サインインページへ遷移する' do
1226           delete :destroy, :id => @scroll.id
1227           response.body.should redirect_to '/users/sign_in'
1228         end
1229       end
1230       context 'json形式' do
1231         it '応答メッセージにUnauthorizedを返す' do
1232           delete :destroy, :id => @scroll.id, :format => :json
1233           response.message.should match(/Unauthorized/)
1234         end
1235       end
1236     end
1237   end
1238
1239 end
1240
1241 end