OSDN Git Service

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