OSDN Git Service

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