OSDN Git Service

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