OSDN Git Service

fix: any
[pettanr/pettanr.git] / spec / controllers / resource_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #素材
3 require 'spec_helper'
4
5 describe ResourcePicturesController 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     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
16   end
17
18 if MagicNumber['run_mode'] == 1
19   describe '一覧表示に於いて' do
20     before do
21       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
22       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
23       sign_in @user
24       ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
25     end
26     context 'パラメータpageについて' do
27       it '@pageに値が入る' do
28         get :index, :page => 5
29         assigns(:page).should eq 5
30       end
31       it '省略されると@pageに1値が入る' do
32         get :index
33         assigns(:page).should eq 1
34       end
35       it '与えられたpage_sizeがセットされている' do
36         get :index, :page_size => 15
37         assigns(:page_size).should eq 15
38       end
39       it '省略されると@page_sizeにデフォルト値が入る' do
40         get :index
41         assigns(:page_size).should eq ResourcePicture.default_page_size
42       end
43       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
44         get :index, :page_size => 1500
45         assigns(:page_size).should eq ResourcePicture.max_page_size
46       end
47       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
48         get :index, :page_size => 0
49         assigns(:page_size).should eq ResourcePicture.default_page_size
50       end
51     end
52     context 'つつがなく終わるとき' do
53       it '素材モデルに一覧を問い合わせている' do
54         ResourcePicture.should_receive(:list).exactly(1)
55         get :index
56       end
57       it '@resource_picturesにリストを取得している' do
58         get :index
59         assigns(:resource_pictures).should have_at_least(3).items
60       end
61       context 'html形式' do
62         it '@paginateにページ制御を取得している' do
63           get :index
64           assigns(:paginate).is_a?(Kaminari::PaginatableArray).should be_true
65         end
66         it 'ステータスコード200 OKを返す' do
67           get :index
68           response.should be_success 
69         end
70         it 'indexテンプレートを描画する' do
71           get :index
72           response.should render_template("index")
73         end
74       end
75       context 'json形式' do
76         it 'ステータスコード200 OKを返す' do
77           get :index, :format => :json
78           response.should be_success 
79         end
80         it 'jsonデータを返す' do
81           get :index, :format => :json
82           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
83         end
84         it '素材モデルにjson一覧出力オプションを問い合わせている' do
85           ResourcePicture.should_receive(:list_json_opt).exactly(1)
86           get :index, :format => :json
87         end
88         it 'データがリスト構造になっている' do
89           get :index, :format => :json
90           json = JSON.parse response.body
91           json.should have_at_least(3).items
92         end
93         it 'リストの先頭くらいは素材っぽいものであって欲しい' do
94           get :index, :format => :json
95           json = JSON.parse response.body
96           json.first.has_key?("ext").should be_true
97           json.first.has_key?("md5").should be_true
98           json.first.has_key?("picture_id").should be_true
99         end
100       end
101     end
102     context 'ユーザ権限がないとき' do
103       before do
104         sign_out @user
105       end
106       context 'html形式' do
107         it 'ステータスコード302 Foundを返す' do
108           get :index
109           response.status.should eq 302
110         end
111         it 'サインインページへ遷移する' do
112           get :index
113           response.should redirect_to '/users/sign_in'
114         end
115       end
116       context 'json形式' do
117         it 'ステータスコード401 Unauthorizedを返す' do
118           get :index, :format => :json
119           response.status.should eq 401
120         end
121         it '応答メッセージにUnauthorizedを返す' do
122           get :index, :format => :json
123           response.message.should match(/Unauthorized/)
124         end
125       end
126     end
127     context 'ユーザ権限はないが管理者権限があるとき' do
128       before do
129         sign_out @user
130         sign_in @admin
131       end
132       it 'ステータスコード200 OKを返す' do
133         get :index
134         response.should be_success 
135       end
136     end
137     context 'ユーザ権限はないが借手権限があるとき' do
138       before do
139         sign_out @user
140         sign_in @demand_user
141       end
142       it 'ステータスコード200 OKを返す' do
143         get :index
144         response.should be_success 
145       end
146     end
147     context 'ユーザだが作家登録していないとき' do
148       before do
149         @author.destroy
150       end
151       it 'ステータスコード200 OKを返す' do
152         get :index
153         response.should be_success 
154       end
155     end
156   end
157   
158   describe '単体表示に於いて' do
159     before do
160       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
161       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
162       sign_in @user
163       ResourcePicture.stub(:show).with(@rp.id.to_s, [@user, nil, nil]).and_return(@rp)
164       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, @admin, nil]).and_return(@rp)
165       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, nil, @demand_user]).and_return(@rp)
166     end
167     context 'つつがなく終わるとき' do
168       it '素材モデルに単体取得を問い合わせている' do
169         ResourcePicture.should_receive(:show).exactly(1)
170         get :show, :id => @rp.id
171       end
172       it '@resource_pictureにアレを取得している' do
173         get :show, :id => @rp.id
174         assigns(:resource_picture).id.should eq(@rp.id)
175       end
176       context 'html形式' do
177         it 'ステータスコード200 OKを返す' do
178           get :show, :id => @rp.id
179           response.should be_success
180         end
181         it 'showテンプレートを描画する' do
182           get :show, :id => @rp.id
183           response.should render_template("show")
184         end
185       end
186       context 'json形式' do
187         it 'ステータスコード200 OKを返す' do
188           get :show, :id => @rp.id, :format => :json
189           response.should be_success
190         end
191         it 'jsonデータを返す' do
192           get :show, :id => @rp.id, :format => :json
193           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
194         end
195         it '素材モデルにjson単体出力オプションを問い合わせている' do
196           ResourcePicture.should_receive(:show_json_opt).exactly(1)
197           get :show, :id => @rp.id, :format => :json
198         end
199         it 'データがアレになっている' do
200           get :show, :id => @rp.id, :format => :json
201           json = JSON.parse response.body
202           json["ext"].should match(/png/)
203           json["md5"].should_not be_nil
204           json["picture_id"].should_not be_nil
205         end
206       end
207       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
208       #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
209       #スタブをおいてsend_dataがデータを返す体裁でテストする。
210       context 'png形式' do
211         before do
212           ResourcePicture.any_instance.stub(:mime_type).and_return('image/png')
213           ResourcePicture.any_instance.stub(:restore).and_return('aaa')
214         end
215         it '画像モデルに画像データを問い合わせる' do
216           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
217           get :show, :id => @rp.id, :format => :png
218         end
219         it '画像モデルにMimeTypeを問い合わせる' do
220           ResourcePicture.any_instance.should_receive(:mime_type).exactly(1)
221           get :show, :id => @rp.id, :format => :png
222         end
223         it '画像を送信する' do
224           get :show, :id => @rp.id, :format => :png
225           response.body.should eq 'aaa'
226         end
227       end
228       context 'gif形式' do
229         before do
230           ResourcePicture.any_instance.stub(:mime_type).and_return('image/gif')
231           ResourcePicture.any_instance.stub(:restore).and_return('bbb')
232         end
233         it '画像モデルに画像データを問い合わせる' do
234           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
235           get :show, :id => @rp.id, :format => :gif
236         end
237         it '画像モデルにMimeTypeを問い合わせる' do
238           ResourcePicture.any_instance.should_receive(:mime_type).exactly(1)
239           get :show, :id => @rp.id, :format => :png
240         end
241         it '画像を送信する' do
242           get :show, :id => @rp.id, :format => :gif
243           response.body.should eq 'bbb'
244         end
245       end
246       context 'jpeg形式' do
247         before do
248           ResourcePicture.any_instance.stub(:mime_type).and_return('image/jpeg')
249           ResourcePicture.any_instance.stub(:restore).and_return('ccc')
250         end
251         it '画像モデルに画像データを問い合わせる' do
252           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
253           get :show, :id => @rp.id, :format => :jpeg
254         end
255         it '画像モデルにMimeTypeを問い合わせる' do
256           ResourcePicture.any_instance.should_receive(:mime_type).exactly(1)
257           get :show, :id => @rp.id, :format => :png
258         end
259         it '画像を送信する' do
260           get :show, :id => @rp.id, :format => :jpeg
261           response.body.should eq 'ccc'
262         end
263       end
264     end
265     context 'ユーザ権限がないとき' do
266       before do
267         sign_out @user
268       end
269       context 'html形式' do
270         it 'ステータスコード302 Foundを返す' do
271           get :show, :id => @rp.id
272           response.status.should eq 302
273         end
274         it 'サインインページへ遷移する' do
275           get :show, :id => @rp.id
276           response.body.should redirect_to '/users/sign_in'
277         end
278       end
279       context 'json形式' do
280         it 'ステータスコード401 Unauthorizedを返す' do
281           get :show, :id => @rp.id, :format => :json
282           response.status.should eq 401
283         end
284         it '応答メッセージにUnauthorizedを返す' do
285           get :show, :id => @rp.id, :format => :json
286           response.message.should match(/Unauthorized/)
287         end
288       end
289     end
290     context 'ユーザ権限はないが管理者権限があるとき' do
291       before do
292         sign_out @user
293         sign_in @admin
294       end
295       it 'ステータスコード200 OKを返す' do
296         get :show, :id => @rp.id
297         response.should be_success 
298       end
299     end
300     context 'ユーザ権限はないが借手権限があるとき' do
301       before do
302         sign_out @user
303         sign_in @demand_user
304       end
305       it 'ステータスコード200 OKを返す' do
306         get :show, :id => @rp.id
307         response.should be_success 
308       end
309     end
310     context 'ユーザだが作家登録していないとき' do
311       before do
312         @author.destroy
313       end
314       it 'ステータスコード200 OKを返す' do
315         get :show, :id => @rp.id
316         response.should be_success 
317       end
318     end
319 =begin
320     context '対象素材がないとき' do
321       before do
322         ResourcePicture.unstub(:show)
323       end
324       context 'html形式' do
325         it '例外404 not_foundを返す' do
326           lambda{
327             get :show, :id => 0
328           }.should raise_error(ActiveRecord::RecordNotFound)
329         end
330       end
331       context 'json形式' do
332         it '例外404 not_foundを返す' do
333           lambda{ 
334             get :show, :id => 0, :format => :json
335           }.should raise_error(ActiveRecord::RecordNotFound)
336         end
337       end
338     end
339     context '他人の素材を見ようとしたとき' do
340       before do
341         ResourcePicture.stub(:show).and_return(@rp)
342         ResourcePicture.any_instance.stub(:own?).with(any_args()).and_return(false)
343       end
344       context 'html形式' do
345         it '例外403 forbiddenを返す' do
346           lambda{
347             get :show, :id => @rp.id
348           }.should raise_error(ActiveRecord::Forbidden)
349         end
350       end
351       context 'json形式' do
352         it '例外403 forbiddenを返す' do
353           lambda{
354             get :show, :id => @rp.id, :format => :json
355           }.should raise_error(ActiveRecord::Forbidden)
356         end
357       end
358     end
359 =end
360   end
361
362   describe '素材数取得に於いて' do
363     before do
364       ResourcePicture.should_receive(:visible_count).and_return(3)
365 #      sign_in @user
366     end
367     context 'つつがなく終わるとき' do
368       it 'ステータスコード200 OKを返す' do
369         get :count, :format => :json
370         response.should be_success 
371       end
372       context 'json形式' do
373         it 'jsonデータを返す' do
374           get :count, :format => :json
375           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
376         end
377         it 'データがHash構造になっていてコマ絵数が3である' do
378           get :count, :format => :json
379           json = JSON.parse response.body
380           json["count"].should == 3
381         end
382       end
383     end
384   end
385
386   describe 'クレジット表示に於いて' do
387     before do
388       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
389       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
390       sign_in @user
391       ResourcePicture.stub(:show).with(@rp.id.to_s, [@user, nil, nil]).and_return(@rp)
392       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, @admin, nil]).and_return(@rp)
393       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, nil, @demand_user]).and_return(@rp)
394     end
395     context 'つつがなく終わるとき' do
396       it '素材モデルに単体取得を問い合わせている' do
397         ResourcePicture.should_receive(:show).exactly(1)
398         get :credit, :id => @rp.id
399       end
400       it '@resource_pictureにアレを取得している' do
401         get :credit, :id => @rp.id
402         assigns(:resource_picture).id.should eq(@rp.id)
403       end
404       context 'html形式' do
405         it 'ステータスコード200 OKを返す' do
406           get :credit, :id => @rp.id
407           response.should be_success
408         end
409         it 'creditテンプレートを描画する' do
410           get :credit, :id => @rp.id
411           response.should render_template("credit")
412         end
413       end
414       context 'json形式' do
415         it 'ステータスコード200 OKを返す' do
416           get :credit, :id => @rp.id, :format => :json
417           response.should be_success
418         end
419         it 'jsonデータを返す' do
420           get :credit, :id => @rp.id, :format => :json
421           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
422         end
423         it '素材モデルにjson単体出力オプションを問い合わせている' do
424           ResourcePicture.should_receive(:show_json_opt).exactly(1)
425           get :credit, :id => @rp.id, :format => :json
426         end
427         it 'データがアレになっている' do
428           get :credit, :id => @rp.id, :format => :json
429           json = JSON.parse response.body
430           json["ext"].should match(/png/)
431           json["md5"].should_not be_nil
432           json["picture_id"].should_not be_nil
433         end
434       end
435     end
436     context 'ユーザ権限がないとき' do
437       before do
438         sign_out @user
439       end
440       context 'html形式' do
441         it 'ステータスコード302 Foundを返す' do
442           get :credit, :id => @rp.id
443           response.status.should eq 302
444         end
445         it 'サインインページへ遷移する' do
446           get :credit, :id => @rp.id
447           response.body.should redirect_to '/users/sign_in'
448         end
449       end
450       context 'json形式' do
451         it 'ステータスコード401 Unauthorizedを返す' do
452           get :credit, :id => @rp.id, :format => :json
453           response.status.should eq 401
454         end
455         it '応答メッセージにUnauthorizedを返す' do
456           get :credit, :id => @rp.id, :format => :json
457           response.message.should match(/Unauthorized/)
458         end
459       end
460     end
461     context 'ユーザ権限はないが管理者権限があるとき' do
462       before do
463         sign_out @user
464         sign_in @admin
465       end
466       it 'ステータスコード200 OKを返す' do
467         get :credit, :id => @rp.id
468         response.should be_success 
469       end
470     end
471     context 'ユーザ権限はないが借手権限があるとき' do
472       before do
473         sign_out @user
474         sign_in @demand_user
475       end
476       it 'ステータスコード200 OKを返す' do
477         get :credit, :id => @rp.id
478         response.should be_success 
479       end
480     end
481     context 'ユーザだが作家登録していないとき' do
482       before do
483         @author.destroy
484       end
485       it 'ステータスコード200 OKを返す' do
486         get :credit, :id => @rp.id
487         response.should be_success 
488       end
489     end
490 =begin
491     context '対象素材がないとき' do
492       before do
493         ResourcePicture.unstub(:show)
494       end
495       context 'html形式' do
496         it '例外404 not_foundを返す' do
497           lambda{
498             get :show, :id => 0
499           }.should raise_error(ActiveRecord::RecordNotFound)
500         end
501       end
502       context 'json形式' do
503         it '例外404 not_foundを返す' do
504           lambda{ 
505             get :show, :id => 0, :format => :json
506           }.should raise_error(ActiveRecord::RecordNotFound)
507         end
508       end
509     end
510 =end
511   end
512   
513   #原画にライセンスを与えるため際の確認フォーム
514   describe '新規作成フォーム表示に於いて' do
515     before do
516       sign_in @user
517       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
518       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
519       @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}, 
520         :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
521       @newop = FactoryGirl.create :original_picture, :artist_id => @artist.id
522       @newattr = {:original_picture_id => @newop.id, :original_picture_license_group => {:original_picture_id => @newop.id, :license_group_id => @lg.id}, 
523         :resource_picture => {:original_picture_id => @newop.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
524       @imager = ImagerTest.load("abc\ndef\nghi")
525     end
526     context '事前チェックしておく' do
527       before do
528         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
529         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
530         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
531         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
532         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
533         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
534         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
535       end
536       it '原画モデルに編集取得を依頼してしている' do
537         OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
538         get :new, @attr
539       end
540       it '原画モデルに画像データを問い合わせている' do
541         OriginalPicture.any_instance.should_receive(:restore).with(any_args()).exactly(1)
542         get :new, @attr
543       end
544       it '画像ライブラリをロードしている' do
545         PettanImager.should_receive(:load).with(any_args()).exactly(1)
546         get :new, @attr
547       end
548       it 'ライセンスグループモデルに単体取得を依頼している' do
549         LicenseGroup.should_receive(:show).with(any_args()).exactly(1)
550         get :new, @attr
551       end
552       context 'ライセンスを与えようとしている原画が素材を作成してないとき' do
553         it '素材モデルにデフォルト値補充を依頼している' do
554           ResourcePicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
555           get :new, @newattr
556         end
557       end
558       context 'ライセンスを与えようとしている原画が既に素材を作成しているとき' do
559         it '素材モデルにデフォルト値補充を依頼してない' do
560           ResourcePicture.any_instance.should_not_receive(:supply_default).with(any_args())
561           get :new, @attr
562         end
563       end
564       it '素材モデルに上書き補充を依頼している' do
565         ResourcePicture.any_instance.should_receive(:overwrite).with(@op).exactly(1)
566         get :new, @attr
567       end
568     end
569     context 'つつがなく終わるとき' do
570       before do
571         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
572         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
573         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
574         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
575         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
576         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
577         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
578       end
579       it '@original_pictureに原画を取得している' do
580         get :new, @attr
581         assigns(:original_picture).should eq @op
582       end
583       it '@imagerに画像ライブラリをロードしている' do
584         get :new, @attr
585         assigns(:imager).should eq @imager
586       end
587       it '@original_picture_license_groupに新規原画ライセンスグループデータを用意している' do
588         get :new, @attr
589         assigns(:original_picture_license_group).should be_a_new(OriginalPictureLicenseGroup)
590       end
591       it '@license_groupにライセンスグループを取得している' do
592         get :new, @attr
593         assigns(:license_group).should eq @lg
594       end
595       context 'ライセンスを与えようとしている原画が素材を作成してないとき' do
596         it '@resource_pictureに新規素材データを用意している' do
597           get :new, @newattr
598           assigns(:resource_picture).should be_a_new(ResourcePicture)
599         end
600       end
601       context 'ライセンスを与えようとしている原画が既に素材を作成しているとき' do
602         it '@resource_pictureに素材データを用意している' do
603           get :new, @attr
604           assigns(:resource_picture).is_a?(ResourcePicture).should be_true
605           assigns(:resource_picture).should_not be_a_new(ResourcePicture)
606         end
607       end
608       context 'html形式' do
609         it 'ステータスコード200 OKを返す' do
610           get :new, @attr
611           response.should be_success 
612         end
613         it 'ページテンプレートnewを描画する' do
614           get :new, @attr
615           response.should render_template("new")
616         end
617       end
618       context 'js形式' do
619         before do
620           @attr.merge!({:format => :js})
621         end
622         it 'ステータスコード200 OKを返す' do
623           get :new, @attr
624           response.should be_success 
625         end
626         it '部分テンプレートnew.jsを描画する' do
627           get :new, @attr
628           response.should render_template("new")
629         end
630       end
631     end
632     context 'ユーザ権限がないとき' do
633       before do
634         sign_out @user
635       end
636       context 'html形式' do
637         it 'ステータスコード302 Foundを返す' do
638           get :new, @attr
639           response.status.should eq 302
640         end
641         it 'サインインページへ遷移する' do
642           get :new, @attr
643           response.body.should redirect_to '/users/sign_in'
644         end
645       end
646       context 'js形式' do
647         before do
648           @attr.merge!({:format => :js})
649         end
650         it 'ステータスコード401 Unauthorizedを返す' do
651           get :new, @attr
652           response.status.should eq 401
653         end
654         it '応答メッセージにUnauthorizedを返す' do
655           get :new, @attr
656           response.message.should match(/Unauthorized/)
657         end
658       end
659     end
660     context 'ユーザ権限はないが管理者権限があるとき' do
661       before do
662         sign_out @user
663         sign_in @admin
664       end
665       context 'html形式' do
666         it 'ステータスコード302 Foundを返す' do
667           get :new, @attr
668           response.status.should eq 302
669         end
670         it 'サインインページへ遷移する' do
671           get :new, @attr
672           response.body.should redirect_to '/users/sign_in'
673         end
674       end
675     end
676     context 'ユーザ権限はないが借手権限があるとき' do
677       before do
678         sign_out @user
679         sign_in @demand_user
680       end
681       context 'html形式' do
682         it 'ステータスコード302 Foundを返す' do
683           get :new, @attr
684           response.status.should eq 302
685         end
686         it 'サインインページへ遷移する' do
687           get :new, @attr
688           response.body.should redirect_to '/users/sign_in'
689         end
690       end
691     end
692     context 'ユーザが絵師でないとき' do
693       before do
694         @artist.destroy
695       end
696       context 'html形式' do
697         it 'ステータスコード302 Foundを返す' do
698           get :new, @attr
699           response.status.should eq 302
700         end
701         it '絵師登録ページへ遷移する' do
702           get :new, @attr
703           response.should redirect_to new_artist_path
704         end
705       end
706       context 'js形式' do
707         before do
708           @attr.merge!({:format => :js})
709         end
710         it 'ステータスコード200 Okを返す' do
711           get :new, @attr
712           response.status.should eq 200
713         end
714         it '絵師登録部分テンプレートartists/new.jsを描画する' do
715           get :new, @attr
716           response.should render_template("artists/new")
717         end
718       end
719     end
720     context '対象ライセンスグループがないとき' do
721       before do
722         @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => 0}, 
723           :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
724         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
725         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
726         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
727       end
728       context 'html形式' do
729         it '例外404 not_foundを返す' do
730           lambda{
731             get :new, @attr
732           }.should raise_error(ActiveRecord::RecordNotFound)
733         end
734       end
735       context 'json形式' do
736         before do
737           @attr.merge!({:format => :js})
738         end
739         it '例外404 not_foundを返す' do
740           lambda{ 
741             get :new, @attr
742           }.should raise_error(ActiveRecord::RecordNotFound)
743         end
744       end
745     end
746   end
747   
748   describe '新規作成に於いて' do
749     before do
750       sign_in @user
751       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
752       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
753       @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}, 
754         :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{"new":0}' }}
755       @newop = FactoryGirl.create :original_picture, :artist_id => @artist.id
756       @newattr = {:original_picture_id => @newop.id, :original_picture_license_group => {:original_picture_id => @newop.id, :license_group_id => @lg.id}, 
757         :resource_picture => {:original_picture_id => @newop.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{"new":1}' }}
758       @imager = ImagerTest.load("abc\ndef\nghi")
759     end
760     context '事前チェックしておく' do
761       before do
762         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
763         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
764         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
765         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
766         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
767         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
768         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
769         ResourcePicture.any_instance.stub(:store).with(@imager).and_return(true)
770       end
771       it '原画モデルに編集取得を依頼してしている' do
772         OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
773         post :create, @attr
774       end
775       it '原画モデルに画像データを問い合わせている' do
776         OriginalPicture.any_instance.should_receive(:restore).with(any_args()).exactly(1)
777         post :create, @attr
778       end
779       it '画像ライブラリをロードしている' do
780         PettanImager.should_receive(:load).with(any_args()).exactly(1)
781         post :create, @attr
782       end
783       it 'ライセンスグループモデルに単体取得を依頼している' do
784         LicenseGroup.should_receive(:show).with(any_args()).exactly(1)
785         post :create, @attr
786       end
787       context 'ライセンスを与えようとしている原画が素材を作成してないとき' do
788         it '素材モデルにデフォルト値補充を依頼している' do
789           ResourcePicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
790           post :create, @newattr
791         end
792       end
793       context 'ライセンスを与えようとしている原画が既に素材を作成しているとき' do
794         it '素材モデルにデフォルト値補充を依頼してない' do
795           ResourcePicture.any_instance.should_not_receive(:supply_default).with(any_args())
796           post :create, @attr
797         end
798       end
799       it '素材モデルに上書き補充を依頼している' do
800         ResourcePicture.any_instance.should_receive(:overwrite).with(@op).exactly(1)
801         post :create, @attr
802       end
803       it '素材モデルに保存を依頼している' do
804         ResourcePicture.any_instance.should_receive(:store).with(@imager).exactly(1)
805         post :create, @attr
806       end
807     end
808     context 'つつがなく終わるとき[ライセンスを与えようとしている原画が素材を作成してない]' do
809       before do
810         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
811         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
812         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
813         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
814         ResourcePicture.any_instance.stub(:store).with(@imager).and_return {
815           assigns(:resource_picture).attributes = @newattr[:resource_picture]
816           assigns(:resource_picture).overwrite @newop
817           assigns(:resource_picture).picture_id = @p.id
818           assigns(:resource_picture).save!
819           true
820         }
821       end
822       it '@original_pictureに原画を取得している' do
823         post :create, @newattr
824         assigns(:original_picture).should eq @newop
825       end
826       it '@imagerに画像ライブラリをロードしている' do
827         post :create, @newattr
828         assigns(:imager).should eq @imager
829       end
830       it '@original_picture_license_groupに新規原画ライセンスグループデータを用意している' do
831         post :create, @newattr
832         assigns(:original_picture_license_group).should be_a_new(OriginalPictureLicenseGroup)
833       end
834       it '@license_groupにライセンスグループを取得している' do
835         post :create, @newattr
836         assigns(:license_group).should eq @lg
837       end
838       it '素材データにPOSTデータの素材情報を適用している' do
839         post :create, @newattr
840         assigns(:resource_picture).license_id.should eq @license.id
841         assigns(:resource_picture).artist_name.should eq @artist.name
842         assigns(:resource_picture).credit.should eq '{}'
843         assigns(:resource_picture).settings.should eq '{"new":1}'
844       end
845       it "作成された素材がDBにある" do
846         lambda{
847           post :create, @newattr
848         }.should change(ResourcePicture, :count)
849       end
850       context 'html形式' do
851         it 'ステータスコード302 Foundを返す' do
852           post :create, @newattr
853           response.status.should eq 302
854         end
855         it '作成された素材の表示ページへ遷移する' do
856           post :create, @newattr
857           response.should redirect_to(ResourcePicture.last)
858         end
859       end
860       context 'json形式' do
861         before do
862           @newattr.merge!({:format => :json})
863         end
864         it 'ステータスコード200 OKを返す' do
865           post :create, @newattr
866           response.should be_success 
867         end
868         it '作成された素材をjsonデータで返す' do
869           post :create, @newattr
870           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
871         end
872         it '素材モデルにjson単体出力オプションを問い合わせている' do
873           ResourcePicture.should_receive(:show_json_opt).exactly(1)
874           post :create, @newattr
875         end
876         it 'データがアレになっている' do
877           post :create, @newattr
878           json = JSON.parse response.body
879           json["ext"].should match(/png/)
880           json["md5"].should_not be_nil
881           json["picture_id"].should_not be_nil
882         end
883       end
884     end
885     context 'つつがなく終わるとき[ライセンスを与えようとしている原画が既に素材を作成している]' do
886       before do
887         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
888         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
889         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
890         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
891         ResourcePicture.any_instance.stub(:store).with(@imager).and_return {
892           assigns(:resource_picture).attributes = @attr[:resource_picture]
893           assigns(:resource_picture).overwrite @op
894           assigns(:resource_picture).save!
895           true
896         }
897       end
898       it '@original_pictureに原画を取得している' do
899         post :create, @attr
900         assigns(:original_picture).should eq @op
901       end
902       it '素材データにPOSTデータの素材情報を適用している' do
903         post :create, @attr
904         assigns(:resource_picture).license_id.should eq @license.id
905         assigns(:resource_picture).artist_name.should eq @artist.name
906         assigns(:resource_picture).credit.should eq '{}'
907         assigns(:resource_picture).settings.should eq '{"new":0}'
908       end
909       it "素材が更新される" do
910         post :create, @attr
911         r = ResourcePicture.find(@rp.id)
912         r.settings.should eq '{"new":0}'
913       end
914       it "上書きなので件数は変化しない" do
915         lambda{
916           post :create, @attr
917         }.should_not change(ResourcePicture, :count)
918       end
919     end
920     context 'ユーザ権限がないとき' do
921       before do
922         sign_out @user
923       end
924       context 'html形式' do
925         it 'ステータスコード302 Foundを返す' do
926           post :create, @attr
927           response.status.should eq 302
928         end
929         it 'サインインページへ遷移する' do
930           post :create, @attr
931           response.body.should redirect_to '/users/sign_in'
932         end
933       end
934       context 'json形式' do
935         before do
936           @attr.merge!({:format => :js})
937         end
938         it 'ステータスコード401 Unauthorizedを返す' do
939           post :create, @attr
940           response.status.should eq 401
941         end
942         it '応答メッセージにUnauthorizedを返す' do
943           post :create, @attr
944           response.message.should match(/Unauthorized/)
945         end
946       end
947     end
948     context 'ユーザ権限はないが管理者権限があるとき' do
949       before do
950         sign_out @user
951         sign_in @admin
952       end
953       context 'html形式' do
954         it 'ステータスコード302 Foundを返す' do
955           post :create, @attr
956           response.status.should eq 302
957         end
958         it 'サインインページへ遷移する' do
959           post :create, @attr
960           response.body.should redirect_to '/users/sign_in'
961         end
962       end
963     end
964     context 'ユーザ権限はないが借手権限があるとき' do
965       before do
966         sign_out @user
967         sign_in @demand_user
968       end
969       context 'html形式' do
970         it 'ステータスコード302 Foundを返す' do
971           post :create, @attr
972           response.status.should eq 302
973         end
974         it 'サインインページへ遷移する' do
975           post :create, @attr
976           response.body.should redirect_to '/users/sign_in'
977         end
978       end
979     end
980     context 'ユーザが絵師でないとき' do
981       before do
982         @artist.destroy
983       end
984       context 'html形式' do
985         it 'ステータスコード302 Foundを返す' do
986           post :create, @attr
987           response.status.should eq 302
988         end
989         it '絵師登録ページへ遷移する' do
990           post :create, @attr
991           response.should redirect_to new_artist_path
992         end
993       end
994       context 'json形式' do
995         it '例外403 forbiddenを返す' do
996           @attr.merge!({:format => :json})
997           lambda{
998             post :create, @attr
999           }.should raise_error(ActiveRecord::Forbidden)
1000         end
1001       end
1002     end
1003     context '検証、保存に失敗した' do
1004       before do
1005         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
1006         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
1007         ResourcePicture.any_instance.stub(:store).and_return(false)
1008       end
1009       it "未保存の素材を保持している" do
1010         post :create, @newattr
1011         assigns(:resource_picture).should be_a_new(ResourcePicture)
1012       end
1013       context 'html形式' do
1014         it 'ステータスコード200 OKを返す' do
1015           post :create, @newattr
1016           response.status.should eq 200
1017         end
1018         it '新規ページを描画する' do
1019           post :create, @newattr
1020           response.should render_template("new")
1021         end
1022       end
1023       context 'json形式' do
1024         before do
1025           @newattr.merge!({:format => :json})
1026         end
1027         it 'ステータスコード422 unprocessable_entity を返す' do
1028           post :create, @newattr
1029           response.status.should eq 422
1030         end
1031         it '応答メッセージUnprocessable Entityを返す' do
1032           post :create, @newattr
1033           response.message.should match(/Unprocessable/)
1034         end
1035       end
1036     end
1037   end
1038   
1039   describe '削除に於いて' do
1040     before do
1041       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1042       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1043       sign_in @user
1044     end
1045     context '事前チェックしておく' do
1046       before do
1047         ResourcePicture.stub(:edit).with(any_args()).and_return @rp
1048         ResourcePicture.any_instance.stub(:unpublish).with(any_args()).and_return(true)
1049       end
1050       it '素材モデルに編集取得を問い合わせている' do
1051         ResourcePicture.should_receive(:edit).exactly(1)
1052         delete :destroy, :id => @rp.id
1053       end
1054       it 'モデルに削除を依頼する' do
1055         ResourcePicture.any_instance.should_receive(:unpublish).exactly(1)
1056         delete :destroy, :id => @rp.id
1057       end
1058       it '@resource_pictureにアレを取得している' do
1059         delete :destroy, :id => @rp.id
1060         assigns(:resource_picture).id.should eq(@rp.id)
1061       end
1062     end
1063     context 'つつがなく終わるとき' do
1064       before do
1065         ResourcePicture.any_instance.stub(:unpublish).with(any_args()).and_return(true)
1066       end
1067       context 'html形式' do
1068         it 'ステータスコード302 Foundを返す' do
1069           delete :destroy, :id => @rp.id
1070           response.status.should eq 302
1071         end
1072         it 'マイ素材の一覧ページへ遷移する' do
1073           delete :destroy, :id => @rp.id
1074           response.should redirect_to('/home/resource_picture')
1075         end
1076       end
1077       context 'json形式' do
1078         it 'ステータスコード200 OKを返す' do
1079           delete :destroy, :id => @rp.id, :format => :json
1080           response.should be_success 
1081         end
1082         it 'ページ本体は特に返さない' do
1083           delete :destroy, :id => @rp.id, :format => :json
1084           response.body.should match /./
1085         end
1086       end
1087     end
1088     context 'ユーザ権限がないとき' do
1089       before do
1090         sign_out @user
1091       end
1092       context 'html形式' do
1093         it 'ステータスコード302 Foundを返す' do
1094           delete :destroy, :id => @rp.id
1095           response.status.should eq 302
1096         end
1097         it 'サインインページへ遷移する' do
1098           delete :destroy, :id => @rp.id
1099           response.body.should redirect_to '/users/sign_in'
1100         end
1101       end
1102       context 'json形式' do
1103         it '応答メッセージにUnauthorizedを返す' do
1104           delete :destroy, :id => @rp.id, :format => :json
1105           response.message.should match(/Unauthorized/)
1106         end
1107       end
1108     end
1109     context 'ユーザ権限はないが管理者権限があるとき' do
1110       before do
1111         sign_out @user
1112         sign_in @admin
1113       end
1114       context 'html形式' do
1115         it 'ステータスコード302 Foundを返す' do
1116           delete :destroy, :id => @rp.id
1117           response.status.should eq 302
1118         end
1119         it 'サインインページへ遷移する' do
1120           delete :destroy, :id => @rp.id
1121           response.body.should redirect_to '/users/sign_in'
1122         end
1123       end
1124     end
1125     context 'ユーザ権限はないが借手権限があるとき' do
1126       before do
1127         sign_out @user
1128         sign_in @demand_user
1129       end
1130       context 'html形式' do
1131         it 'ステータスコード302 Foundを返す' do
1132           delete :destroy, :id => @rp.id
1133           response.status.should eq 302
1134         end
1135         it 'サインインページへ遷移する' do
1136           delete :destroy, :id => @rp.id
1137           response.body.should redirect_to '/users/sign_in'
1138         end
1139       end
1140     end
1141     context 'ユーザが絵師でないとき' do
1142       before do
1143         @artist.destroy
1144       end
1145       context 'html形式' do
1146         it 'ステータスコード302 Foundを返す' do
1147           delete :destroy, :id => @rp.id
1148           response.status.should eq 302
1149         end
1150         it '絵師登録ページへ遷移する' do
1151           delete :destroy, :id => @rp.id
1152           response.should redirect_to new_artist_path
1153         end
1154       end
1155       context 'json形式' do
1156         it '例外403 forbiddenを返す' do
1157           lambda{
1158             delete :destroy, :id => @rp.id, :format => :json
1159           }.should raise_error(ActiveRecord::Forbidden)
1160         end
1161       end
1162     end
1163     context '削除に失敗したとき' do
1164       before do
1165         ResourcePicture.any_instance.stub(:unpublish).and_return(false)
1166       end
1167       context 'html形式' do
1168         it 'ステータスコード302 Foundを返す' do
1169           delete :destroy, :id => @rp.id
1170           response.status.should eq 302
1171         end
1172         it 'その素材の詳細ページへ遷移する' do
1173           delete :destroy, :id => @rp.id
1174           response.should redirect_to(resource_picture_path(@rp))
1175         end
1176       end
1177       context 'json形式' do
1178         it 'ステータスコード422 unprocessable_entity を返す' do
1179           delete :destroy, :id => @rp.id, :format => :json
1180           response.status.should eq 422
1181         end
1182         it '応答メッセージUnprocessable Entityを返す' do
1183           delete :destroy, :id => @rp.id, :format => :json
1184           response.message.should match(/Unprocessable/)
1185         end
1186       end
1187     end
1188   end
1189
1190 else
1191   describe '一覧表示に於いて' do
1192     before do
1193       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1194       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1195       sign_in @user
1196       ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
1197     end
1198     context 'つつがなく終わるとき' do
1199       context 'html形式' do
1200         it 'ステータスコード200 OKを返す' do
1201           get :index
1202           response.should be_success 
1203         end
1204         it 'indexテンプレートを描画する' do
1205           get :index
1206           response.should render_template("index")
1207         end
1208       end
1209       context 'json形式' do
1210         it 'ステータスコード200 OKを返す' do
1211           get :index, :format => :json
1212           response.should be_success 
1213         end
1214         it 'jsonデータを返す' do
1215           get :index, :format => :json
1216           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1217         end
1218       end
1219     end
1220     context 'ユーザ権限がないとき' do
1221       before do
1222         sign_out @user
1223       end
1224       context 'html形式' do
1225         it 'ステータスコード200 OKを返す' do
1226           get :index
1227           response.should be_success 
1228         end
1229         it 'indexテンプレートを描画する' do
1230           get :index
1231           response.should render_template("index")
1232         end
1233       end
1234       context 'json形式' do
1235         it 'ステータスコード200 OKを返す' do
1236           get :index, :format => :json
1237           response.should be_success 
1238         end
1239         it 'jsonデータを返す' do
1240           get :index, :format => :json
1241           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1242         end
1243       end
1244     end
1245   end
1246   
1247   describe '単体表示に於いて' do
1248     before do
1249       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1250       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1251       sign_in @user
1252       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, nil, nil]).and_return(@rp)
1253       ResourcePicture.stub(:show).with(@rp.id.to_s, [@user, nil, nil]).and_return(@rp)
1254       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, @admin, nil]).and_return(@rp)
1255       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, nil, @demand_user]).and_return(@rp)
1256     end
1257     context 'つつがなく終わるとき' do
1258       context 'html形式' do
1259         it 'ステータスコード200 OKを返す' do
1260           get :show, :id => @rp.id
1261           response.should be_success
1262         end
1263         it 'showテンプレートを描画する' do
1264           get :show, :id => @rp.id
1265           response.should render_template("show")
1266         end
1267       end
1268       context 'json形式' do
1269         it 'ステータスコード200 OKを返す' do
1270           get :show, :id => @rp.id, :format => :json
1271           response.should be_success
1272         end
1273         it 'jsonデータを返す' do
1274           get :show, :id => @rp.id, :format => :json
1275           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1276         end
1277       end
1278     end
1279     context 'ユーザ権限がないとき' do
1280       before do
1281         sign_out @user
1282       end
1283       context 'html形式' do
1284         it 'ステータスコード200 OKを返す' do
1285           get :show, :id => @rp.id
1286           response.should be_success
1287         end
1288         it 'showテンプレートを描画する' do
1289           get :show, :id => @rp.id
1290           response.should render_template("show")
1291         end
1292       end
1293       context 'json形式' do
1294         it 'ステータスコード200 OKを返す' do
1295           get :show, :id => @rp.id, :format => :json
1296           response.should be_success
1297         end
1298         it 'jsonデータを返す' do
1299           get :show, :id => @rp.id, :format => :json
1300           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1301         end
1302       end
1303     end
1304   end
1305
1306   describe '素材数取得に於いて' do
1307     before do
1308       ResourcePicture.should_receive(:visible_count).and_return(3)
1309 #      sign_in @user
1310     end
1311     context 'つつがなく終わるとき' do
1312       it 'ステータスコード200 OKを返す' do
1313         get :count, :format => :json
1314         response.should be_success 
1315       end
1316       context 'json形式' do
1317         it 'jsonデータを返す' do
1318           get :count, :format => :json
1319           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1320         end
1321       end
1322     end
1323   end
1324
1325   describe 'クレジット表示に於いて' do
1326     before do
1327       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1328       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1329       sign_in @user
1330       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, nil, nil]).and_return(@rp)
1331       ResourcePicture.stub(:show).with(@rp.id.to_s, [@user, nil, nil]).and_return(@rp)
1332       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, @admin, nil]).and_return(@rp)
1333       ResourcePicture.stub(:show).with(@rp.id.to_s, [nil, nil, @demand_user]).and_return(@rp)
1334     end
1335     context 'つつがなく終わるとき' do
1336       context 'html形式' do
1337         it 'ステータスコード200 OKを返す' do
1338           get :credit, :id => @rp.id
1339           response.should be_success
1340         end
1341         it 'creditテンプレートを描画する' do
1342           get :credit, :id => @rp.id
1343           response.should render_template("credit")
1344         end
1345       end
1346       context 'json形式' do
1347         it 'ステータスコード200 OKを返す' do
1348           get :credit, :id => @rp.id, :format => :json
1349           response.should be_success
1350         end
1351         it 'jsonデータを返す' do
1352           get :credit, :id => @rp.id, :format => :json
1353           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1354         end
1355       end
1356     end
1357     context 'ユーザ権限がないとき' do
1358       before do
1359         sign_out @user
1360       end
1361       context 'html形式' do
1362         it 'ステータスコード200 OKを返す' do
1363           get :credit, :id => @rp.id
1364           response.should be_success
1365         end
1366         it 'creditテンプレートを描画する' do
1367           get :credit, :id => @rp.id
1368           response.should render_template("credit")
1369         end
1370       end
1371       context 'json形式' do
1372         it 'ステータスコード200 OKを返す' do
1373           get :credit, :id => @rp.id, :format => :json
1374           response.should be_success
1375         end
1376         it 'jsonデータを返す' do
1377           get :credit, :id => @rp.id, :format => :json
1378           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1379         end
1380       end
1381     end
1382   end
1383   
1384   #原画にライセンスを与えるため際の確認フォーム
1385   describe '新規作成フォーム表示に於いて' do
1386     before do
1387       sign_in @user
1388       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1389       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1390       @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}, 
1391         :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
1392       @newop = FactoryGirl.create :original_picture, :artist_id => @artist.id
1393       @newattr = {:original_picture_id => @newop.id, :original_picture_license_group => {:original_picture_id => @newop.id, :license_group_id => @lg.id}, 
1394         :resource_picture => {:original_picture_id => @newop.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
1395       @imager = ImagerTest.load("abc\ndef\nghi")
1396     end
1397     context 'つつがなく終わるとき' do
1398       before do
1399         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
1400         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
1401         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
1402         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
1403         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
1404         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
1405         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
1406       end
1407       context 'html形式' do
1408         it 'ステータスコード200 OKを返す' do
1409           get :new, @attr
1410           response.should be_success 
1411         end
1412         it 'ページテンプレートnewを描画する' do
1413           get :new, @attr
1414           response.should render_template("new")
1415         end
1416       end
1417       context 'js形式' do
1418         before do
1419           @attr.merge!({:format => :js})
1420         end
1421         it 'ステータスコード200 OKを返す' do
1422           get :new, @attr
1423           response.should be_success 
1424         end
1425         it '部分テンプレートnew.jsを描画する' do
1426           get :new, @attr
1427           response.should render_template("new")
1428         end
1429       end
1430     end
1431     context 'ユーザ権限がないとき' do
1432       before do
1433         sign_out @user
1434       end
1435       context 'html形式' do
1436         it 'ステータスコード302 Foundを返す' do
1437           get :new, @attr
1438           response.status.should eq 302
1439         end
1440         it 'サインインページへ遷移する' do
1441           get :new, @attr
1442           response.body.should redirect_to '/users/sign_in'
1443         end
1444       end
1445       context 'js形式' do
1446         before do
1447           @attr.merge!({:format => :js})
1448         end
1449         it 'ステータスコード401 Unauthorizedを返す' do
1450           get :new, @attr
1451           response.status.should eq 401
1452         end
1453         it '応答メッセージにUnauthorizedを返す' do
1454           get :new, @attr
1455           response.message.should match(/Unauthorized/)
1456         end
1457       end
1458     end
1459   end
1460   
1461   describe '新規作成に於いて' do
1462     before do
1463       sign_in @user
1464       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1465       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1466       @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}, 
1467         :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{"new":0}' }}
1468       @newop = FactoryGirl.create :original_picture, :artist_id => @artist.id
1469       @newattr = {:original_picture_id => @newop.id, :original_picture_license_group => {:original_picture_id => @newop.id, :license_group_id => @lg.id}, 
1470         :resource_picture => {:original_picture_id => @newop.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{"new":1}' }}
1471       @imager = ImagerTest.load("abc\ndef\nghi")
1472     end
1473     context 'つつがなく終わるとき[ライセンスを与えようとしている原画が素材を作成してない]' do
1474       before do
1475         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
1476         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
1477         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
1478         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
1479         ResourcePicture.any_instance.stub(:store).with(@imager).and_return {
1480           assigns(:resource_picture).attributes = @newattr[:resource_picture]
1481           assigns(:resource_picture).overwrite @newop
1482           assigns(:resource_picture).picture_id = @p.id
1483           assigns(:resource_picture).save!
1484           true
1485         }
1486       end
1487       context 'html形式' do
1488         it 'ステータスコード302 Foundを返す' do
1489           post :create, @newattr
1490           response.status.should eq 302
1491         end
1492         it '作成された素材の表示ページへ遷移する' do
1493           post :create, @newattr
1494           response.should redirect_to(ResourcePicture.last)
1495         end
1496       end
1497       context 'json形式' do
1498         before do
1499           @newattr.merge!({:format => :json})
1500         end
1501         it 'ステータスコード200 OKを返す' do
1502           post :create, @newattr
1503           response.should be_success 
1504         end
1505         it '作成された素材をjsonデータで返す' do
1506           post :create, @newattr
1507           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1508         end
1509       end
1510     end
1511     context 'ユーザ権限がないとき' do
1512       before do
1513         sign_out @user
1514       end
1515       context 'html形式' do
1516         it 'ステータスコード302 Foundを返す' do
1517           post :create, @newattr
1518           response.status.should eq 302
1519         end
1520         it 'サインインページへ遷移する' do
1521           post :create, @newattr
1522           response.body.should redirect_to '/users/sign_in'
1523         end
1524       end
1525     end
1526   end
1527   
1528   describe '削除に於いて' do
1529     before do
1530       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
1531       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
1532       sign_in @user
1533     end
1534     context 'つつがなく終わるとき' do
1535       before do
1536         ResourcePicture.any_instance.stub(:unpublish).with(any_args()).and_return(true)
1537       end
1538       context 'html形式' do
1539         it 'ステータスコード302 Foundを返す' do
1540           delete :destroy, :id => @rp.id
1541           response.status.should eq 302
1542         end
1543         it 'マイ素材の一覧ページへ遷移する' do
1544           delete :destroy, :id => @rp.id
1545           response.should redirect_to('/home/resource_picture')
1546         end
1547       end
1548       context 'json形式' do
1549         it 'ステータスコード200 OKを返す' do
1550           delete :destroy, :id => @rp.id, :format => :json
1551           response.should be_success 
1552         end
1553         it 'ページ本体は特に返さない' do
1554           delete :destroy, :id => @rp.id, :format => :json
1555           response.body.should match /./
1556         end
1557       end
1558     end
1559     context 'ユーザ権限がないとき' do
1560       before do
1561         sign_out @user
1562       end
1563       it 'ステータスコード302 Foundを返す' do
1564         delete :destroy, :id => @rp.id
1565         response.status.should eq 302
1566       end
1567       context 'html形式' do
1568         it 'サインインページへ遷移する' do
1569           delete :destroy, :id => @rp.id
1570           response.body.should redirect_to '/users/sign_in'
1571         end
1572       end
1573       context 'json形式' do
1574         it '応答メッセージにUnauthorizedを返す' do
1575           delete :destroy, :id => @rp.id, :format => :json
1576           response.message.should match(/Unauthorized/)
1577         end
1578       end
1579     end
1580   end
1581
1582 end
1583 end