OSDN Git Service

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