OSDN Git Service

t#31018:add search om 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 = FactoryGirl.create :author, :user_id => @user.id\r
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id\r
11     @other_user = FactoryGirl.create( :user_yas)\r
12     @other_author = FactoryGirl.create :author, :user_id => @other_user.id\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\r
34       it '下限データが通る' do\r
35         @rp.ext = 'png' #リストにない拡張子は通らないし\r
36         @rp.width = 1\r
37         @rp.height = 1\r
38         @rp.filesize = 1\r
39         @rp.md5 = 'a'*32\r
40         @rp.classname = 'a'*1\r
41         @rp.should be_valid\r
42       end\r
43       it '上限データが通る' do\r
44         @rp.ext = 'jpeg'\r
45         @rp.width = 99999\r
46         @rp.height = 99999\r
47         @rp.filesize = 2000000\r
48         @rp.md5 = 'a'*32\r
49         @rp.classname = 'a'*50\r
50         @rp.should be_valid\r
51       end\r
52     end\r
53     \r
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\r
142         @rp.md5 = 'a'*31\r
143         @rp.should_not be_valid\r
144       end\r
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\r
215     it 'defined' do\r
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\r
219     end\r
220   end\r
221   \r
222   describe '上書き補充に於いて' do\r
223     before do\r
224       @original_picture.attributes = {:ext => 'gif', :width => 267, :height => 268, :filesize => 269, \r
225         :artist_id => @artist.id }\r
226       @rp = FactoryGirl.build :resource_picture, :original_picture_id => @original_picture.id\r
227     end\r
228     it 'width, height, ext, filesize, md5, original_picture_id, artist_idが設定されている' do\r
229       @rp.overwrite @original_picture\r
230       @rp.width.should eq 267\r
231       @rp.height.should eq 268\r
232       @rp.ext.should eq 'gif'\r
233       @rp.filesize.should eq 269\r
234       @rp.md5.should eq @rp.md5\r
235       @rp.original_picture_id.should eq @original_picture.id\r
236       @rp.artist_id.should eq @artist.id\r
237     end\r
238   end\r
239   \r
240   describe '所持判定に於いて' do\r
241     before do\r
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\r
245     context '事前チェックする' do\r
246       it '自身にロールリストからの絵師取得を依頼している' do\r
247         ResourcePicture.should_receive(:get_artist_from_roles).with(any_args).exactly(1)\r
248         r = @rp.own?([@author])\r
249       end\r
250     end\r
251     context 'ロール内絵師が取得できるとき' do\r
252       before do\r
253       end\r
254       it 'ロール内絵師のidが自身の絵師idと一致するなら許可する' do\r
255         ResourcePicture.stub(:get_artist_from_roles).with(any_args).and_return(@artist)\r
256         r = @rp.own?([@author])\r
257         r.should be_true\r
258       end\r
259       it 'ロール内絵師のidが自身の絵師idと一致しないならno' do\r
260         ResourcePicture.stub(:get_artist_from_roles).with(any_args).and_return(@other_artist)\r
261         @rp.own?(@other_artist).should be_false\r
262       end\r
263     end\r
264     context 'ロール内絵師が取得できないとき' do\r
265       before do\r
266         ResourcePicture.stub(:get_artist_from_roles).with(any_args).and_return(nil)\r
267       end\r
268       it 'Falseを返す' do\r
269         r = @rp.own?([@author])\r
270         r.should be_false\r
271       end\r
272     end\r
273   end\r
274   \r
275   describe '閲覧許可に於いて' do\r
276     before do\r
277       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
278       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
279     end\r
280     context 'オープンモードのとき' do\r
281       before do\r
282         MagicNumber['run_mode'] = 0\r
283       end\r
284       it '自身にゲスト用ロールチェックを問い合わせしている' do\r
285         ResourcePicture.any_instance.stub(:guest_role_check).and_return(true)\r
286         ResourcePicture.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)\r
287         r = @rp.visible?([@author])\r
288       end\r
289       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do\r
290         ResourcePicture.any_instance.stub(:guest_role_check).and_return(false)\r
291         r = @rp.visible?([@author])\r
292         r.should be_false\r
293       end\r
294     end\r
295     context 'クローズドモードのとき' do\r
296       before do\r
297         MagicNumber['run_mode'] = 1\r
298       end\r
299       it '自身に素材読者用ロールチェックを問い合わせしている' do\r
300         ResourcePicture.any_instance.stub(:resource_reader_role_check).and_return(true)\r
301         ResourcePicture.any_instance.should_receive(:resource_reader_role_check).with(any_args).exactly(1)\r
302         r = @rp.visible?([@author])\r
303       end\r
304       it '素材読者用ロールチェックが失敗したとき、falseを返す' do\r
305         ResourcePicture.any_instance.stub(:resource_reader_role_check).and_return(false)\r
306         r = @rp.visible?([@author])\r
307         r.should be_false\r
308       end\r
309     end\r
310     context 'つつがなく終わるとき' do\r
311       before do\r
312         MagicNumber['run_mode'] = 1\r
313         ResourcePicture.any_instance.stub(:resource_reader_role_check).and_return(true)\r
314       end\r
315       it '許可する' do\r
316         r = @rp.visible?([@author])\r
317         r.should be_true\r
318       end\r
319     end\r
320   end\r
321   \r
322   describe 'ファイル名に於いて' do\r
323     before do\r
324       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
325       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
326     end\r
327     it 'id+拡張子のフォーマットで返す' do\r
328       r = @rp.filename\r
329       r.should eq "#{@rp.id}.png"\r
330     end\r
331   end\r
332   \r
333   describe 'MimeTypeに於いて' do\r
334     before do\r
335       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
336       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
337     end\r
338     it 'image/拡張子のフォーマットで返す' do\r
339       r = @rp.mime_type\r
340       r.should eq "image/png"\r
341     end\r
342   end\r
343   \r
344   describe 'ファイルのurlに於いて' do\r
345     before do\r
346       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
347       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
348       ResourcePicture.any_instance.stub(:filename).and_return('3.gif')\r
349     end\r
350     it 'ファイル名取得を依頼している' do\r
351       ResourcePicture.any_instance.should_receive(:filename).exactly(1)\r
352       @rp.url\r
353     end\r
354     it '/resource_pictures/3.gifのフォーマットで返す' do\r
355       r = @rp.url\r
356       r.should eq "/resource_pictures/3.gif"\r
357     end\r
358   end\r
359   \r
360   describe '一覧取得に於いて' do\r
361     before do\r
362       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
363       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
364       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
365     end\r
366     context 'page補正について' do\r
367       it '文字列から数値に変換される' do\r
368         ResourcePicture.page('8').should eq 8\r
369       end\r
370       it 'nilの場合は1になる' do\r
371         ResourcePicture.page().should eq 1\r
372       end\r
373       it '0以下の場合は1になる' do\r
374         ResourcePicture.page('0').should eq 1\r
375       end\r
376     end\r
377     context 'page_size補正について' do\r
378       it '文字列から数値に変換される' do\r
379         ResourcePicture.page_size('7').should eq 7\r
380       end\r
381       it 'nilの場合はResourcePicture.default_page_sizeになる' do\r
382         ResourcePicture.page_size().should eq ResourcePicture.default_page_size\r
383       end\r
384       it '0以下の場合はResourcePicture.default_page_sizeになる' do\r
385         ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size\r
386       end\r
387       it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do\r
388         ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size\r
389       end\r
390     end\r
391     it 'リストを返す' do\r
392       r = ResourcePicture.list\r
393       r.should eq [@rp]\r
394     end\r
395     it '時系列で並んでいる' do\r
396       nop = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
397       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
398       r = ResourcePicture.list\r
399       r.should eq [nrp, @rp]\r
400     end\r
401     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
402       before do\r
403         nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
404         @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
405         nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
406         @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
407         nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
408         @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
409         nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
410         @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
411         ResourcePicture.stub(:default_page_size).and_return(2)\r
412       end\r
413       it '通常は2件を返す' do\r
414         r = ResourcePicture.list\r
415         r.should have(2).items \r
416       end\r
417       it 'page=1なら末尾2件を返す' do\r
418         #時系列で並んでいる\r
419         r = ResourcePicture.list(1)\r
420         r.should eq [@nrp5, @nrp4]\r
421       end\r
422       it 'page=2なら中間2件を返す' do\r
423         r = ResourcePicture.list(2)\r
424         r.should eq [@nrp3, @nrp2]\r
425       end\r
426       it 'page=3なら先頭1件を返す' do\r
427         r = ResourcePicture.list(3)\r
428         r.should eq [@rp]\r
429       end\r
430     end\r
431     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
432       before do\r
433         nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
434         @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
435         nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
436         @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
437         nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
438         @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
439         nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
440         @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
441         ResourcePicture.stub(:default_page_size).and_return(2)\r
442       end\r
443       it '件数0は全件(5件)を返す' do\r
444         r = ResourcePicture.list 5, 0\r
445         r.should have(5).items \r
446       end\r
447     end\r
448   end\r
449   describe '一覧取得オプションに於いて' do\r
450     it 'includeキーを含んでいる' do\r
451       r = ResourcePicture.list_opt\r
452       r.has_key?(:include).should be_true\r
453     end\r
454     it '3つの項目を含んでいる' do\r
455       r = ResourcePicture.list_opt[:include]\r
456       r.should have(3).items\r
457     end\r
458     it 'ライセンスを含んでいる' do\r
459       r = ResourcePicture.list_opt[:include]\r
460       r.has_key?(:license).should be_true\r
461     end\r
462     it '絵師を含んでいる' do\r
463       r = ResourcePicture.list_opt[:include]\r
464       r.has_key?(:artist).should be_true\r
465     end\r
466     it '実素材を含んでいる' do\r
467       r = ResourcePicture.list_opt[:include]\r
468       r.has_key?(:picture).should be_true\r
469     end\r
470   end\r
471   describe 'json一覧出力オプションに於いて' do\r
472     before do\r
473       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
474       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
475       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
476       @sbt = FactoryGirl.create :speech_balloon_template\r
477       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1\r
478       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
479       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
480     end\r
481     it 'ライセンスを含んでいる' do\r
482       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
483       j = JSON.parse r\r
484       i = j.first\r
485       i.has_key?('license').should be_true\r
486     end\r
487     it '絵師を含んでいる' do\r
488       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
489       j = JSON.parse r\r
490       i = j.first\r
491       i.has_key?('artist').should be_true\r
492     end\r
493     it '実素材を含んでいる' do\r
494       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
495       j = JSON.parse r\r
496       i = j.first\r
497       i.has_key?('picture').should be_true\r
498     end\r
499   end\r
500   \r
501   describe '自分の素材一覧取得に於いて' do\r
502     before do\r
503       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
504       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
505       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
506     end\r
507     context 'つつがなく終わるとき' do\r
508       it '一覧取得オプションを利用している' do\r
509         ResourcePicture.stub(:list_opt).with(any_args).and_return({})\r
510         ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1)\r
511         r = ResourcePicture.mylist @artist\r
512       end\r
513     end\r
514     it 'リストを返す' do\r
515       c = ResourcePicture.mylist @artist\r
516       c.should eq [@rp]\r
517     end\r
518     it '時系列で並んでいる' do\r
519       nop = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
520       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
521       cl = ResourcePicture.mylist @artist\r
522       cl.should eq [nrp, @rp]\r
523     end\r
524     it '他人の素材は含まない' do\r
525       nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
526       nrp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
527       cl = ResourcePicture.mylist @artist\r
528       cl.should eq [@rp]\r
529     end\r
530     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
531       before do\r
532         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
533         @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
534         @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
535         @rp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
536         @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
537         @rp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
538         @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
539         @rp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
540       end\r
541       it '通常は2件を返す' do\r
542         c = ResourcePicture.mylist @artist, 1, 2\r
543         c.should have(2).items \r
544       end\r
545       it 'page=1なら末尾2件を返す' do\r
546         #時系列で並んでいる\r
547         c = ResourcePicture.mylist(@artist, 1, 2)\r
548         c.should eq [@rp5, @rp4]\r
549       end\r
550       it 'page=2なら中間2件を返す' do\r
551         c = ResourcePicture.mylist(@artist, 2, 2)\r
552         c.should eq [@rp3, @rp2]\r
553       end\r
554       it 'page=3なら先頭1件を返す' do\r
555         c = ResourcePicture.mylist(@artist, 3, 2)\r
556         c.should eq [@rp]\r
557       end\r
558     end\r
559     context 'DBに5件あって1ページの件数を0件に変えたとして' do\r
560       before do\r
561         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
562         @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
563         @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
564         @rp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
565         @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
566         @rp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
567         @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
568         @rp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
569         Author.stub(:default_resource_picture_page_size).and_return(2)\r
570       end\r
571       it '通常は全件(5件)を返す' do\r
572         r = ResourcePicture.mylist @artist, 5, 0\r
573         r.should have(5).items \r
574       end\r
575     end\r
576   end\r
577   \r
578   describe '単体取得に於いて' do\r
579     before do\r
580       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
581       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
582       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
583     end\r
584     context 'つつがなく終わるとき' do\r
585       it '単体取得オプションを利用している' do\r
586         ResourcePicture.stub(:show_opt).with(any_args).and_return({})\r
587         ResourcePicture.should_receive(:show_opt).with(any_args).exactly(1)\r
588         r = ResourcePicture.show @rp.id, @author\r
589       end\r
590       it '閲覧許可を問い合わせている' do\r
591         ResourcePicture.any_instance.stub(:visible?).with(any_args).and_return(true)\r
592         ResourcePicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
593         r = ResourcePicture.show @rp.id, @author\r
594       end\r
595     end\r
596     it '指定の素材を返す' do\r
597       r = ResourcePicture.show @rp.id, @author\r
598       r.should eq @rp\r
599     end\r
600     context '他人の素材を開こうとしたとき' do\r
601       it '403Forbidden例外を返す' do\r
602         ResourcePicture.any_instance.stub(:visible?).and_return(false)\r
603         lambda{\r
604           r = ResourcePicture.show @rp.id, @other_author\r
605         }.should raise_error(ActiveRecord::Forbidden)\r
606       end\r
607     end\r
608     context '存在しない素材を開こうとしたとき' do\r
609       it '404RecordNotFound例外を返す' do\r
610         lambda{\r
611           r = ResourcePicture.show 0, @author\r
612         }.should raise_error(ActiveRecord::RecordNotFound)\r
613       end\r
614     end\r
615   end\r
616   describe '単体取得オプションに於いて' do\r
617     it 'includeキーを含んでいる' do\r
618       r = ResourcePicture.show_opt\r
619       r.has_key?(:include).should be_true\r
620     end\r
621     it '3つの項目を含んでいる' do\r
622       r = ResourcePicture.show_opt[:include]\r
623       r.should have(3).items\r
624     end\r
625     it 'ライセンスを含んでいる' do\r
626       r = ResourcePicture.show_opt[:include]\r
627       r.has_key?(:license).should be_true\r
628     end\r
629     it '絵師を含んでいる' do\r
630       r = ResourcePicture.show_opt[:include]\r
631       r.has_key?(:artist).should be_true\r
632     end\r
633     it '実素材を含んでいる' do\r
634       r = ResourcePicture.show_opt[:include]\r
635       r.has_key?(:picture).should be_true\r
636     end\r
637   end\r
638   describe 'json単体出力オプションに於いて' do\r
639     before do\r
640       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
641       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
642       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
643       @sbt = FactoryGirl.create :speech_balloon_template\r
644       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1\r
645       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
646       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
647     end\r
648     it 'ライセンスを含んでいる' do\r
649       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt\r
650       j = JSON.parse r\r
651       i = j\r
652       i.has_key?('license').should be_true\r
653     end\r
654     it '絵師を含んでいる' do\r
655       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt\r
656       j = JSON.parse r\r
657       i = j\r
658       i.has_key?('artist').should be_true\r
659     end\r
660     it '実素材を含んでいる' do\r
661       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt\r
662       j = JSON.parse r\r
663       i = j\r
664       i.has_key?('picture').should be_true\r
665     end\r
666   end\r
667   \r
668   describe 'フォーマット変換対象判定に於いて' do\r
669     before do\r
670       @rp = FactoryGirl.build :resource_picture, \r
671         :artist_id => @artist.id, :license_id => @license.id\r
672     end\r
673     context '変換するケース' do\r
674       it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do\r
675         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
676         @rp.ext = 'png'\r
677         @rp.to_gif?.should be_true\r
678       end\r
679     end\r
680     context '変換しないケース' do\r
681       it '画像フォーマットがでない' do\r
682         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
683         @rp.ext = 'gif'\r
684         @rp.to_gif?.should be_false\r
685       end\r
686       it '変換禁止フラグが無効' do\r
687         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)\r
688         @rp.ext = 'png'\r
689         @rp.to_gif?.should be_false\r
690       end\r
691     end\r
692   end\r
693   \r
694   describe '新規実素材の取得に於いて' do\r
695     before do\r
696       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
697       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
698       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', \r
699         :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s}\r
700       @rp = FactoryGirl.build :resource_picture, attr\r
701       @imager = ImagerTest.load "abc\ndef\nghi"\r
702     end\r
703     context '事前チェック' do\r
704       before do\r
705         Picture.any_instance.stub(:store).with(any_args).and_return(true)\r
706       end\r
707       it '実素材モデルにデフォルト値補充を依頼している' do\r
708         Picture.any_instance.should_receive(:supply_default).with(any_args).exactly(1)\r
709         r = @rp.new_picture @imager\r
710       end\r
711       it '実素材モデルに上書き補充を依頼している' do\r
712         Picture.any_instance.should_receive(:overwrite).with(any_args).exactly(1)\r
713         r = @rp.new_picture @imager\r
714       end\r
715       it '実素材を保存している' do\r
716         Picture.any_instance.should_receive(:store).with(any_args).exactly(1)\r
717         r = @rp.new_picture @imager\r
718       end\r
719     end\r
720     context 'つつがなく終わるとき' do\r
721       it '保存された実素材を返す' do\r
722         r = @rp.new_picture @imager\r
723         r.is_a?(Picture).should be_true\r
724       end\r
725     end\r
726     #以下から例外ケース。処理先頭から失敗させていく\r
727     context '実素材の保存に失敗したとき' do\r
728       before do\r
729         Picture.any_instance.stub(:store).with(any_args).and_return(false)\r
730       end\r
731       it 'Falseを返す' do\r
732         r = @rp.new_picture @imager\r
733         r.should be_false\r
734       end\r
735       it '自身の全体エラーメッセージにその旨をセットしている' do\r
736         r = @rp.new_picture @imager\r
737         @rp.errors[:base].should_not be_empty\r
738       end\r
739     end\r
740   end\r
741   \r
742   describe '作成・更新に於いて' do\r
743     before do\r
744       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
745       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
746       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
747       @rp = FactoryGirl.build :resource_picture, attr\r
748       @imager = ImagerTest.load "abc\ndef\nghi"\r
749     end\r
750     context '事前チェック' do\r
751       before do\r
752         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
753         #それで外部のメソッド呼び出しだけに着目してテストする。\r
754         OriginalPicture.any_instance.stub(:save).and_return(true)\r
755         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
756         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)\r
757       end\r
758       it '原画に保存を依頼している' do\r
759         OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
760         r = @rp.store @imager\r
761       end\r
762       it '新規実素材の取得を依頼している' do\r
763         ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1)\r
764         r = @rp.store @imager\r
765       end\r
766       it '自身を保存している' do\r
767         ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)\r
768         r = @rp.store @imager\r
769       end\r
770       it 'gif付き画像ファイルの作成・更新機能で画像を保存している' do\r
771         ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@imager).exactly(1)\r
772         r = @rp.store @imager\r
773       end\r
774     end\r
775     context 'つつがなく終わるとき' do\r
776       before do\r
777         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
778         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
779         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(@imager).and_return(true)\r
780       end\r
781       it 'Trueを返す' do\r
782         r = @rp.store @imager\r
783         r.should be_true\r
784       end\r
785       it '原画の停止日時をクリアする' do\r
786         r = @rp.store @imager\r
787         @rp.original_picture.stopped_at.should be_nil\r
788       end\r
789       it '原画の公開日時に現在時刻をセットする' do\r
790         lambda {\r
791           r = @rp.store @imager\r
792         }.should change(@rp.original_picture, :published_at)\r
793       end\r
794       it '実素材idから最新画像idを取得してセットしている' do\r
795         r = @rp.store @imager\r
796         @rp.picture_id.should eq @p.id\r
797       end\r
798       it '自身が保存されている' do\r
799         lambda {\r
800           r = @rp.store @imager\r
801         }.should change ResourcePicture, :count\r
802       end\r
803     end\r
804     #以下から例外ケース。処理先頭から失敗させていく\r
805     context '原画の保存に失敗したとき' do\r
806       before do\r
807         OriginalPicture.any_instance.stub(:save).and_return(false)\r
808       end\r
809       it 'Falseを返す' do\r
810         r = @rp.unpublish\r
811         r.should be_false\r
812       end\r
813       it 'ロールバックしている' do\r
814         lambda {\r
815           r = @rp.unpublish\r
816         }.should_not change(ResourcePicture, :count)\r
817       end\r
818     end\r
819     context '画像オブジェクトの取得に失敗したとき' do\r
820       before do\r
821         @imager = false\r
822       end\r
823       it 'Falseを返す' do\r
824         r = @rp.store @imager\r
825         r.should be_false\r
826       end\r
827       it '更新されていない' do\r
828         r = @rp.store @imager\r
829         @rp.should be_a_new ResourcePicture\r
830       end\r
831       it '処理を中断してロールバックする' do\r
832         lambda {\r
833           r = @rp.store @imager\r
834         }.should_not change Picture, :count\r
835       end\r
836     end\r
837     context '新規実素材の取得に失敗したとき' do\r
838       before do\r
839         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(false)\r
840       end\r
841       it 'Falseを返す' do\r
842         r = @rp.store @imager\r
843         r.should be_false\r
844       end\r
845       it '更新されていない' do\r
846         r = @rp.store @imager\r
847         @rp.should be_a_new ResourcePicture\r
848       end\r
849       it '処理を中断してロールバックする' do\r
850         lambda {\r
851           r = @rp.store @imager\r
852         }.should_not change Picture, :count\r
853       end\r
854     end\r
855     context '自身の保存に失敗したとき' do\r
856       before do\r
857         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
858         ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)\r
859       end\r
860       it 'Falseを返す' do\r
861         r = @rp.store @imager\r
862         r.should be_false\r
863       end\r
864       it '更新されていない' do\r
865         r = @rp.store @imager\r
866         @rp.should be_a_new ResourcePicture\r
867       end\r
868       it '処理を中断してロールバックする' do\r
869         lambda {\r
870           r = @rp.store @imager\r
871         }.should_not change Picture, :count\r
872       end\r
873     end\r
874     context 'gif付き画像ファイルの作成・更新機能に失敗したとき' do\r
875       before do\r
876         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
877         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(false)\r
878       end\r
879       it 'Falseを返す' do\r
880         r = @rp.store @imager\r
881         r.should be_false\r
882       end\r
883       it '更新されていない' do\r
884         r = @rp.store @imager\r
885         @rp.should be_a_new ResourcePicture\r
886       end\r
887       it '処理を中断してロールバックする' do\r
888         lambda {\r
889           r = @rp.store @imager\r
890         }.should_not change Picture, :count\r
891       end\r
892     end\r
893   end\r
894   \r
895   describe 'gif付き画像ファイルの作成・更新に於いて' do\r
896     before do\r
897       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
898       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
899       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
900       @rp = FactoryGirl.build :resource_picture, attr\r
901       @imager = ImagerTest.load "abc\ndef\nghi"\r
902     end\r
903     context '事前チェック' do\r
904       before do\r
905         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
906         #それで外部のメソッド呼び出しだけに着目してテストする。\r
907         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
908         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
909         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
910         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
911       end\r
912       it '画像ファイルの作成・更新機能で画像を保存している' do\r
913         #二回目の保存はgif変換の結果を渡す。\r
914         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(2)\r
915         r = @rp.store_picture_with_gif @imager\r
916       end\r
917       it '自身にフォーマット変換対象かを問い合わせている' do\r
918         ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)\r
919         r = @rp.store_picture_with_gif @imager\r
920       end\r
921       it '画像ライブラリにGifフォーマット変換を依頼している' do\r
922         ImagerTest.any_instance.should_receive(:to_gif).with(any_args).exactly(1)\r
923         r = @rp.store_picture_with_gif @imager\r
924       end\r
925     end\r
926     context 'つつがなく終わるとき' do\r
927       before do\r
928         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
929         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
930         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
931         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
932         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
933       end\r
934       it 'Trueを返す' do\r
935         r = @rp.store_picture_with_gif @imager\r
936         r.should be_true\r
937       end\r
938     end\r
939     context 'gif変換なしで、つつがなく終わるとき' do\r
940       before do\r
941         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
942         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
943         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)\r
944         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
945         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
946       end\r
947       it 'Trueを返す' do\r
948         r = @rp.store_picture_with_gif @imager\r
949         r.should be_true\r
950       end\r
951       it 'gif保存は呼ばれていない' do\r
952         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
953         r = @rp.store_picture_with_gif @imager\r
954       end\r
955     end\r
956     #以下から例外ケース。処理先頭から失敗させていく\r
957     context '画像の保存に失敗したとき' do\r
958       before do\r
959         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)\r
960       end\r
961       it 'Falseを返す' do\r
962         r = @rp.store_picture_with_gif @imager\r
963         r.should be_false\r
964       end\r
965       it '全体エラーメッセージがセットされている' do\r
966         lambda {\r
967           r = @rp.store_picture_with_gif @imager\r
968         }.should change(@rp.errors[:base], :count)\r
969       end\r
970       it 'gif変換判定は呼ばれていない' do\r
971         ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)\r
972         r = @rp.store_picture_with_gif @imager\r
973       end\r
974     end\r
975     context 'gif変換に失敗したとき' do\r
976       before do\r
977         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
978         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
979         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(false)\r
980       end\r
981       it 'Falseを返す' do\r
982         r = @rp.store_picture_with_gif @imager\r
983         r.should be_false\r
984       end\r
985       it '全体エラーメッセージがセットされている' do\r
986         lambda {\r
987           r = @rp.store_picture_with_gif @imager\r
988         }.should change(@rp.errors[:base], :count)\r
989       end\r
990       it 'gif画像の保存は呼ばれていない' do\r
991         #本画像の保存があるので、一度は呼ばれる\r
992         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
993         r = @rp.store_picture_with_gif @imager\r
994       end\r
995     end\r
996     context 'gif画像の保存に失敗したとき' do\r
997       before do\r
998         ResourcePicture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true)\r
999         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
1000         @gifimager = @imager.to_gif\r
1001         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(@gifimager)\r
1002         ResourcePicture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false)\r
1003         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
1004         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
1005       end\r
1006       it 'Falseを返す' do\r
1007         r = @rp.store_picture_with_gif @imager\r
1008         r.should be_false\r
1009       end\r
1010       it '全体エラーメッセージがセットされている' do\r
1011         lambda {\r
1012           r = @rp.store_picture_with_gif @imager\r
1013         }.should change(@rp.errors[:base], :count)\r
1014       end\r
1015     end\r
1016   end\r
1017   \r
1018   describe '画像ファイルの作成・更新に於いて' do\r
1019     #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。\r
1020     before do\r
1021       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1022       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1023       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
1024       @rp = FactoryGirl.build :resource_picture, attr\r
1025       @rp.overwrite @op\r
1026       @imager = ImagerTest.load "abc\ndef\nghi"\r
1027     end\r
1028     context '事前チェック' do\r
1029       before do\r
1030         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
1031         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
1032         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1033       end\r
1034       it 'サムネイル化が1回呼ばれる' do\r
1035         ImagerTest.any_instance.should_receive(:to_thumbnail).with(any_args).exactly(1)\r
1036         r = @rp.store_picture(@imager, '1.png')\r
1037       end\r
1038       it '画像ファイルの保存が2回呼ばれる' do\r
1039         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2)\r
1040         r = @rp.store_picture(@imager, '1.gif')\r
1041       end\r
1042       it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do\r
1043         PictureIO.resource_picture_io.should_receive(:put).with(@imager.to_thumbnail.binary, '1.gif').exactly(1)\r
1044         r = @rp.store_picture(@imager, '1.gif')\r
1045       end\r
1046       it '画像ファイルのfullへの保存が1回呼ばれる' do\r
1047         PictureIO.resource_picture_io.should_receive(:put).with(@imager.binary, '1.gif', 'full').exactly(1)\r
1048         r = @rp.store_picture(@imager, '1.gif')\r
1049       end\r
1050     end\r
1051     context 'つつがなく終わるとき' do\r
1052       before do\r
1053         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
1054         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
1055         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1056       end\r
1057       it 'Trueを返す' do\r
1058         r = @rp.store_picture(@imager, '1.gif')\r
1059         r.should be_true\r
1060       end\r
1061     end\r
1062     #以下から例外ケース。処理先頭から失敗させていく\r
1063     context 'サムネイル化に失敗したとき' do\r
1064       before do\r
1065         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1066         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(false)\r
1067       end\r
1068       it 'Falseを返す' do\r
1069         r = @rp.store_picture(@imager, '1.gif')\r
1070         r.should be_false\r
1071       end\r
1072       it '画像ファイルの保存は呼ばれていない' do\r
1073         PictureIO.resource_picture_io.should_not_receive(:put).with(any_args)\r
1074         r = @rp.store_picture(@imager, '1.gif')\r
1075       end\r
1076     end\r
1077     context 'サムネイル画像の保存に失敗したとき' do\r
1078       before do\r
1079         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1080         @tmbimager = @imager.to_thumbnail\r
1081         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
1082         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_raise(PictureIO::Error)\r
1083       end\r
1084       it 'Falseを返す' do\r
1085         r = @rp.store_picture(@imager, '1.gif')\r
1086         r.should be_false\r
1087       end\r
1088       it '画像ファイルの保存は呼ばれていない' do\r
1089         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(1)\r
1090         r = @rp.store_picture(@imager, '1.gif')\r
1091       end\r
1092     end\r
1093     context '画像の保存に失敗したとき' do\r
1094       before do\r
1095         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1096         @tmbimager = @imager.to_thumbnail\r
1097         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
1098         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(true)\r
1099         PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_raise(PictureIO::Error)\r
1100       end\r
1101       it 'Falseを返す' do\r
1102         r = @rp.store_picture(@imager, '1.gif')\r
1103         r.should be_false\r
1104       end\r
1105     end\r
1106     \r
1107   end\r
1108   \r
1109   describe '公開停止に於いて' do\r
1110     before do\r
1111       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1112       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1113       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
1114       PictureIO.resource_picture_io.stub(:exist?).with(@rp.filename).and_return(true)\r
1115       PictureIO.resource_picture_io.stub(:exist?).with(@rp.filename, 'full').and_return(true)\r
1116     end\r
1117     context '事前チェックしておく' do\r
1118       before do\r
1119         OriginalPicture.any_instance.stub(:save).and_return(true)\r
1120         ResourcePicture.any_instance.stub(:destroy).and_return(true)\r
1121         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true)\r
1122         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true)\r
1123       end\r
1124       it '原画に保存を依頼している' do\r
1125         OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
1126         r = @rp.unpublish\r
1127       end\r
1128       it '素材モデルに削除を依頼している' do\r
1129         ResourcePicture.any_instance.should_receive(:destroy).exactly(1)\r
1130         r = @rp.unpublish\r
1131       end\r
1132       it '保管庫にサムネイルの画像データ削除を依頼している' do\r
1133         PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename).exactly(1)\r
1134         r = @rp.unpublish\r
1135       end\r
1136       it '保管庫にフルサイズの画像データ削除を依頼している' do\r
1137         PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename, 'full').exactly(1)\r
1138         r = @rp.unpublish\r
1139       end\r
1140     end\r
1141     context 'つつがなく終わるとき' do\r
1142       it '自身を削除する' do\r
1143         lambda {\r
1144           r = @rp.unpublish\r
1145         }.should change(ResourcePicture, :count).by(-1)\r
1146         lambda {\r
1147           r = ResourcePicture.find @rp.id\r
1148         }.should raise_error\r
1149       end\r
1150       it '原画の公開日時をクリアする' do\r
1151         r = @rp.store @imager\r
1152         @rp.original_picture.published_at.should be_nil\r
1153       end\r
1154       it '原画の停止日時に現在時刻をセットする' do\r
1155         lambda {\r
1156           r = @rp.unpublish\r
1157         }.should change(@rp.original_picture, :stopped_at)\r
1158       end\r
1159       it 'Trueを返す' do\r
1160         r = @rp.unpublish\r
1161         r.should be_true\r
1162       end\r
1163     end\r
1164     context '原画の保存に失敗したとき' do\r
1165       before do\r
1166         OriginalPicture.any_instance.stub(:save).and_return(false)\r
1167       end\r
1168       it 'Falseを返す' do\r
1169         r = @rp.unpublish\r
1170         r.should be_false\r
1171       end\r
1172       it 'ロールバックしている' do\r
1173         lambda {\r
1174           r = @rp.unpublish\r
1175         }.should_not change(ResourcePicture, :count)\r
1176       end\r
1177     end\r
1178     context '自身の削除に失敗したとき' do\r
1179       before do\r
1180         ResourcePicture.any_instance.stub(:destroy).with(any_args).and_return(false)\r
1181       end\r
1182       it 'Falseを返す' do\r
1183         r = @rp.unpublish\r
1184         r.should be_false\r
1185       end\r
1186       it 'ロールバックしている' do\r
1187         lambda {\r
1188           r = @rp.unpublish\r
1189         }.should_not change(ResourcePicture, :count)\r
1190       end\r
1191     end\r
1192     context 'サムネイル画像の削除に失敗したとき' do\r
1193       before do\r
1194         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_raise(PictureIO::Error)\r
1195         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true)\r
1196       end\r
1197       it 'Falseを返す' do\r
1198         r = @rp.unpublish\r
1199         r.should be_false\r
1200       end\r
1201       it 'ロールバックしている' do\r
1202         lambda {\r
1203           r = @rp.unpublish\r
1204         }.should_not change(ResourcePicture, :count)\r
1205       end\r
1206     end\r
1207     context 'フルサイズ画像の削除に失敗したとき' do\r
1208       before do\r
1209         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true)\r
1210         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_raise(PictureIO::Error)\r
1211       end\r
1212       it 'Falseを返す' do\r
1213         r = @rp.unpublish\r
1214         r.should be_false\r
1215       end\r
1216       it 'ロールバックしている' do\r
1217         lambda {\r
1218           r = @rp.unpublish\r
1219         }.should_not change(ResourcePicture, :count)\r
1220       end\r
1221     end\r
1222   end\r
1223   \r
1224   describe 'クレジットデータに於いて' do\r
1225     before do\r
1226       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1227       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1228       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{"system_picture_id": 2}', :settings => {}.to_json.to_s}\r
1229       @rp = FactoryGirl.build :resource_picture, attr\r
1230     end\r
1231     it 'system_picture_idが入っている' do\r
1232       res = @rp.credit_data\r
1233       res["system_picture_id"].should eq 2\r
1234     end\r
1235   end\r
1236   \r
1237 end\r
1238 \r