OSDN Git Service

t#29983:show panel with license
[pettanr/pettanr.git] / spec / models / resource_picture_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 #素材\r
3 require 'spec_helper'\r
4 \r
5 describe ResourcePicture do\r
6   before do\r
7     @admin = FactoryGirl.create :admin\r
8     @user = FactoryGirl.create( :user_yas)\r
9     @author = @user.author\r
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id\r
11     @other_user = FactoryGirl.create( :user_yas)\r
12     @other_author = @other_user.author\r
13     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id\r
14     @sp = FactoryGirl.create :system_picture\r
15     @lg = FactoryGirl.create :license_group\r
16     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id\r
17     @original_picture = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
18     \r
19     #原画ファイル削除だけは必ず成功するものとしておく\r
20     PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)\r
21     Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh'])\r
22     Picture.any_instance.stub(:h).with(any_args).and_return('data')\r
23     Picture.any_instance.stub(:v).with(any_args).and_return('data')\r
24     Picture.any_instance.stub(:vh).with(any_args).and_return('data')\r
25   end\r
26   \r
27   describe '検証に於いて' do\r
28     before do\r
29       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
30       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
31     end\r
32     \r
33     context 'オーソドックスなデータのとき' do
34       it '下限データが通る' do
35         @rp.ext = 'png' #リストにない拡張子は通らないし
36         @rp.width = 1
37         @rp.height = 1
38         @rp.filesize = 1
39         @rp.md5 = 'a'*32
40         @rp.classname = 'a'*1
41         @rp.should be_valid
42       end
43       it '上限データが通る' do
44         @rp.ext = 'jpeg'
45         @rp.width = 99999
46         @rp.height = 99999
47         @rp.filesize = 2000000
48         @rp.md5 = 'a'*32
49         @rp.classname = 'a'*50
50         @rp.should be_valid
51       end
52     end
53     
54     context 'extを検証するとき' do\r
55       it 'nullなら失敗する' do\r
56         @rp.ext = ''\r
57         @rp.should_not be_valid\r
58       end\r
59       it '5文字以上なら失敗する' do\r
60         @rp.ext = 'a'*5\r
61         @rp.should_not be_valid\r
62       end\r
63       it 'png,gif,jpeg以外なら失敗する' do\r
64         @rp.ext = 'bmp'\r
65         @rp.should_not be_valid\r
66       end\r
67     end\r
68     context 'widthを検証するとき' do\r
69       it 'nullなら失敗する' do\r
70         @rp.width = nil\r
71         @rp.should_not be_valid\r
72       end\r
73       it '数値でなければ失敗する' do\r
74         @rp.width = 'a'\r
75         @rp.should_not be_valid\r
76       end\r
77       it '0なら失敗する' do\r
78         @rp.width = '0'\r
79         @rp.should_not be_valid\r
80       end\r
81       it '負でも失敗する' do\r
82         @rp.width = -1\r
83         @rp.should_not be_valid\r
84       end\r
85     end\r
86     context 'heightを検証するとき' do\r
87       it 'nullなら失敗する' do\r
88         @rp.height = nil\r
89         @rp.should_not be_valid\r
90       end\r
91       it '数値でなければ失敗する' do\r
92         @rp.height = 'a'\r
93         @rp.should_not be_valid\r
94       end\r
95       it '0なら失敗する' do\r
96         @rp.height = '0'\r
97         @rp.should_not be_valid\r
98       end\r
99       it '負でも失敗する' do\r
100         @rp.height = -1\r
101         @rp.should_not be_valid\r
102       end\r
103     end\r
104     context 'filesizeを検証するとき' do\r
105       it 'nullなら失敗する' do\r
106         @rp.filesize = nil\r
107         @rp.should_not be_valid\r
108       end\r
109       it '数値でなければ失敗する' do\r
110         @rp.filesize = 'a'\r
111         @rp.should_not be_valid\r
112       end\r
113       it '負なら失敗する' do\r
114         @rp.filesize = '-1'\r
115         @rp.should_not be_valid\r
116       end\r
117       it '2MB以上なら失敗する' do\r
118         @rp.filesize = 2000000+1\r
119         @rp.should_not be_valid\r
120       end\r
121     end\r
122     context 'artist_idを検証するとき' do\r
123       it 'nullなら失敗する' do\r
124         @rp.artist_id = nil\r
125         @rp.should_not be_valid\r
126       end\r
127       it '数値でなければ失敗する' do\r
128         @rp.artist_id = 'a'\r
129         @rp.should_not be_valid\r
130       end\r
131       it '存在する絵師でなければ失敗する' do\r
132         @rp.artist_id = 0\r
133         @rp.should_not be_valid\r
134       end\r
135     end\r
136     context 'md5を検証するとき' do\r
137       it 'nullなら失敗する' do\r
138         @rp.md5 = ''\r
139         @rp.should_not be_valid\r
140       end\r
141       it '31文字なら失敗する' do
142         @rp.md5 = 'a'*31
143         @rp.should_not be_valid
144       end
145       it '32文字以上なら失敗する' do\r
146         @rp.md5 = 'a'*33\r
147         @rp.should_not be_valid\r
148       end\r
149     end\r
150     context 'license_idを検証するとき' do\r
151       it 'nullなら失敗する' do\r
152         @rp.license_id = nil\r
153         @rp.should_not be_valid\r
154       end\r
155       it '数値でなければ失敗する' do\r
156         @rp.license_id = 'a'\r
157         @rp.should_not be_valid\r
158       end\r
159       it '存在する絵師でなければ失敗する' do\r
160         @rp.license_id = 0\r
161         @rp.should_not be_valid\r
162       end\r
163     end\r
164     context 'original_picture_idを検証するとき' do\r
165       it 'nullなら失敗する' do\r
166         @rp.original_picture_id = nil\r
167         @rp.should_not be_valid\r
168       end\r
169       it '数値でなければ失敗する' do\r
170         @rp.original_picture_id = 'a'\r
171         @rp.should_not be_valid\r
172       end\r
173       it '存在する原画でなければ失敗する' do\r
174         @rp.original_picture_id = 0\r
175         @rp.should_not be_valid\r
176       end\r
177     end\r
178     context 'artist_nameを検証するとき' do\r
179       it 'nullなら失敗する' do\r
180         @rp.artist_name = nil\r
181         @rp.should_not be_valid\r
182       end\r
183     end\r
184     context 'classnameを検証するとき' do\r
185       it 'nullなら失敗する' do\r
186         @rp.classname = ''\r
187         @rp.should_not be_valid\r
188       end\r
189       it '51文字以上なら失敗する' do\r
190         @rp.classname = 'a'*51\r
191         @rp.should_not be_valid\r
192       end\r
193     end\r
194     context 'creditを検証するとき' do\r
195     end\r
196     context 'settingsを検証するとき' do\r
197     end\r
198     context 'picture_idを検証するとき' do\r
199       it 'nullなら失敗する' do\r
200         @rp.picture_id = nil\r
201         @rp.should_not be_valid\r
202       end\r
203       it '数値でなければ失敗する' do\r
204         @rp.picture_id = 'a'\r
205         @rp.should_not be_valid\r
206       end\r
207       it '存在する実素材でなければ失敗する' do\r
208         @rp.picture_id = 0\r
209         @rp.should_not be_valid\r
210       end\r
211     end\r
212   end\r
213   \r
214   describe 'デフォルト値補充に於いて' do
215     it 'defined' do
216       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
217       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
218       @rp.supply_default
219     end
220   end
221   
222   describe '上書き補充に於いて' do
223     before do
224       @original_picture.attributes = {:ext => 'gif', :width => 267, :height => 268, :filesize => 269, 
225         :artist_id => @artist.id }
226       @rp = FactoryGirl.build :resource_picture, :original_picture_id => @original_picture.id
227     end
228     it 'width, height, ext, filesize, md5, original_picture_id, artist_idが設定されている' do
229       @rp.overwrite @original_picture
230       @rp.width.should eq 267
231       @rp.height.should eq 268
232       @rp.ext.should eq 'gif'
233       @rp.filesize.should eq 269
234       @rp.md5.should eq @rp.md5
235       @rp.original_picture_id.should eq @original_picture.id
236       @rp.artist_id.should eq @artist.id
237     end
238   end
239   
240   describe '所持判定に於いて' do
241     before do
242       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
243       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
244     end
245     it '素材を更新することはないので、Falseを返す' do
246       @rp.own?(@author).should == false
247     end
248   end
249   
250   describe '閲覧許可に於いて' do
251     before do
252       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
253       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
254     end
255     context '検査対象がnil(ゲスト)のとき' do
256       context 'クローズドモードのとき' do
257         before do
258           MagicNumber['run_mode'] = 1
259         end
260         it '不許可を返す。' do\r
261           r = @rp.visible?(nil)
262           r.should be_false
263         end\r
264       end\r
265       context 'オープンモードのとき' do
266         before do
267           MagicNumber['run_mode'] = 0
268         end
269         it '許可する' do\r
270           r = @rp.visible?(nil)
271           r.should be_true
272         end\r
273       end\r
274     end\r
275     context '検査対象が作家のとき' do
276       it '許可する' do\r
277         r = @rp.visible?(@author)
278         r.should == true
279       end\r
280     end\r
281     context '検査対象がそれ以外のとき' do
282       it '不許可を返す。' do\r
283         r = @rp.visible?(@admin)
284         r.should be_false
285       end\r
286     end\r
287   end
288   
289   describe 'ファイル名に於いて' do
290     before do
291       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
292       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
293     end
294     it 'id+拡張子のフォーマットで返す' do
295       r = @rp.filename
296       r.should eq "#{@rp.id}.png"
297     end
298   end
299   
300   describe 'MimeTypeに於いて' do
301     before do
302       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
303       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
304     end
305     it 'image/拡張子のフォーマットで返す' do
306       r = @rp.mime_type
307       r.should eq "image/png"
308     end
309   end
310   
311   describe 'ファイルのurlに於いて' do
312     before do
313       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
314       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
315       ResourcePicture.any_instance.stub(:filename).and_return('3.gif')
316     end
317     it 'ファイル名取得を依頼している' do
318       ResourcePicture.any_instance.should_receive(:filename).exactly(1)
319       @rp.url
320     end
321     it '/resource_pictures/3.gifのフォーマットで返す' do
322       r = @rp.url
323       r.should eq "/resource_pictures/3.gif"
324     end
325   end
326   
327   describe '一覧取得に於いて' do\r
328     before do\r
329       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
330       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
331       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
332     end\r
333     context 'page補正について' do\r
334       it '文字列から数値に変換される' do\r
335         ResourcePicture.page('8').should eq 8\r
336       end\r
337       it 'nilの場合は1になる' do\r
338         ResourcePicture.page().should eq 1\r
339       end\r
340       it '0以下の場合は1になる' do\r
341         ResourcePicture.page('0').should eq 1\r
342       end\r
343     end\r
344     context 'page_size補正について' do\r
345       it '文字列から数値に変換される' do\r
346         ResourcePicture.page_size('7').should eq 7\r
347       end\r
348       it 'nilの場合はResourcePicture.default_page_sizeになる' do\r
349         ResourcePicture.page_size().should eq ResourcePicture.default_page_size\r
350       end\r
351       it '0以下の場合はResourcePicture.default_page_sizeになる' do\r
352         ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size\r
353       end\r
354       it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do\r
355         ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size\r
356       end\r
357     end\r
358     it 'リストを返す' do\r
359       r = ResourcePicture.list\r
360       r.should eq [@rp]\r
361     end\r
362     it '時系列で並んでいる' do\r
363       nop = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
364       nrp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
365       r = ResourcePicture.list\r
366       r.should eq [nrp, @rp]\r
367     end\r
368     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
369       before do\r
370         nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
371         @nrp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
372         nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
373         @nrp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
374         nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
375         @nrp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
376         nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
377         @nrp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
378         ResourcePicture.stub(:default_page_size).and_return(2)\r
379       end\r
380       it '通常は2件を返す' do\r
381         r = ResourcePicture.list\r
382         r.should have(2).items \r
383       end\r
384       it 'page=1なら末尾2件を返す' do\r
385         #時系列で並んでいる\r
386         r = ResourcePicture.list(1)\r
387         r.should eq [@nrp5, @nrp4]\r
388       end\r
389       it 'page=2なら中間2件を返す' do\r
390         r = ResourcePicture.list(2)\r
391         r.should eq [@nrp3, @nrp2]\r
392       end\r
393       it 'page=3なら先頭1件を返す' do\r
394         r = ResourcePicture.list(3)\r
395         r.should eq [@rp]\r
396       end\r
397     end\r
398     context 'DBに5件あって1ページの件数を2件に変えたとして' do
399       before do
400         nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
401         @nrp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
402         nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
403         @nrp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
404         nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
405         @nrp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
406         nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
407         @nrp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
408         ResourcePicture.stub(:default_page_size).and_return(2)\r
409       end
410       it '件数0は全件(5件)を返す' do
411         r = ResourcePicture.list 5, 0
412         r.should have(5).items 
413       end
414     end
415   end\r
416   describe '一覧取得オプションに於いて' do\r
417     it 'includeキーを含んでいる' do
418       r = ResourcePicture.list_opt
419       r.has_key?(:include).should be_true
420     end
421     it '3つの項目を含んでいる' do
422       r = ResourcePicture.list_opt[:include]
423       r.should have(3).items
424     end
425     it 'ライセンスを含んでいる' do
426       r = ResourcePicture.list_opt[:include]
427       r.has_key?(:license).should be_true
428     end
429     it '絵師を含んでいる' do
430       r = ResourcePicture.list_opt[:include]
431       r.has_key?(:artist).should be_true
432     end
433     it '実素材を含んでいる' do
434       r = ResourcePicture.list_opt[:include]
435       r.has_key?(:picture).should be_true
436     end
437   end\r
438   describe 'json一覧出力オプションに於いて' do
439     before do
440       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
441       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
442       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
443       @sbt = FactoryGirl.create :speech_balloon_template\r
444       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
445       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
446       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
447     end
448     it 'ライセンスを含んでいる' do
449       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt
450       j = JSON.parse r
451       i = j.first
452       i.has_key?('license').should be_true
453     end
454     it '絵師を含んでいる' do
455       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt
456       j = JSON.parse r
457       i = j.first
458       i.has_key?('artist').should be_true
459     end
460     it '実素材を含んでいる' do
461       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt
462       j = JSON.parse r
463       i = j.first
464       i.has_key?('picture').should be_true
465     end
466   end
467   \r
468   describe '単体取得に於いて' do\r
469     before do\r
470       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
471       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
472       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
473     end\r
474     context 'つつがなく終わるとき' do
475       it '単体取得オプションを利用している' do
476         ResourcePicture.stub(:show_opt).with(any_args).and_return({})
477         ResourcePicture.should_receive(:show_opt).with(any_args).exactly(1)
478         r = ResourcePicture.show @rp.id, @author
479       end
480       it '閲覧許可を問い合わせている' do
481         ResourcePicture.any_instance.stub(:visible?).with(any_args).and_return(true)
482         ResourcePicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
483         r = ResourcePicture.show @rp.id, @author
484       end
485     end
486     it '指定の素材を返す' do\r
487       r = ResourcePicture.show @rp.id, @author\r
488       r.should eq @rp\r
489     end\r
490     context '他人の素材を開こうとしたとき' do
491       it '403Forbidden例外を返す' do
492         ResourcePicture.any_instance.stub(:visible?).and_return(false)
493         lambda{
494           r = ResourcePicture.show @rp.id, @other_author
495         }.should raise_error(ActiveRecord::Forbidden)
496       end
497     end
498     context '存在しない素材を開こうとしたとき' do\r
499       it '404RecordNotFound例外を返す' do\r
500         lambda{\r
501           r = ResourcePicture.show 0, @author\r
502         }.should raise_error(ActiveRecord::RecordNotFound)\r
503       end\r
504     end\r
505   end\r
506   describe '単体取得オプションに於いて' do\r
507     it 'includeキーを含んでいる' do
508       r = ResourcePicture.show_opt
509       r.has_key?(:include).should be_true
510     end
511     it '3つの項目を含んでいる' do
512       r = ResourcePicture.show_opt[:include]
513       r.should have(3).items
514     end
515     it 'ライセンスを含んでいる' do
516       r = ResourcePicture.show_opt[:include]
517       r.has_key?(:license).should be_true
518     end
519     it '絵師を含んでいる' do
520       r = ResourcePicture.show_opt[:include]
521       r.has_key?(:artist).should be_true
522     end
523     it '実素材を含んでいる' do
524       r = ResourcePicture.show_opt[:include]
525       r.has_key?(:picture).should be_true
526     end
527   end\r
528   describe 'json単体出力オプションに於いて' do\r
529     before do
530       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
531       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
532       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
533       @sbt = FactoryGirl.create :speech_balloon_template\r
534       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
535       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
536       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
537     end
538     it 'ライセンスを含んでいる' do
539       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt
540       j = JSON.parse r
541       i = j
542       i.has_key?('license').should be_true
543     end
544     it '絵師を含んでいる' do
545       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt
546       j = JSON.parse r
547       i = j
548       i.has_key?('artist').should be_true
549     end
550     it '実素材を含んでいる' do
551       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt
552       j = JSON.parse r
553       i = j
554       i.has_key?('picture').should be_true
555     end
556   end\r
557   \r
558   describe 'フォーマット変換対象判定に於いて' do\r
559     before do\r
560       @rp = FactoryGirl.build :resource_picture, \r
561         :artist_id => @artist.id, :license_id => @license.id\r
562     end\r
563     context '変換するケース' do\r
564       it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do\r
565         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
566         @rp.ext = 'png'\r
567         @rp.to_gif?.should be_true\r
568       end\r
569     end\r
570     context '変換しないケース' do\r
571       it '画像フォーマットがでない' do\r
572         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
573         @rp.ext = 'gif'\r
574         @rp.to_gif?.should be_false\r
575       end\r
576       it '変換禁止フラグが無効' do\r
577         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)\r
578         @rp.ext = 'png'\r
579         @rp.to_gif?.should be_false\r
580       end\r
581     end\r
582   end\r
583   \r
584   describe '新規実素材の取得に於いて' do\r
585     before do\r
586       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
587       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
588       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', \r
589         :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s}\r
590       @rp = FactoryGirl.build :resource_picture, attr\r
591       @imager = ImagerTest.load "abc\ndef\nghi"
592     end\r
593     context '事前チェック' do\r
594       before do\r
595         Picture.any_instance.stub(:store).with(any_args).and_return(true)\r
596       end\r
597       it '実素材モデルにデフォルト値補充を依頼している' do\r
598         Picture.any_instance.should_receive(:supply_default).with(any_args).exactly(1)\r
599         r = @rp.new_picture @imager\r
600       end\r
601       it '実素材モデルに上書き補充を依頼している' do\r
602         Picture.any_instance.should_receive(:overwrite).with(any_args).exactly(1)\r
603         r = @rp.new_picture @imager\r
604       end\r
605       it '実素材を保存している' do\r
606         Picture.any_instance.should_receive(:store).with(any_args).exactly(1)\r
607         r = @rp.new_picture @imager\r
608       end\r
609     end\r
610     context 'つつがなく終わるとき' do\r
611       it '保存された実素材を返す' do\r
612         r = @rp.new_picture @imager\r
613         r.is_a?(Picture).should be_true\r
614       end\r
615     end\r
616     #以下から例外ケース。処理先頭から失敗させていく\r
617     context '実素材の保存に失敗したとき' do\r
618       before do\r
619         Picture.any_instance.stub(:store).with(any_args).and_return(false)\r
620       end\r
621       it 'Falseを返す' do\r
622         r = @rp.new_picture @imager\r
623         r.should be_false\r
624       end\r
625       it '自身の全体エラーメッセージにその旨をセットしている' do\r
626         r = @rp.new_picture @imager\r
627         @rp.errors[:base].should_not be_empty\r
628       end\r
629     end\r
630   end\r
631   \r
632   describe '作成・更新に於いて' do\r
633     before do\r
634       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
635       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
636       attr = {:original_picture_id => @op.id, :picture_id => nil, :license_id => @license.id, :artist_name => 'tester', :classname => 'StandardLicense', :credit => '{}', :settings => {}.to_json.to_s}\r
637       @rp = FactoryGirl.build :resource_picture, attr\r
638       @imager = ImagerTest.load "abc\ndef\nghi"
639     end\r
640     context '事前チェック' do\r
641       before do\r
642         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
643         #それで外部のメソッド呼び出しだけに着目してテストする。\r
644         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
645         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)\r
646       end\r
647       it '新規実素材の取得を依頼している' do\r
648         ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1)\r
649         r = @rp.store @imager\r
650       end\r
651       it '自身を保存している' do\r
652         ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)\r
653         r = @rp.store @imager\r
654       end\r
655       it 'gif付き画像ファイルの作成・更新機能で画像を保存している' do\r
656         ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@imager).exactly(1)\r
657         r = @rp.store @imager\r
658       end\r
659     end\r
660     context 'つつがなく終わるとき' do\r
661       before do\r
662         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
663         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
664         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(@imager).and_return(true)\r
665       end\r
666       it 'Trueを返す' do\r
667         r = @rp.store @imager\r
668         r.should be_true\r
669       end\r
670       it '実素材idから最新画像idを取得してセットしている' do\r
671         r = @rp.store @imager\r
672         @rp.picture_id.should eq @p.id\r
673       end\r
674       it '自身が保存されている' do\r
675         lambda {\r
676           r = @rp.store @imager\r
677         }.should change ResourcePicture, :count\r
678       end\r
679     end\r
680     #以下から例外ケース。処理先頭から失敗させていく\r
681     context '画像オブジェクトの取得に失敗したとき' do\r
682       before do\r
683         @imager = false\r
684       end\r
685       it 'Falseを返す' do\r
686         r = @rp.store @imager\r
687         r.should be_false\r
688       end\r
689       it '更新されていない' do\r
690         r = @rp.store @imager\r
691         @rp.should be_a_new ResourcePicture\r
692       end\r
693       it '処理を中断してロールバックする' do\r
694         lambda {\r
695           r = @rp.store @imager\r
696         }.should_not change Picture, :count\r
697       end\r
698     end\r
699     context '新規実素材の取得に失敗したとき' do\r
700       before do\r
701         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(false)\r
702       end\r
703       it 'Falseを返す' do\r
704         r = @rp.store @imager\r
705         r.should be_false\r
706       end\r
707       it '更新されていない' do\r
708         r = @rp.store @imager\r
709         @rp.should be_a_new ResourcePicture\r
710       end\r
711       it '処理を中断してロールバックする' do\r
712         lambda {\r
713           r = @rp.store @imager\r
714         }.should_not change Picture, :count\r
715       end\r
716     end\r
717     context '自身の保存に失敗したとき' do\r
718       before do\r
719         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
720         ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)\r
721       end\r
722       it 'Falseを返す' do\r
723         r = @rp.store @imager\r
724         r.should be_false\r
725       end\r
726       it '更新されていない' do\r
727         r = @rp.store @imager\r
728         @rp.should be_a_new ResourcePicture\r
729       end\r
730       it '処理を中断してロールバックする' do\r
731         lambda {\r
732           r = @rp.store @imager\r
733         }.should_not change Picture, :count\r
734       end\r
735     end\r
736     context 'gif付き画像ファイルの作成・更新機能に失敗したとき' do\r
737       before do\r
738         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
739         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(false)\r
740       end\r
741       it 'Falseを返す' do\r
742         r = @rp.store @imager\r
743         r.should be_false\r
744       end\r
745       it '更新されていない' do\r
746         r = @rp.store @imager\r
747         @rp.should be_a_new ResourcePicture\r
748       end\r
749       it '処理を中断してロールバックする' do\r
750         lambda {\r
751           r = @rp.store @imager\r
752         }.should_not change Picture, :count\r
753       end\r
754     end\r
755   end\r
756   \r
757   describe 'gif付き画像ファイルの作成・更新に於いて' do\r
758     before do\r
759       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
760       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
761       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
762       @rp = FactoryGirl.build :resource_picture, attr\r
763       @imager = ImagerTest.load "abc\ndef\nghi"
764     end\r
765     context '事前チェック' do\r
766       before do\r
767         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
768         #それで外部のメソッド呼び出しだけに着目してテストする。\r
769         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
770         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
771         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
772         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
773       end\r
774       it '画像ファイルの作成・更新機能で画像を保存している' do\r
775         #二回目の保存はgif変換の結果を渡す。\r
776         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(2)\r
777         r = @rp.store_picture_with_gif @imager\r
778       end\r
779       it '自身にフォーマット変換対象かを問い合わせている' do\r
780         ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)\r
781         r = @rp.store_picture_with_gif @imager\r
782       end\r
783       it '画像ライブラリにGifフォーマット変換を依頼している' do\r
784         ImagerTest.any_instance.should_receive(:to_gif).with(any_args).exactly(1)\r
785         r = @rp.store_picture_with_gif @imager\r
786       end\r
787     end\r
788     context 'つつがなく終わるとき' do\r
789       before do\r
790         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
791         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
792         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
793         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
794         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
795       end\r
796       it 'Trueを返す' do\r
797         r = @rp.store_picture_with_gif @imager\r
798         r.should be_true\r
799       end\r
800     end\r
801     context 'gif変換なしで、つつがなく終わるとき' do\r
802       before do\r
803         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
804         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
805         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)\r
806         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
807         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
808       end\r
809       it 'Trueを返す' do\r
810         r = @rp.store_picture_with_gif @imager\r
811         r.should be_true\r
812       end\r
813       it 'gif保存は呼ばれていない' do\r
814         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
815         r = @rp.store_picture_with_gif @imager\r
816       end\r
817     end\r
818     #以下から例外ケース。処理先頭から失敗させていく\r
819     context '画像の保存に失敗したとき' do\r
820       before do\r
821         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)\r
822       end\r
823       it 'Falseを返す' do\r
824         r = @rp.store_picture_with_gif @imager\r
825         r.should be_false\r
826       end\r
827       it 'gif変換判定は呼ばれていない' do\r
828         ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)\r
829         r = @rp.store_picture_with_gif @imager\r
830       end\r
831     end\r
832     context 'gif変換に失敗したとき' do\r
833       before do\r
834         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
835         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
836         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(false)\r
837       end\r
838       it 'Falseを返す' do\r
839         r = @rp.store_picture_with_gif @imager\r
840         r.should be_false\r
841       end\r
842       it 'gif画像の保存は呼ばれていない' do\r
843         #本画像の保存があるので、一度は呼ばれる\r
844         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
845         r = @rp.store_picture_with_gif @imager\r
846       end\r
847     end\r
848     context 'gif画像の保存に失敗したとき' do\r
849       before do\r
850         ResourcePicture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true)\r
851         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
852         @gifimager = @imager.to_gif\r
853         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(@gifimager)\r
854         ResourcePicture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false)\r
855         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
856         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
857       end\r
858       it 'Falseを返す' do\r
859         r = @rp.store_picture_with_gif @imager\r
860         r.should be_false\r
861       end\r
862     end\r
863   end\r
864   \r
865   describe '画像ファイルの作成・更新に於いて' do\r
866     #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。\r
867     before do\r
868       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
869       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
870       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
871       @rp = FactoryGirl.build :resource_picture, attr\r
872       @rp.overwrite @op\r
873       @imager = ImagerTest.load "abc\ndef\nghi"
874     end\r
875     context '事前チェック' do\r
876       before do\r
877         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
878         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
879         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
880       end\r
881       it 'サムネイル化が1回呼ばれる' do\r
882         ImagerTest.any_instance.should_receive(:to_thumbnail).with(any_args).exactly(1)\r
883         r = @rp.store_picture(@imager, '1.png')\r
884       end\r
885       it '画像ファイルの保存が2回呼ばれる' do\r
886         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2)\r
887         r = @rp.store_picture(@imager, '1.gif')\r
888       end\r
889       it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do\r
890         PictureIO.resource_picture_io.should_receive(:put).with(@imager.to_thumbnail.binary, '1.gif').exactly(1)\r
891         r = @rp.store_picture(@imager, '1.gif')\r
892       end\r
893       it '画像ファイルのfullへの保存が1回呼ばれる' do\r
894         PictureIO.resource_picture_io.should_receive(:put).with(@imager.binary, '1.gif', 'full').exactly(1)\r
895         r = @rp.store_picture(@imager, '1.gif')\r
896       end\r
897     end\r
898     context 'つつがなく終わるとき' do\r
899       before do\r
900         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
901         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
902         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
903       end\r
904       it 'Trueを返す' do\r
905         r = @rp.store_picture(@imager, '1.gif')\r
906         r.should be_true\r
907       end\r
908     end\r
909     #以下から例外ケース。処理先頭から失敗させていく\r
910     context 'サムネイル化に失敗したとき' do\r
911       before do\r
912         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
913         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(false)\r
914       end\r
915       it 'Falseを返す' do\r
916         r = @rp.store_picture(@imager, '1.gif')\r
917         r.should be_false\r
918       end\r
919       it '画像ファイルの保存は呼ばれていない' do\r
920         PictureIO.resource_picture_io.should_not_receive(:put).with(any_args)\r
921         r = @rp.store_picture(@imager, '1.gif')\r
922       end\r
923     end\r
924     context 'サムネイル画像の保存に失敗したとき' do\r
925       before do\r
926         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
927         @tmbimager = @imager.to_thumbnail\r
928         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
929         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(false)\r
930       end\r
931       it 'Falseを返す' do\r
932         r = @rp.store_picture(@imager, '1.gif')\r
933         r.should be_false\r
934       end\r
935       it '画像ファイルの保存は呼ばれていない' do\r
936         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(1)\r
937         r = @rp.store_picture(@imager, '1.gif')\r
938       end\r
939     end\r
940     context '画像の保存に失敗したとき' do\r
941       before do\r
942         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
943         @tmbimager = @imager.to_thumbnail\r
944         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
945         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(true)\r
946         PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_return(false)\r
947       end\r
948       it 'Falseを返す' do\r
949         r = @rp.store_picture(@imager, '1.gif')\r
950         r.should be_false\r
951       end\r
952     end\r
953     \r
954   end\r
955   \r
956   describe 'クレジットデータに於いて' do\r
957     before do\r
958       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
959       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
960       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{"system_picture_id": 2}', :settings => {}.to_json.to_s}\r
961       @rp = FactoryGirl.build :resource_picture, attr\r
962     end\r
963     it 'system_picture_idが入っている' do\r
964       res = @rp.credit_data\r
965       res["system_picture_id"].should eq 2\r
966     end\r
967   end\r
968   \r
969 end\r
970 \r