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