OSDN Git Service

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