OSDN Git Service

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