OSDN Git Service

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