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