OSDN Git Service

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