OSDN Git Service

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