OSDN Git Service

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