OSDN Git Service

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