OSDN Git Service

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