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