OSDN Git Service

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