OSDN Git Service

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