OSDN Git Service

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