OSDN Git Service

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