OSDN Git Service

t#29400:itr3?
[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     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 = @user.author
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
15   end
16
17   describe '一覧表示に於いて' do
18     before do
19       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
20       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
21       sign_in @user
22       ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
23     end
24     context 'パラメータpageについて' do
25       it '@pageに値が入る' do
26         get :index, :page => 5
27         assigns(:page).should eq 5
28       end
29       it '省略されると@pageに1値が入る' do
30         get :index
31         assigns(:page).should eq 1
32       end
33       it '与えられたpage_sizeがセットされている' do
34         get :index, :page_size => 15
35         assigns(:page_size).should eq 15
36       end
37       it '省略されると@page_sizeにデフォルト値が入る' do
38         get :index
39         assigns(:page_size).should eq ResourcePicture.default_page_size
40       end
41       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
42         get :index, :page_size => 1500
43         assigns(:page_size).should eq ResourcePicture.max_page_size
44       end
45       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
46         get :index, :page_size => 0
47         assigns(:page_size).should eq ResourcePicture.default_page_size
48       end
49     end
50     context 'つつがなく終わるとき' do
51       it '素材モデルに一覧を問い合わせている' do
52         ResourcePicture.should_receive(:list).exactly(1)
53         get :index
54       end
55       it '@resource_picturesにリストを取得している' do
56         get :index
57         assigns(:resource_pictures).should have_at_least(3).items
58       end
59       context 'html形式' do
60         it 'ステータスコード200 OKを返す' do
61           get :index
62           response.should be_success 
63         end
64         it 'indexテンプレートを描画する' do
65           get :index
66           response.should render_template("index")
67         end
68       end
69       context 'json形式' do
70         it 'ステータスコード200 OKを返す' do
71           get :index, :format => :json
72           response.should be_success 
73         end
74         it 'jsonデータを返す' do
75           get :index, :format => :json
76           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
77         end
78         it '素材モデルにjson一覧出力オプションを問い合わせている' do
79           ResourcePicture.should_receive(:list_json_opt).exactly(1)
80           get :index, :format => :json
81         end
82         it 'データがリスト構造になっている' do
83           get :index, :format => :json
84           json = JSON.parse response.body
85           json.should have_at_least(3).items
86         end
87         it 'リストの先頭くらいは素材っぽいものであって欲しい' do
88           get :index, :format => :json
89           json = JSON.parse response.body
90           json.first.has_key?("ext").should be_true
91           json.first.has_key?("md5").should be_true
92           json.first.has_key?("picture_id").should be_true
93         end
94       end
95     end
96     context '作家権限がないとき' do
97       before do
98         sign_out @user
99       end
100       context 'html形式' do
101         it 'ステータスコード302 Foundを返す' do
102           get :index
103           response.status.should eq 302
104         end
105         it 'サインインページへ遷移する' do
106           get :index
107           response.should redirect_to '/users/sign_in'
108         end
109       end
110       context 'json形式' do
111         it 'ステータスコード401 Unauthorizedを返す' do
112           get :index, :format => :json
113           response.status.should eq 401
114         end
115         it '応答メッセージにUnauthorizedを返す' do
116           get :index, :format => :json
117           response.message.should match(/Unauthorized/)
118         end
119       end
120     end
121   end
122   
123   describe '単体表示に於いて' do
124     before do
125       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
126       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
127       sign_in @user
128       ResourcePicture.stub(:show).with(@rp.id.to_s, @author).and_return(@rp)
129     end
130     context 'つつがなく終わるとき' do
131       it '素材モデルに単体取得を問い合わせている' do
132         ResourcePicture.should_receive(:show).exactly(1)
133         get :show, :id => @rp.id
134       end
135       it '@resource_pictureにアレを取得している' do
136         get :show, :id => @rp.id
137         assigns(:resource_picture).id.should eq(@rp.id)
138       end
139       context 'html形式' do
140         it 'ステータスコード200 OKを返す' do
141           get :show, :id => @rp.id
142           response.should be_success
143         end
144         it 'showテンプレートを描画する' do
145           get :show, :id => @rp.id
146           response.should render_template("show")
147         end
148       end
149       context 'json形式' do
150         it 'ステータスコード200 OKを返す' do
151           get :show, :id => @rp.id, :format => :json
152           response.should be_success
153         end
154         it 'jsonデータを返す' do
155           get :show, :id => @rp.id, :format => :json
156           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
157         end
158         it '素材モデルにjson単体出力オプションを問い合わせている' do
159           ResourcePicture.should_receive(:show_json_opt).exactly(1)
160           get :show, :id => @rp.id, :format => :json
161         end
162         it 'データがアレになっている' do
163           get :show, :id => @rp.id, :format => :json
164           json = JSON.parse response.body
165           json["ext"].should match(/png/)
166           json["md5"].should_not be_nil
167           json["picture_id"].should_not be_nil
168         end
169       end
170       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
171       #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
172       #スタブをおいてsend_dataがデータを返す体裁でテストする。
173       context 'png形式' do
174         before do
175           ResourcePicture.any_instance.stub(:mime_type).and_return('image/png')
176           ResourcePicture.any_instance.stub(:restore).and_return('aaa')
177         end
178         it '画像モデルに画像データを問い合わせる' do
179           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
180           get :show, :id => @rp.id, :format => :png
181         end
182         it '画像モデルにMimeTypeを問い合わせる' do
183           ResourcePicture.any_instance.should_receive(:mime_type).exactly(1)
184           get :show, :id => @rp.id, :format => :png
185         end
186         it '画像を送信する' do
187           get :show, :id => @rp.id, :format => :png
188           response.body.should eq 'aaa'
189         end
190       end
191       context 'gif形式' do
192         before do
193           ResourcePicture.any_instance.stub(:mime_type).and_return('image/gif')
194           ResourcePicture.any_instance.stub(:restore).and_return('bbb')
195         end
196         it '画像モデルに画像データを問い合わせる' do
197           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
198           get :show, :id => @rp.id, :format => :gif
199         end
200         it '画像モデルにMimeTypeを問い合わせる' do
201           ResourcePicture.any_instance.should_receive(:mime_type).exactly(1)
202           get :show, :id => @rp.id, :format => :png
203         end
204         it '画像を送信する' do
205           get :show, :id => @rp.id, :format => :gif
206           response.body.should eq 'bbb'
207         end
208       end
209       context 'jpeg形式' do
210         before do
211           ResourcePicture.any_instance.stub(:mime_type).and_return('image/jpeg')
212           ResourcePicture.any_instance.stub(:restore).and_return('ccc')
213         end
214         it '画像モデルに画像データを問い合わせる' do
215           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
216           get :show, :id => @rp.id, :format => :jpeg
217         end
218         it '画像モデルにMimeTypeを問い合わせる' do
219           ResourcePicture.any_instance.should_receive(:mime_type).exactly(1)
220           get :show, :id => @rp.id, :format => :png
221         end
222         it '画像を送信する' do
223           get :show, :id => @rp.id, :format => :jpeg
224           response.body.should eq 'ccc'
225         end
226       end
227     end
228     context '作家権限がないとき' do
229       before do
230         sign_out @user
231       end
232       context 'html形式' do
233         it 'ステータスコード302 Foundを返す' do
234           get :show, :id => @rp.id
235           response.status.should eq 302
236         end
237         it 'サインインページへ遷移する' do
238           get :show, :id => @rp.id
239           response.body.should redirect_to '/users/sign_in'
240         end
241       end
242       context 'json形式' do
243         it 'ステータスコード401 Unauthorizedを返す' do
244           get :show, :id => @rp.id, :format => :json
245           response.status.should eq 401
246         end
247         it '応答メッセージにUnauthorizedを返す' do
248           get :show, :id => @rp.id, :format => :json
249           response.message.should match(/Unauthorized/)
250         end
251       end
252     end
253 =begin
254     context '対象素材がないとき' do
255       before do
256         ResourcePicture.unstub(:show)
257       end
258       context 'html形式' do
259         it '例外404 not_foundを返す' do
260           lambda{
261             get :show, :id => 0
262           }.should raise_error(ActiveRecord::RecordNotFound)
263         end
264       end
265       context 'json形式' do
266         it '例外404 not_foundを返す' do
267           lambda{ 
268             get :show, :id => 0, :format => :json
269           }.should raise_error(ActiveRecord::RecordNotFound)
270         end
271       end
272     end
273     context '他人の素材を見ようとしたとき' do
274       before do
275         ResourcePicture.stub(:show).and_return(@rp)
276         ResourcePicture.any_instance.stub(:own?).with(any_args()).and_return(false)
277       end
278       context 'html形式' do
279         it '例外403 forbiddenを返す' do
280           lambda{
281             get :show, :id => @rp.id
282           }.should raise_error(ActiveRecord::Forbidden)
283         end
284       end
285       context 'json形式' do
286         it '例外403 forbiddenを返す' do
287           lambda{
288             get :show, :id => @rp.id, :format => :json
289           }.should raise_error(ActiveRecord::Forbidden)
290         end
291       end
292     end
293 =end
294   end
295
296   describe '素材数取得に於いて' do
297     before do
298       ResourcePicture.should_receive(:visible_count).and_return(3)
299 #      sign_in @user
300     end
301     context 'つつがなく終わるとき' do
302       it 'ステータスコード200 OKを返す' do
303         get :count, :format => :json
304         response.should be_success 
305       end
306       context 'json形式' do
307         it 'jsonデータを返す' do
308           get :count, :format => :json
309           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
310         end
311         it 'データがHash構造になっていてコマ絵数が3である' do
312           get :count, :format => :json
313           json = JSON.parse response.body
314           json["count"].should == 3
315         end
316       end
317     end
318   end
319
320   describe 'クレジット表示に於いて' do\r
321     before do\r
322       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
323       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
324       sign_in @user
325       ResourcePicture.stub(:show).with(@rp.id.to_s, @author).and_return(@rp)
326     end\r
327     context 'つつがなく終わるとき' do\r
328       it '素材モデルに単体取得を問い合わせている' do\r
329         ResourcePicture.should_receive(:show).exactly(1)\r
330         get :credit, :id => @rp.id\r
331       end\r
332       it '@resource_pictureにアレを取得している' do\r
333         get :credit, :id => @rp.id\r
334         assigns(:resource_picture).id.should eq(@rp.id)\r
335       end\r
336       context 'html形式' do\r
337         it 'ステータスコード200 OKを返す' do\r
338           get :credit, :id => @rp.id\r
339           response.should be_success\r
340         end\r
341         it 'creditテンプレートを描画する' do\r
342           get :credit, :id => @rp.id\r
343           response.should render_template("credit")\r
344         end\r
345       end\r
346       context 'json形式' do\r
347         it 'ステータスコード200 OKを返す' do\r
348           get :credit, :id => @rp.id, :format => :json\r
349           response.should be_success\r
350         end\r
351         it 'jsonデータを返す' do\r
352           get :credit, :id => @rp.id, :format => :json\r
353           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
354         end\r
355         it '素材モデルにjson単体出力オプションを問い合わせている' do
356           ResourcePicture.should_receive(:show_json_opt).exactly(1)
357           get :credit, :id => @rp.id, :format => :json\r
358         end
359         it 'データがアレになっている' do\r
360           get :credit, :id => @rp.id, :format => :json\r
361           json = JSON.parse response.body\r
362           json["ext"].should match(/png/)\r
363           json["md5"].should_not be_nil
364           json["picture_id"].should_not be_nil
365         end\r
366       end\r
367     end\r
368     context '作家権限がないとき' do\r
369       before do\r
370         sign_out @user\r
371       end\r
372       context 'html形式' do\r
373         it 'ステータスコード302 Foundを返す' do\r
374           get :credit, :id => @rp.id\r
375           response.status.should eq 302\r
376         end\r
377         it 'サインインページへ遷移する' do\r
378           get :credit, :id => @rp.id\r
379           response.body.should redirect_to '/users/sign_in'\r
380         end\r
381       end\r
382       context 'json形式' do\r
383         it 'ステータスコード401 Unauthorizedを返す' do\r
384           get :credit, :id => @rp.id, :format => :json\r
385           response.status.should eq 401\r
386         end\r
387         it '応答メッセージにUnauthorizedを返す' do\r
388           get :credit, :id => @rp.id, :format => :json\r
389           response.message.should match(/Unauthorized/)\r
390         end\r
391       end\r
392     end\r
393 =begin\r
394     context '対象素材がないとき' do\r
395       before do\r
396         ResourcePicture.unstub(:show)\r
397       end\r
398       context 'html形式' do\r
399         it '例外404 not_foundを返す' do\r
400           lambda{\r
401             get :show, :id => 0\r
402           }.should raise_error(ActiveRecord::RecordNotFound)\r
403         end\r
404       end\r
405       context 'json形式' do\r
406         it '例外404 not_foundを返す' do\r
407           lambda{ \r
408             get :show, :id => 0, :format => :json\r
409           }.should raise_error(ActiveRecord::RecordNotFound)\r
410         end\r
411       end\r
412     end\r
413 =end\r
414   end
415   
416   #原画にライセンスを与えるため際の確認フォーム\r
417   describe '新規作成フォーム表示に於いて' do
418     before do
419       sign_in @user
420       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
421       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
422       @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}, 
423         :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
424       @newop = FactoryGirl.create :original_picture, :artist_id => @artist.id
425       @newattr = {:original_picture_id => @newop.id, :original_picture_license_group => {:original_picture_id => @newop.id, :license_group_id => @lg.id}, 
426         :resource_picture => {:original_picture_id => @newop.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
427       @imager = ImagerTest.load("abc\ndef\nghi")
428     end
429     context '事前チェックしておく' do
430       before do
431         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
432         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
433         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
434         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
435         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
436         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
437         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
438       end
439       it '原画モデルに編集取得を依頼してしている' do
440         OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
441         get :new, @attr
442       end
443       it '原画モデルに画像データを問い合わせている' do
444         OriginalPicture.any_instance.should_receive(:restore).with(any_args()).exactly(1)
445         get :new, @attr
446       end
447       it '画像ライブラリをロードしている' do
448         PettanImager.should_receive(:load).with(any_args()).exactly(1)
449         get :new, @attr
450       end
451       it 'ライセンスグループモデルに単体取得を依頼している' do
452         LicenseGroup.should_receive(:show).with(any_args()).exactly(1)
453         get :new, @attr
454       end
455       context 'ライセンスを与えようとしている原画が素材を作成してないとき' do
456         it '素材モデルにデフォルト値補充を依頼している' do
457           ResourcePicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
458           get :new, @newattr
459         end
460       end
461       context 'ライセンスを与えようとしている原画が既に素材を作成しているとき' do
462         it '素材モデルにデフォルト値補充を依頼してない' do
463           ResourcePicture.any_instance.should_not_receive(:supply_default).with(any_args())
464           get :new, @attr
465         end
466       end
467       it '素材モデルに上書き補充を依頼している' do
468         ResourcePicture.any_instance.should_receive(:overwrite).with(@op).exactly(1)
469         get :new, @attr
470       end
471     end
472     context 'つつがなく終わるとき' do
473       before do
474         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
475         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
476         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
477         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
478         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
479         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
480         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
481       end
482       it '@original_pictureに原画を取得している' do
483         get :new, @attr
484         assigns(:original_picture).should eq @op
485       end
486       it '@imagerに画像ライブラリをロードしている' do
487         get :new, @attr
488         assigns(:imager).should eq @imager
489       end
490       it '@original_picture_license_groupに新規原画ライセンスグループデータを用意している' do
491         get :new, @attr
492         assigns(:original_picture_license_group).should be_a_new(OriginalPictureLicenseGroup)
493       end
494       it '@license_groupにライセンスグループを取得している' do
495         get :new, @attr
496         assigns(:license_group).should eq @lg
497       end
498       context 'ライセンスを与えようとしている原画が素材を作成してないとき' do
499         it '@resource_pictureに新規素材データを用意している' do
500           get :new, @newattr
501           assigns(:resource_picture).should be_a_new(ResourcePicture)
502         end
503       end
504       context 'ライセンスを与えようとしている原画が既に素材を作成しているとき' do
505         it '@resource_pictureに素材データを用意している' do
506           get :new, @attr
507           assigns(:resource_picture).is_a?(ResourcePicture).should be_true
508           assigns(:resource_picture).should_not be_a_new(ResourcePicture)
509         end
510       end
511       context 'html形式' do
512         it 'ステータスコード200 OKを返す' do
513           get :new, @attr
514           response.should be_success 
515         end
516         it 'ページテンプレートnewを描画する' do
517           get :new, @attr
518           response.should render_template("new")
519         end
520       end
521       context 'js形式' do
522         before do
523           @attr.merge!({:format => :js})
524         end
525         it 'ステータスコード200 OKを返す' do
526           get :new, @attr
527           response.should be_success 
528         end
529         it '部分テンプレートnew.jsを描画する' do
530           get :new, @attr
531           response.should render_template("new")
532         end
533       end
534     end
535     context '作家権限がないとき' do
536       before do
537         sign_out @user
538       end
539       context 'html形式' do
540         it 'ステータスコード302 Foundを返す' do
541           get :new, @attr
542           response.status.should eq 302
543         end
544         it 'サインインページへ遷移する' do
545           get :new, @attr
546           response.body.should redirect_to '/users/sign_in'
547         end
548       end
549       context 'js形式' do
550         before do
551           @attr.merge!({:format => :js})
552         end
553         it 'ステータスコード401 Unauthorizedを返す' do
554           get :new, @attr
555           response.status.should eq 401
556         end
557         it '応答メッセージにUnauthorizedを返す' do
558           get :new, @attr
559           response.message.should match(/Unauthorized/)
560         end
561       end
562     end
563     context '作家が絵師でないとき' do
564       before do
565         Author.any_instance.stub(:artist?).and_return(false)
566       end
567       context 'html形式' do
568         it 'ステータスコード302 Foundを返す' do
569           get :new, @attr
570           response.status.should eq 302
571         end
572         it '絵師登録ページへ遷移する' do
573           get :new, @attr
574           response.should redirect_to new_artist_path
575         end
576       end
577       context 'js形式' do
578         before do
579           @attr.merge!({:format => :js})
580         end
581         it 'ステータスコード200 Okを返す' do
582           get :new, @attr
583           response.status.should eq 200
584         end
585         it '絵師登録部分テンプレートartists/new.jsを描画する' do
586           get :new, @attr
587           response.should render_template("artists/new")
588         end
589       end
590     end
591     context '対象ライセンスグループがないとき' do\r
592       before do\r
593         @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => 0}, 
594           :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{}' }}
595       end\r
596       context 'html形式' do\r
597         it '例外404 not_foundを返す' do\r
598           lambda{\r
599             get :new, @attr
600           }.should raise_error(ActiveRecord::RecordNotFound)\r
601         end\r
602       end\r
603       context 'json形式' do\r
604         before do
605           @attr.merge!({:format => :js})
606         end
607         it '例外404 not_foundを返す' do\r
608           lambda{ \r
609             get :new, @attr\r
610           }.should raise_error(ActiveRecord::RecordNotFound)\r
611         end\r
612       end\r
613     end\r
614   end
615   
616   describe '新規作成に於いて' do
617     before do
618       sign_in @user
619       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
620       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
621       @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}, 
622         :resource_picture => {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{"new":0}' }}
623       @newop = FactoryGirl.create :original_picture, :artist_id => @artist.id
624       @newattr = {:original_picture_id => @newop.id, :original_picture_license_group => {:original_picture_id => @newop.id, :license_group_id => @lg.id}, 
625         :resource_picture => {:original_picture_id => @newop.id, :license_id => @license.id, :artist_name => @artist.name, :credit => '{}', :settings => '{"new":1}' }}
626       @imager = ImagerTest.load("abc\ndef\nghi")
627     end
628     context '事前チェックしておく' do
629       before do
630         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
631         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
632         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
633         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
634         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
635         ResourcePicture.any_instance.stub(:overwrite).with(@op).and_return(true)
636         ResourcePicture.any_instance.stub(:overwrite).with(@newop).and_return(true)
637         ResourcePicture.any_instance.stub(:store).with(@imager).and_return(true)
638       end
639       it '原画モデルに編集取得を依頼してしている' do
640         OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
641         post :create, @attr
642       end
643       it '原画モデルに画像データを問い合わせている' do
644         OriginalPicture.any_instance.should_receive(:restore).with(any_args()).exactly(1)
645         post :create, @attr
646       end
647       it '画像ライブラリをロードしている' do
648         PettanImager.should_receive(:load).with(any_args()).exactly(1)
649         post :create, @attr
650       end
651       it 'ライセンスグループモデルに単体取得を依頼している' do
652         LicenseGroup.should_receive(:show).with(any_args()).exactly(1)
653         post :create, @attr
654       end
655       context 'ライセンスを与えようとしている原画が素材を作成してないとき' do
656         it '素材モデルにデフォルト値補充を依頼している' do
657           ResourcePicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
658           post :create, @newattr
659         end
660       end
661       context 'ライセンスを与えようとしている原画が既に素材を作成しているとき' do
662         it '素材モデルにデフォルト値補充を依頼してない' do
663           ResourcePicture.any_instance.should_not_receive(:supply_default).with(any_args())
664           post :create, @attr
665         end
666       end
667       it '素材モデルに上書き補充を依頼している' do
668         ResourcePicture.any_instance.should_receive(:overwrite).with(@op).exactly(1)
669         post :create, @attr
670       end
671       it '素材モデルに保存を依頼している' do
672         ResourcePicture.any_instance.should_receive(:store).with(@imager).exactly(1)
673         post :create, @attr
674       end
675     end
676     context 'つつがなく終わるとき[ライセンスを与えようとしている原画が素材を作成してない]' do
677       before do
678         OriginalPicture.stub(:edit).with(@newop.id.to_s, @artist).and_return(@newop)
679         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
680         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
681         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
682         ResourcePicture.any_instance.stub(:store).with(@imager).and_return {
683           assigns(:resource_picture).attributes = @newattr[:resource_picture]
684           assigns(:resource_picture).overwrite @newop
685           assigns(:resource_picture).picture_id = @p.id
686           assigns(:resource_picture).save!
687           true
688         }
689       end
690       it '@original_pictureに原画を取得している' do
691         post :create, @newattr
692         assigns(:original_picture).should eq @newop
693       end
694       it '@imagerに画像ライブラリをロードしている' do
695         post :create, @newattr
696         assigns(:imager).should eq @imager
697       end
698       it '@original_picture_license_groupに新規原画ライセンスグループデータを用意している' do
699         post :create, @newattr
700         assigns(:original_picture_license_group).should be_a_new(OriginalPictureLicenseGroup)
701       end
702       it '@license_groupにライセンスグループを取得している' do
703         post :create, @newattr
704         assigns(:license_group).should eq @lg
705       end
706       it '素材データにPOSTデータの素材情報を適用している' do
707         post :create, @newattr
708         assigns(:resource_picture).license_id.should eq @license.id
709         assigns(:resource_picture).artist_name.should eq @artist.name
710         assigns(:resource_picture).credit.should eq '{}'
711         assigns(:resource_picture).settings.should eq '{"new":1}'
712       end
713       it "作成された素材がDBにある" do
714         lambda{\r
715           post :create, @newattr
716         }.should change(ResourcePicture, :count)\r
717       end
718       context 'html形式' do
719         it 'ステータスコード302 Foundを返す' do
720           post :create, @newattr
721           response.status.should eq 302
722         end
723         it '作成された素材の表示ページへ遷移する' do
724           post :create, @newattr
725           response.should redirect_to(ResourcePicture.last)
726         end
727       end
728       context 'json形式' do
729         before do
730           @newattr.merge!({:format => :json})
731         end
732         it 'ステータスコード200 OKを返す' do
733           post :create, @newattr
734           response.should be_success 
735         end
736         it '作成された素材をjsonデータで返す' do
737           post :create, @newattr
738           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
739         end
740         it '素材モデルにjson単体出力オプションを問い合わせている' do
741           ResourcePicture.should_receive(:show_json_opt).exactly(1)
742           post :create, @newattr
743         end
744         it 'データがアレになっている' do
745           post :create, @newattr
746           json = JSON.parse response.body
747           json["ext"].should match(/png/)\r
748           json["md5"].should_not be_nil
749           json["picture_id"].should_not be_nil
750         end
751       end
752     end
753     context 'つつがなく終わるとき[ライセンスを与えようとしている原画が既に素材を作成している]' do
754       before do
755         OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
756         OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
757         PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
758         LicenseGroup.stub(:show).with(@lg.id).and_return(@lg)
759         ResourcePicture.any_instance.stub(:store).with(@imager).and_return {
760           assigns(:resource_picture).attributes = @attr[:resource_picture]
761           assigns(:resource_picture).overwrite @op
762           assigns(:resource_picture).save!
763           true
764         }
765       end
766       it '@original_pictureに原画を取得している' do
767         post :create, @attr
768         assigns(:original_picture).should eq @op
769       end
770       it '素材データにPOSTデータの素材情報を適用している' do
771         post :create, @attr
772         assigns(:resource_picture).license_id.should eq @license.id
773         assigns(:resource_picture).artist_name.should eq @artist.name
774         assigns(:resource_picture).credit.should eq '{}'
775         assigns(:resource_picture).settings.should eq '{"new":0}'
776       end
777       it "素材が更新される" do
778         post :create, @attr
779         r = ResourcePicture.find(@rp.id)
780         r.settings.should eq '{"new":0}'\r
781       end
782       it "上書きなので件数は変化しない" do
783         lambda{\r
784           post :create, @attr
785         }.should_not change(ResourcePicture, :count)\r
786       end
787     end
788     context '作家権限がないとき' do
789       before do
790         sign_out @user
791       end
792       context 'html形式' do
793         it 'ステータスコード302 Foundを返す' do
794           post :create, @attr
795           response.status.should eq 302
796         end
797         it 'サインインページへ遷移する' do
798           post :create, @attr
799           response.body.should redirect_to '/users/sign_in'
800         end
801       end
802       context 'json形式' do
803         before do
804           @attr.merge!({:format => :js})
805         end
806         it 'ステータスコード401 Unauthorizedを返す' do
807           post :create, @attr
808           response.status.should eq 401
809         end
810         it '応答メッセージにUnauthorizedを返す' do
811           post :create, @attr
812           response.message.should match(/Unauthorized/)
813         end
814       end
815     end
816     context '作家が絵師でないとき' do
817       before do
818         Author.any_instance.stub(:artist?).and_return(false)
819       end
820       context 'html形式' do
821         it 'ステータスコード302 Foundを返す' do
822           post :create, @attr
823           response.status.should eq 302
824         end
825         it '絵師登録ページへ遷移する' do
826           post :create, @attr
827           response.should redirect_to new_artist_path
828         end
829       end
830       context 'json形式' do
831         before do
832           @attr.merge!({:format => :json})
833         end
834         it '例外403 forbiddenを返す' do
835           lambda{
836             post :create, @attr
837           }.should raise_error(ActiveRecord::Forbidden)
838         end
839       end
840     end
841     context '検証、保存に失敗した' do
842       before do
843         ResourcePicture.any_instance.stub(:store).and_return(false)
844       end
845       it "未保存の素材を保持している" do
846         post :create, @newattr
847         assigns(:resource_picture).should be_a_new(ResourcePicture)
848       end
849       context 'html形式' do
850         it 'ステータスコード200 OKを返す' do
851           post :create, @newattr
852           response.status.should eq 200
853         end
854         it '新規ページを描画する' do
855           post :create, @newattr
856           response.should render_template("new")
857         end
858       end
859       context 'json形式' do
860         before do
861           @newattr.merge!({:format => :json})
862         end
863         it 'ステータスコード422 unprocessable_entity を返す' do
864           post :create, @newattr
865           response.status.should eq 422
866         end
867         it '応答メッセージUnprocessable Entityを返す' do
868           post :create, @newattr
869           response.message.should match(/Unprocessable/)
870         end
871       end
872     end
873   end
874   
875 end