OSDN Git Service

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