OSDN Git Service

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