OSDN Git Service

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