OSDN Git Service

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