OSDN Git Service

t#30443:add test for open mode
[pettanr/pettanr.git] / spec / controllers / authors_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #作家
4
5 describe AuthorsController do
6   before do
7     @admin =FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create( :user_yas)
12     @author = FactoryGirl.create :author, :user_id => @user.id
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14   end
15
16 if MagicNumber['run_mode'] == 1
17   describe '一覧表示に於いて' do
18     before do
19       Author.stub(:list).and_return([@author, @author, @author])
20       sign_in @user
21     end
22     context '事前チェックする' do
23       it '与えられたpageがセットされている' do
24         get :index, :page => 5
25         assigns(:page).should eq 5
26       end
27       it '省略されると@pageに1値が入る' do
28         get :index
29         assigns(:page).should eq 1
30       end
31       it '与えられたpage_sizeがセットされている' do
32         get :index, :page_size => 15
33         assigns(:page_size).should eq 15
34       end
35       it '省略されると@page_sizeにデフォルト値が入る' do
36         get :index
37         assigns(:page_size).should eq Author.default_page_size
38       end
39       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
40         get :index, :page_size => 1500
41         assigns(:page_size).should eq Author.max_page_size
42       end
43       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
44         get :index, :page_size => 0
45         assigns(:page_size).should eq Author.default_page_size
46       end
47     end
48     context 'つつがなく終わるとき' do
49       it 'ステータスコード200 OKを返す' do
50         get :index
51         response.should be_success 
52       end
53       it '作家モデルに一覧を問い合わせている' do
54         Author.should_receive(:list).exactly(1)
55         get :index
56       end
57       it '@authorsにリストを取得している' do
58         get :index
59         assigns(:authors).should have_at_least(3).items
60       end
61       context 'html形式' do
62         it 'indexテンプレートを描画する' do
63           get :index
64           response.should render_template("index")
65         end
66       end
67       context 'json形式' do
68         it 'jsonデータを返す' do
69           get :index, :format => :json
70           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71         end
72         it '作家モデルにjson一覧出力オプションを問い合わせている' do
73           Author.should_receive(:list_json_opt).exactly(1)
74           get :index, :format => :json
75         end
76         it 'データがリスト構造になっている' do
77           get :index, :format => :json
78           json = JSON.parse response.body
79           json.should have_at_least(3).items
80         end
81         it 'リストの先頭くらいは作家っぽいものであって欲しい' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.first.has_key?("name").should be_true
85           json.first.has_key?("user_id").should be_true
86         end
87       end
88     end
89     context '作家権限がないとき' do
90       before do
91         sign_out @user
92       end
93       context 'html形式' do
94         it 'ステータスコード302 Foundを返す' do
95           get :index
96           response.status.should eq 302
97         end
98         it 'サインインページへ遷移する' do
99           get :index
100           response.should redirect_to '/users/sign_in'
101         end
102       end
103       context 'json形式' do
104         it 'ステータスコード401 Unauthorizedを返す' do
105           get :index, :format => :json
106           response.status.should eq 401
107         end
108         it '応答メッセージにUnauthorizedを返す' do
109           get :index, :format => :json
110           response.message.should match(/Unauthorized/)
111         end
112       end
113     end
114   end
115   
116   describe '閲覧に於いて' do
117     before do
118       Author.stub(:show).and_return(@author)
119       sign_in @user
120     end
121     context 'つつがなく終わるとき' do
122       it 'ステータスコード200 OKを返す' do
123         get :show, :id => @author.id
124         response.should be_success
125       end
126       it '作家モデルに単体取得を問い合わせている' do
127         Author.should_receive(:show).exactly(1)
128         get :show
129       end
130       #@authorだとログイン中のアカウントと干渉してしまう。
131       it '@auにアレを取得している' do
132         get :show, :id => @author.id
133         assigns(:au).should eq(@author)
134       end
135       context 'html形式' do
136         it 'showテンプレートを描画する' do
137           get :show, :id => @author.id
138           response.should render_template("show")
139         end
140       end
141       context 'json形式' do
142         it 'jsonデータを返す' do
143           get :show, :id => @author.id, :format => :json
144           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
145         end
146         it '作家モデルにjson単体出力オプションを問い合わせている' do
147           Author.should_receive(:show_json_opt).exactly(1)
148           get :show, :id => @author.id, :format => :json
149         end
150         it 'データがアレになっている' do
151           get :show, :id => @author.id, :format => :json
152           json = JSON.parse response.body
153           json["name"].should match(/test/)
154           json["user_id"].should eq @author.user_id
155         end
156       end
157     end
158     context '作家権限がないとき' do
159       before do
160         sign_out @user
161       end
162       context 'html形式' do
163         it 'ステータスコード302 Foundを返す' do
164           get :show, :id => @author.id
165           response.status.should eq 302
166         end
167         it 'サインインページへ遷移する' do
168           get :show, :id => @author.id
169           response.body.should redirect_to '/users/sign_in'
170         end
171       end
172       context 'json形式' do
173         it 'ステータスコード401 Unauthorizedを返す' do
174           get :show, :id => @author.id, :format => :json
175           response.status.should eq 401
176         end
177         it '応答メッセージにUnauthorizedを返す' do
178           get :show, :id => @author.id, :format => :json
179           response.message.should match(/Unauthorized/)
180         end
181       end
182     end
183 =begin
184     context '対象作家がないとき' do
185       context 'html形式' do
186         it '例外404 not_foundを返す' do
187           lambda{
188             get :show, :id => 0
189           }.should raise_error(ActiveRecord::RecordNotFound)
190         end
191       end
192       context 'json形式' do
193         it '例外404 not_foundを返す' do
194           lambda{ 
195             get :show, :id => 0, :format => :json
196           }.should raise_error(ActiveRecord::RecordNotFound)
197         end
198       end
199     end
200 =end
201   end
202   
203   describe '作家数取得に於いて' do
204     before do
205       Author.should_receive(:visible_count).and_return(3)
206 #      sign_in @user
207     end
208     context 'つつがなく終わるとき' do
209       it 'ステータスコード200 OKを返す' do
210         get :count, :format => :json
211         response.should be_success 
212       end
213       context 'json形式' do
214         it 'jsonデータを返す' do
215           get :count, :format => :json
216           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
217         end
218         it 'データがHash構造になっていて作家数が3である' do
219           get :count, :format => :json
220           json = JSON.parse response.body
221           json["count"].should == 3
222         end
223       end
224     end
225   end
226   
227   describe '新規作成フォーム表示に於いて' do
228     before do
229       @new_user = FactoryGirl.create( :user_yas)
230       sign_in @new_user
231     end
232     context 'つつがなく終わるとき' do
233       it 'ステータスコード200 OKを返す' do
234         get :new
235         response.should be_success 
236       end
237       it '@auに新規データを用意している' do
238         get :new
239         assigns(:au).should be_a_new(Author)
240       end
241       it '作家モデルにデフォルト値補充を依頼している' do
242         Author.any_instance.should_receive(:supply_default).exactly(1)
243         get :new
244       end
245       context 'html形式' do
246         it 'newテンプレートを描画する' do
247           get :new
248           response.should render_template("new")
249         end
250       end
251       context 'js形式' do
252         it 'new.jsテンプレートを描画する' do
253           get :new, :format => :js
254           response.should render_template("new")
255         end
256       end
257       context 'json形式' do
258         it 'jsonデータを返す' do
259           get :new, :format => :json
260           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
261         end
262         it '作家モデルにjson単体出力オプションを問い合わせている' do
263           Author.should_receive(:show_json_opt).exactly(1)
264           get :new, :format => :json
265         end
266       end
267     end
268     context '作家権限がないとき' do
269       before do
270         sign_out @new_user
271       end
272       context 'html形式' do
273         it 'ステータスコード302 Foundを返す' do
274           get :new
275           response.status.should eq 302
276         end
277         it 'サインインページへ遷移する' do
278           get :new
279           response.body.should redirect_to '/users/sign_in'
280         end
281       end
282       context 'js形式' do
283         it 'ステータスコード401 Unauthorizedを返す' do
284           get :new, :format => :js
285           response.status.should eq 401
286         end
287         it '応答メッセージにUnauthorizedを返す' do
288           get :new, :format => :js
289           response.message.should match(/Unauthorized/)
290         end
291       end
292       context 'json形式' do
293         it 'ステータスコード401 Unauthorizedを返す' do
294           get :new, :format => :json
295           response.status.should eq 401
296         end
297         it '応答メッセージにUnauthorizedを返す' do
298           get :new, :format => :json
299           response.message.should match(/Unauthorized/)
300         end
301       end
302     end
303   end
304
305   describe '新規作成に於いて' do
306     before do
307       @new_user = FactoryGirl.create( :user_yas)
308       sign_in @new_user
309       @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
310     end
311     context '事前チェックしておく' do
312       it '作家モデルにデフォルト値補充を依頼している' do
313         Author.any_instance.should_receive(:supply_default).exactly(1)
314         post :create, :author => @attr
315       end
316       it '作家モデルにカラム値復元を依頼している' do
317         Author.any_instance.should_receive(:attributes=).exactly(1)
318         post :create, :author => @attr
319       end
320       it '作家モデルに上書き補充を依頼している' do
321         Author.any_instance.should_receive(:overwrite).exactly(1)
322         post :create, :author => @attr
323       end
324       it 'モデルに保存依頼する' do
325         Author.any_instance.should_receive(:save).exactly(1)
326         post :create, :author => @attr
327       end
328     end
329     context 'つつがなく終わるとき' do
330       it "@auに作成された作家を保持していて、それがDBにある" do
331         post :create, :author => @attr
332         assigns(:au).should be_a(Author)
333         assigns(:au).should be_persisted
334       end
335       context 'html形式' do
336         it 'ステータスコード302 Foundを返す' do
337           Author.any_instance.stub(:save).and_return(true)
338           post :create, :author => @attr
339           response.status.should eq 302
340         end
341         it 'トップページへ遷移する' do
342 #          Author.any_instance.stub(:save).and_return(true)
343           post :create, :author => @attr
344           response.should redirect_to(root_path)
345         end
346       end
347       context 'json形式' do
348         it 'ステータスコード200 OKを返す' do
349 #          Author.any_instance.stub(:save).and_return(true)
350           post :create, :author => @attr, :format => :json
351           response.should be_success 
352         end
353         it '作成された作家をjsonデータで返す' do
354           post :create, :author => @attr, :format => :json
355           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
356         end
357         it '作家モデルにjson単体出力オプションを問い合わせている' do
358           Author.should_receive(:show_json_opt).exactly(1)
359           post :create, :author => @attr, :format => :json
360         end
361         it 'データがアレになっている' do
362           post :create, :author => @attr, :format => :json
363           json = JSON.parse response.body
364           json["name"].should match(/ken/)
365         end
366       end
367     end
368     context '作家権限がないとき' do
369       before do
370         sign_out @new_user
371       end
372       context 'html形式' do
373         it 'ステータスコード302 Foundを返す' do
374           post :create, :author => @attr
375           response.status.should eq 302
376         end
377         it 'サインインページへ遷移する' do
378           post :create, :author => @attr
379           response.body.should redirect_to '/users/sign_in'
380         end
381       end
382       context 'json形式' do
383         it 'ステータスコード401 Unauthorizedを返す' do
384           post :create, :author => @attr, :format => :json
385           response.status.should eq 401
386         end
387         it '応答メッセージにUnauthorizedを返す' do
388           post :create, :author => @attr, :format => :json
389           response.message.should match(/Unauthorized/)
390         end
391       end
392     end
393     context '検証、保存に失敗した' do
394       before do
395         Author.any_instance.stub(:save).and_return(false)
396       end
397       it "未保存の作家を保持している" do
398         post :create, :author => @attr
399         assigns(:au).should be_a_new(Author)
400       end
401       context 'html形式' do
402         it 'ステータスコード200 OKを返す' do
403           post :create, :author => @attr
404           response.status.should eq 200
405         end
406         it '新規ページを描画する' do
407           post :create, :author => @attr
408           response.should render_template("new")
409         end
410       end
411       context 'json形式' do
412         it 'ステータスコード422 unprocessable_entity を返す' do
413           post :create, :author => @attr, :format => :json
414           response.status.should eq 422
415         end
416         it '応答メッセージUnprocessable Entityを返す' do
417           post :create, :author => @attr, :format => :json
418           response.message.should match(/Unprocessable/)
419         end
420       end
421     end
422   end
423
424   describe '編集フォーム表示に於いて' do
425     before do
426       sign_in @user
427       Author.stub(:edit).and_return(@author)
428     end
429     context 'つつがなく終わるとき' do
430       it 'ステータスコード200 OKを返す' do
431         get :edit, :id => @author.id
432         response.should be_success 
433       end
434       it '作家モデルに編集取得を問い合わせている' do
435         Author.should_receive(:edit).exactly(1)
436         get :edit, :id => @author.id
437       end
438       #@authorだとログイン中のアカウントと干渉してしまう。
439       it '@auにデータを用意している' do
440         get :edit, :id => @author.id
441         assigns(:au).should eq @author
442       end
443       context 'html形式' do
444         it 'editテンプレートを描画する' do
445           get :edit, :id => @author.id
446           response.should render_template("edit")
447         end
448       end
449       context 'js形式' do
450         it 'edit.jsテンプレートを描画する' do
451           get :edit, :id => @author.id, :format => :js
452           response.should render_template("edit")
453         end
454       end
455     end
456     context '作家権限がないとき' do
457       before do
458         sign_out @user
459       end
460       context 'html形式' do
461         it 'ステータスコード302 Foundを返す' do
462           get :edit, :id => @author.id
463           response.status.should eq 302
464         end
465         it 'サインインページへ遷移する' do
466           get :edit, :id => @author.id
467           response.body.should redirect_to '/users/sign_in'
468         end
469       end
470       context 'js形式' do
471         it 'ステータスコード401 Unauthorizedを返す' do
472           get :edit, :id => @author.id, :format => :js
473           response.status.should eq 401
474         end
475         it '応答メッセージにUnauthorizedを返す' do
476           get :edit, :id => @author.id, :format => :js
477           response.message.should match(/Unauthorized/)
478         end
479       end
480     end
481   end
482
483   describe '更新に於いて' do
484     before do
485       @attr = @author.attributes
486       @attr['name'] = 'ryu'
487       sign_in @user
488     end
489     context '事前チェックしておく' do
490       it '作家モデルに編集取得を問い合わせている' do
491         Author.stub(:edit).with(any_args()).and_return @author
492         Author.should_receive(:edit).exactly(1)
493         put :update, :id => @author.id, :author => @attr
494       end
495       it 'モデルにpostデータ転送を依頼する' do
496         Author.any_instance.stub(:attributes=).with(any_args)
497         Author.any_instance.should_receive(:attributes=).exactly(1)
498         put :update, :id => @author.id, :author => @attr
499       end
500       it 'モデルに上書き補充を依頼する' do
501         Author.any_instance.stub(:overwrite).with(any_args)
502         Author.any_instance.should_receive(:overwrite).exactly(1)
503         put :update, :id => @author.id, :author => @attr
504       end
505       it 'モデルに更新を依頼する' do
506         Author.any_instance.stub(:save).with(any_args).and_return true
507         Author.any_instance.should_receive(:save).exactly(1)
508         put :update, :id => @author.id, :author => @attr
509       end
510       it '@auにアレを取得している' do
511         put :update, :id => @author.id, :author => @attr
512         assigns(:au).should eq @author
513       end
514     end
515     context 'つつがなく終わるとき' do
516       it '更新される' do
517         put :update, :id => @author.id, :author => @attr
518         Author.find(@author.id).name.should eq 'ryu'
519       end
520       context 'html形式' do
521         it 'ステータスコード302 Foundを返す' do
522           Author.any_instance.stub(:save).with(any_args()).and_return(true)
523           put :update, :id => @author.id, :author => @attr
524           response.status.should eq 302
525         end
526         it '設定ページへ遷移する' do
527           put :update, :id => @author.id, :author => @attr
528           response.should redirect_to('/home/configure')
529         end
530       end
531       context 'json形式' do
532         it 'ステータスコード200 OKを返す' do
533           Author.any_instance.stub(:save).with(any_args()).and_return(true)
534           put :update, :id => @author.id, :author => @attr, :format => :json
535           response.should be_success 
536         end
537         it 'ページ本体は特に返さない' do
538           Author.any_instance.stub(:save).with(any_args()).and_return(true)
539           put :update, :id => @author.id, :author => @attr, :format => :json
540           response.body.should match /./
541         end
542       end
543     end
544     context '作家権限がないとき' do
545       before do
546         sign_out @user
547       end
548       it 'ステータスコード302 Foundを返す' do
549         put :update, :id => @author.id, :author => @attr
550         response.status.should eq 302
551       end
552       context 'html形式' do
553         it 'サインインページへ遷移する' do
554           put :update, :id => @author.id, :author => @attr
555           response.body.should redirect_to '/users/sign_in'
556         end
557       end
558       context 'json形式' do
559         it '応答メッセージにUnauthorizedを返す' do
560           put :update, :id => @author.id, :author => @attr, :format => :json
561           response.message.should match(/Unauthorized/)
562         end
563       end
564     end
565     context '検証、保存に失敗したとき' do
566       before do
567         Author.any_instance.stub(:save).and_return(false)
568       end
569       context 'html形式' do
570         it 'ステータスコード200 Okを返す' do
571           put :update, :id => @author.id, :author => @attr
572           response.status.should eq 200
573         end
574         it '編集ページを描画する' do
575           put :update, :id => @author.id, :author => @attr
576           response.should render_template("edit")
577         end
578       end
579       context 'json形式' do
580         it 'ステータスコード422 unprocessable_entity を返す' do
581           Author.any_instance.stub(:save).and_return(false)
582           put :update, :id => @author.id, :author => @attr, :format => :json
583           response.status.should eq 422
584         end
585         it '応答メッセージUnprocessable Entityを返す' do
586           put :update, :id => @author.id, :author => @attr, :format => :json
587           response.message.should match(/Unprocessable/)
588         end
589       end
590     end
591   end
592
593 else
594   describe '一覧表示に於いて' do
595     before do
596       Author.stub(:list).and_return([@author, @author, @author])
597       sign_in @user
598     end
599     context 'つつがなく終わるとき' do
600       it 'ステータスコード200 OKを返す' do
601         get :index
602         response.should be_success 
603       end
604       context 'html形式' do
605         it 'indexテンプレートを描画する' do
606           get :index
607           response.should render_template("index")
608         end
609       end
610       context 'json形式' do
611         it 'jsonデータを返す' do
612           get :index, :format => :json
613           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
614         end
615       end
616     end
617     context '作家権限がないとき' do
618       before do
619         sign_out @user
620       end
621       it 'ステータスコード200 OKを返す' do
622         get :index
623         response.should be_success 
624       end
625       context 'html形式' do
626         it 'indexテンプレートを描画する' do
627           get :index
628           response.should render_template("index")
629         end
630       end
631       context 'json形式' do
632         it 'jsonデータを返す' do
633           get :index, :format => :json
634           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
635         end
636       end
637     end
638   end
639   
640   describe '閲覧に於いて' do
641     before do
642       Author.stub(:show).and_return(@author)
643       sign_in @user
644     end
645     context 'つつがなく終わるとき' do
646       it 'ステータスコード200 OKを返す' do
647         get :show, :id => @author.id
648         response.should be_success
649       end
650       context 'html形式' do
651         it 'showテンプレートを描画する' do
652           get :show, :id => @author.id
653           response.should render_template("show")
654         end
655       end
656       context 'json形式' do
657         it 'jsonデータを返す' do
658           get :show, :id => @author.id, :format => :json
659           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
660         end
661       end
662     end
663     context '作家権限がないとき' do
664       before do
665         sign_out @user
666       end
667       it 'ステータスコード200 OKを返す' do
668         get :show, :id => @author.id
669         response.should be_success
670       end
671       context 'html形式' do
672         it 'showテンプレートを描画する' do
673           get :show, :id => @author.id
674           response.should render_template("show")
675         end
676       end
677       context 'json形式' do
678         it 'jsonデータを返す' do
679           get :show, :id => @author.id, :format => :json
680           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
681         end
682       end
683     end
684   end
685   
686   describe '作家数取得に於いて' do
687     before do
688       Author.should_receive(:visible_count).and_return(3)
689 #      sign_in @user
690     end
691     context 'つつがなく終わるとき' do
692       it 'ステータスコード200 OKを返す' do
693         get :count, :format => :json
694         response.should be_success 
695       end
696       context 'json形式' do
697         it 'jsonデータを返す' do
698           get :count, :format => :json
699           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
700         end
701       end
702     end
703   end
704   
705   describe '新規作成フォーム表示に於いて' do
706     before do
707       @new_user = FactoryGirl.create( :user_yas)
708       sign_in @new_user
709     end
710     context 'つつがなく終わるとき' do
711       it 'ステータスコード200 OKを返す' do
712         get :new
713         response.should be_success 
714       end
715       context 'html形式' do
716         it 'newテンプレートを描画する' do
717           get :new
718           response.should render_template("new")
719         end
720       end
721       context 'js形式' do
722         it 'new.jsテンプレートを描画する' do
723           get :new, :format => :js
724           response.should render_template("new")
725         end
726       end
727       context 'json形式' do
728         it 'jsonデータを返す' do
729           get :new, :format => :json
730           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
731         end
732       end
733     end
734     context '作家権限がないとき' do
735       before do
736         sign_out @new_user
737       end
738       context 'html形式' do
739         it 'ステータスコード302 Foundを返す' do
740           get :new
741           response.status.should eq 302
742         end
743         it 'サインインページへ遷移する' do
744           get :new
745           response.body.should redirect_to '/users/sign_in'
746         end
747       end
748       context 'js形式' do
749         it 'ステータスコード401 Unauthorizedを返す' do
750           get :new, :format => :js
751           response.status.should eq 401
752         end
753         it '応答メッセージにUnauthorizedを返す' do
754           get :new, :format => :js
755           response.message.should match(/Unauthorized/)
756         end
757       end
758       context 'json形式' do
759         it 'ステータスコード401 Unauthorizedを返す' do
760           get :new, :format => :json
761           response.status.should eq 401
762         end
763         it '応答メッセージにUnauthorizedを返す' do
764           get :new, :format => :json
765           response.message.should match(/Unauthorized/)
766         end
767       end
768     end
769   end
770
771   describe '新規作成に於いて' do
772     before do
773       @new_user = FactoryGirl.create( :user_yas)
774       sign_in @new_user
775       @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
776     end
777     context 'つつがなく終わるとき' do
778       context 'html形式' do
779         it 'ステータスコード302 Foundを返す' do
780           Author.any_instance.stub(:save).and_return(true)
781           post :create, :author => @attr
782           response.status.should eq 302
783         end
784         it 'トップページへ遷移する' do
785 #          Author.any_instance.stub(:save).and_return(true)
786           post :create, :author => @attr
787           response.should redirect_to(root_path)
788         end
789       end
790       context 'json形式' do
791         it 'ステータスコード200 OKを返す' do
792 #          Author.any_instance.stub(:save).and_return(true)
793           post :create, :author => @attr, :format => :json
794           response.should be_success 
795         end
796       end
797     end
798     context '作家権限がないとき' do
799       before do
800         sign_out @new_user
801       end
802       context 'html形式' do
803         it 'ステータスコード302 Foundを返す' do
804           post :create, :author => @attr
805           response.status.should eq 302
806         end
807         it 'サインインページへ遷移する' do
808           post :create, :author => @attr
809           response.body.should redirect_to '/users/sign_in'
810         end
811       end
812       context 'json形式' do
813         it 'ステータスコード401 Unauthorizedを返す' do
814           post :create, :author => @attr, :format => :json
815           response.status.should eq 401
816         end
817         it '応答メッセージにUnauthorizedを返す' do
818           post :create, :author => @attr, :format => :json
819           response.message.should match(/Unauthorized/)
820         end
821       end
822     end
823   end
824
825   describe '編集フォーム表示に於いて' do
826     before do
827       sign_in @user
828       Author.stub(:edit).and_return(@author)
829     end
830     context 'つつがなく終わるとき' do
831       it 'ステータスコード200 OKを返す' do
832         get :edit, :id => @author.id
833         response.should be_success 
834       end
835       context 'html形式' do
836         it 'editテンプレートを描画する' do
837           get :edit, :id => @author.id
838           response.should render_template("edit")
839         end
840       end
841       context 'js形式' do
842         it 'edit.jsテンプレートを描画する' do
843           get :edit, :id => @author.id, :format => :js
844           response.should render_template("edit")
845         end
846       end
847     end
848     context '作家権限がないとき' do
849       before do
850         sign_out @user
851       end
852       context 'html形式' do
853         it 'ステータスコード302 Foundを返す' do
854           get :edit, :id => @author.id
855           response.status.should eq 302
856         end
857         it 'サインインページへ遷移する' do
858           get :edit, :id => @author.id
859           response.body.should redirect_to '/users/sign_in'
860         end
861       end
862       context 'js形式' do
863         it 'ステータスコード401 Unauthorizedを返す' do
864           get :edit, :id => @author.id, :format => :js
865           response.status.should eq 401
866         end
867         it '応答メッセージにUnauthorizedを返す' do
868           get :edit, :id => @author.id, :format => :js
869           response.message.should match(/Unauthorized/)
870         end
871       end
872     end
873   end
874
875   describe '更新に於いて' do
876     before do
877       @attr = @author.attributes
878       @attr['name'] = 'ryu'
879       sign_in @user
880     end
881     context 'つつがなく終わるとき' do
882       it '更新される' do
883         put :update, :id => @author.id, :author => @attr
884         Author.find(@author.id).name.should eq 'ryu'
885       end
886       context 'html形式' do
887         it 'ステータスコード302 Foundを返す' do
888           Author.any_instance.stub(:save).with(any_args()).and_return(true)
889           put :update, :id => @author.id, :author => @attr
890           response.status.should eq 302
891         end
892         it '設定ページへ遷移する' do
893           put :update, :id => @author.id, :author => @attr
894           response.should redirect_to('/home/configure')
895         end
896       end
897       context 'json形式' do
898         it 'ステータスコード200 OKを返す' do
899           Author.any_instance.stub(:save).with(any_args()).and_return(true)
900           put :update, :id => @author.id, :author => @attr, :format => :json
901           response.should be_success 
902         end
903         it 'ページ本体は特に返さない' do
904           Author.any_instance.stub(:save).with(any_args()).and_return(true)
905           put :update, :id => @author.id, :author => @attr, :format => :json
906           response.body.should match /./
907         end
908       end
909     end
910     context '作家権限がないとき' do
911       before do
912         sign_out @user
913       end
914       it 'ステータスコード302 Foundを返す' do
915         put :update, :id => @author.id, :author => @attr
916         response.status.should eq 302
917       end
918       context 'html形式' do
919         it 'サインインページへ遷移する' do
920           put :update, :id => @author.id, :author => @attr
921           response.body.should redirect_to '/users/sign_in'
922         end
923       end
924       context 'json形式' do
925         it '応答メッセージにUnauthorizedを返す' do
926           put :update, :id => @author.id, :author => @attr, :format => :json
927           response.message.should match(/Unauthorized/)
928         end
929       end
930     end
931   end
932
933 end
934 end