OSDN Git Service

t#31470:create pager
[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   end\r
476   \r
477   describe '自分の素材一覧取得に於いて' do\r
478     before do\r
479       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
480       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
481       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
482     end\r
483     context 'つつがなく終わるとき' do\r
484       it '一覧取得オプションを利用している' do\r
485         ResourcePicture.stub(:list_opt).with(any_args).and_return({})\r
486         ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1)\r
487         r = ResourcePicture.mylist @artist\r
488       end\r
489     end\r
490     it 'リストを返す' do\r
491       c = ResourcePicture.mylist @artist\r
492       c.should eq [@rp]\r
493     end\r
494     it '時系列で並んでいる' do\r
495       nop = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
496       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
497       cl = ResourcePicture.mylist @artist\r
498       cl.should eq [nrp, @rp]\r
499     end\r
500     it '他人の素材は含まない' do\r
501       nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
502       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
503       cl = ResourcePicture.mylist @artist\r
504       cl.should eq [@rp]\r
505     end\r
506     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
507       before do\r
508         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
509         @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
510         @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
511         @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
512         @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
513         @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
514         @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
515         @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
516       end\r
517       it '通常は2件を返す' do\r
518         c = ResourcePicture.mylist @artist, 1, 2\r
519         c.should have(2).items \r
520       end\r
521       it 'page=1なら末尾2件を返す' do\r
522         #時系列で並んでいる\r
523         c = ResourcePicture.mylist(@artist, 1, 2)\r
524         c.should eq [@rp5, @rp4]\r
525       end\r
526       it 'page=2なら中間2件を返す' do\r
527         c = ResourcePicture.mylist(@artist, 2, 2)\r
528         c.should eq [@rp3, @rp2]\r
529       end\r
530       it 'page=3なら先頭1件を返す' do\r
531         c = ResourcePicture.mylist(@artist, 3, 2)\r
532         c.should eq [@rp]\r
533       end\r
534     end\r
535   end\r
536   \r
537   describe '他作家の素材一覧取得に於いて' do\r
538     before do\r
539       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
540       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
541       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
542       @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
543       @other_p = FactoryGirl.create :picture, :original_picture_id => @other_op.id, :license_id => @license.id, :artist_id => @other_artist.id\r
544       @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @other_op.id, :picture_id => @other_p.id\r
545     end\r
546     context 'つつがなく終わるとき' do\r
547       it '一覧取得オプションを利用している' do\r
548         ResourcePicture.stub(:list_opt).with(any_args).and_return({})\r
549         ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1)\r
550         r = ResourcePicture.himlist @other_artist\r
551       end\r
552     end\r
553     it '指定した作家のリストを返す' do\r
554       r = ResourcePicture.himlist @other_artist\r
555       r.should eq [@other_rp]\r
556     end\r
557     it '時系列で並んでいる' do\r
558       nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
559       nrp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @other_p.id, :updated_at => Time.now + 100\r
560       r = ResourcePicture.himlist @other_artist\r
561       r.should eq [nrp, @other_rp]\r
562     end\r
563     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
564       before do\r
565         @op2 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
566         @rp2 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
567         @op3 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
568         @rp3 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
569         @op4 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
570         @rp4 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
571         @op5 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
572         @rp5 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
573       end\r
574       it '通常は2件を返す' do\r
575         r = ResourcePicture.himlist @other_artist, 1, 2\r
576         r.should have(2).items \r
577       end\r
578       it 'page=1なら末尾2件を返す' do\r
579         #時系列で並んでいる\r
580         r = ResourcePicture.himlist(@other_artist, 1, 2)\r
581         r.should eq [@rp5, @rp4]\r
582       end\r
583       it 'page=2なら中間2件を返す' do\r
584         r = ResourcePicture.himlist(@other_artist, 2, 2)\r
585         r.should eq [@rp3, @rp2]\r
586       end\r
587       it 'page=3なら先頭1件を返す' do\r
588         r = ResourcePicture.himlist(@other_artist, 3, 2)\r
589         r.should eq [@other_rp]\r
590       end\r
591     end\r
592   end\r
593   \r
594   describe '一覧ページ制御に於いて' do\r
595     before do\r
596       ResourcePicture.stub(:count).with(any_args).and_return(100)\r
597     end\r
598     it 'ページ制御を返す' do\r
599       r = ResourcePicture.list_paginate\r
600       r.is_a?(Kaminari::PaginatableArray).should be_true\r
601     end\r
602     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do\r
603       r = ResourcePicture.list_paginate 3, 10\r
604       r.limit_value.should eq 10\r
605       r.offset_value.should eq 20\r
606     end\r
607   end\r
608   \r
609   describe '自分の素材一覧ページ制御に於いて' do\r
610     before do\r
611       ResourcePicture.stub(:count).with(any_args).and_return(100)\r
612     end\r
613     it 'ページ制御を返す' do\r
614       r = ResourcePicture.mylist_paginate @artist\r
615       r.is_a?(Kaminari::PaginatableArray).should be_true\r
616     end\r
617     it '自分の素材一覧の取得条件を利用している' do\r
618       ResourcePicture.stub(:mylist_where).with(any_args).and_return('')\r
619       ResourcePicture.should_receive(:mylist_where).with(any_args).exactly(1)\r
620       r = ResourcePicture.mylist_paginate @artist\r
621     end\r
622     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do\r
623       r = ResourcePicture.mylist_paginate @artist, 3, 10\r
624       r.limit_value.should eq 10\r
625       r.offset_value.should eq 20\r
626     end\r
627   end\r
628   \r
629   describe '他作家の素材一覧ページ制御に於いて' do\r
630     before do\r
631       ResourcePicture.stub(:count).with(any_args).and_return(100)\r
632     end\r
633     it 'ページ制御を返す' do\r
634       r = ResourcePicture.himlist_paginate @other_artist\r
635       r.is_a?(Kaminari::PaginatableArray).should be_true\r
636     end\r
637     it '他作家の素材一覧の取得条件を利用している' do\r
638       ResourcePicture.stub(:himlist_where).with(any_args).and_return('')\r
639       ResourcePicture.should_receive(:himlist_where).with(any_args).exactly(1)\r
640       r = ResourcePicture.himlist_paginate @other_artist\r
641     end\r
642     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do\r
643       r = ResourcePicture.himlist_paginate @other_artist, 3, 10\r
644       r.limit_value.should eq 10\r
645       r.offset_value.should eq 20\r
646     end\r
647   end\r
648   \r
649   describe '一覧取得オプションに於いて' do\r
650     it '3つの項目を含んでいる' do\r
651       r = ResourcePicture.list_opt\r
652       r.should have(3).items\r
653     end\r
654     it 'ライセンスを含んでいる' do\r
655       r = ResourcePicture.list_opt\r
656       r.has_key?(:license).should be_true\r
657     end\r
658     it '絵師を含んでいる' do\r
659       r = ResourcePicture.list_opt\r
660       r.has_key?(:artist).should be_true\r
661     end\r
662     it '実素材を含んでいる' do\r
663       r = ResourcePicture.list_opt\r
664       r.has_key?(:picture).should be_true\r
665     end\r
666   end\r
667   describe 'json一覧出力オプションに於いて' do\r
668     before do\r
669       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
670       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
671       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
672       @sbt = FactoryGirl.create :speech_balloon_template\r
673       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1\r
674       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
675       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
676     end\r
677     it 'ライセンスを含んでいる' do\r
678       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
679       j = JSON.parse r\r
680       i = j.first\r
681       i.has_key?('license').should be_true\r
682     end\r
683     it '絵師を含んでいる' do\r
684       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
685       j = JSON.parse r\r
686       i = j.first\r
687       i.has_key?('artist').should be_true\r
688     end\r
689     it '実素材を含んでいる' do\r
690       r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
691       j = JSON.parse r\r
692       i = j.first\r
693       i.has_key?('picture').should be_true\r
694     end\r
695   end\r
696   \r
697   describe '単体取得に於いて' do\r
698     before do\r
699       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
700       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
701       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
702     end\r
703     context 'つつがなく終わるとき' do\r
704       it '単体取得オプションを利用している' do\r
705         ResourcePicture.stub(:show_opt).with(any_args).and_return({})\r
706         ResourcePicture.should_receive(:show_opt).with(any_args).exactly(1)\r
707         r = ResourcePicture.show @rp.id, @author\r
708       end\r
709       it '閲覧許可を問い合わせている' do\r
710         ResourcePicture.any_instance.stub(:visible?).with(any_args).and_return(true)\r
711         ResourcePicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
712         r = ResourcePicture.show @rp.id, @author\r
713       end\r
714     end\r
715     it '指定の素材を返す' do\r
716       r = ResourcePicture.show @rp.id, @author\r
717       r.should eq @rp\r
718     end\r
719     context '他人の素材を開こうとしたとき' do\r
720       it '403Forbidden例外を返す' do\r
721         ResourcePicture.any_instance.stub(:visible?).and_return(false)\r
722         lambda{\r
723           r = ResourcePicture.show @rp.id, @other_author\r
724         }.should raise_error(ActiveRecord::Forbidden)\r
725       end\r
726     end\r
727     context '存在しない素材を開こうとしたとき' do\r
728       it '404RecordNotFound例外を返す' do\r
729         lambda{\r
730           r = ResourcePicture.show 0, @author\r
731         }.should raise_error(ActiveRecord::RecordNotFound)\r
732       end\r
733     end\r
734   end\r
735   describe '単体取得オプションに於いて' do\r
736     it 'includeキーを含んでいる' do\r
737       r = ResourcePicture.show_opt\r
738       r.has_key?(:include).should be_true\r
739     end\r
740     it '3つの項目を含んでいる' do\r
741       r = ResourcePicture.show_opt[:include]\r
742       r.should have(3).items\r
743     end\r
744     it 'ライセンスを含んでいる' do\r
745       r = ResourcePicture.show_opt[:include]\r
746       r.has_key?(:license).should be_true\r
747     end\r
748     it '絵師を含んでいる' do\r
749       r = ResourcePicture.show_opt[:include]\r
750       r.has_key?(:artist).should be_true\r
751     end\r
752     it '実素材を含んでいる' do\r
753       r = ResourcePicture.show_opt[:include]\r
754       r.has_key?(:picture).should be_true\r
755     end\r
756   end\r
757   describe 'json単体出力オプションに於いて' do\r
758     before do\r
759       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
760       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
761       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
762       @sbt = FactoryGirl.create :speech_balloon_template\r
763       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1\r
764       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
765       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
766     end\r
767     it 'ライセンスを含んでいる' do\r
768       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt\r
769       j = JSON.parse r\r
770       i = j\r
771       i.has_key?('license').should be_true\r
772     end\r
773     it '絵師を含んでいる' do\r
774       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt\r
775       j = JSON.parse r\r
776       i = j\r
777       i.has_key?('artist').should be_true\r
778     end\r
779     it '実素材を含んでいる' do\r
780       r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt\r
781       j = JSON.parse r\r
782       i = j\r
783       i.has_key?('picture').should be_true\r
784     end\r
785   end\r
786   \r
787   describe 'フォーマット変換対象判定に於いて' do\r
788     before do\r
789       @rp = FactoryGirl.build :resource_picture, \r
790         :artist_id => @artist.id, :license_id => @license.id\r
791     end\r
792     context '変換するケース' do\r
793       it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do\r
794         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
795         @rp.ext = 'png'\r
796         @rp.to_gif?.should be_true\r
797       end\r
798     end\r
799     context '変換しないケース' do\r
800       it '画像フォーマットがでない' do\r
801         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
802         @rp.ext = 'gif'\r
803         @rp.to_gif?.should be_false\r
804       end\r
805       it '変換禁止フラグが無効' do\r
806         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)\r
807         @rp.ext = 'png'\r
808         @rp.to_gif?.should be_false\r
809       end\r
810     end\r
811   end\r
812   \r
813   describe '新規実素材の取得に於いて' do\r
814     before do\r
815       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
816       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
817       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', \r
818         :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s}\r
819       @rp = FactoryGirl.build :resource_picture, attr\r
820       @imager = ImagerTest.load "abc\ndef\nghi"\r
821     end\r
822     context '事前チェック' do\r
823       before do\r
824         Picture.any_instance.stub(:store).with(any_args).and_return(true)\r
825       end\r
826       it '実素材モデルにデフォルト値補充を依頼している' do\r
827         Picture.any_instance.should_receive(:supply_default).with(any_args).exactly(1)\r
828         r = @rp.new_picture @imager\r
829       end\r
830       it '実素材モデルに上書き補充を依頼している' do\r
831         Picture.any_instance.should_receive(:overwrite).with(any_args).exactly(1)\r
832         r = @rp.new_picture @imager\r
833       end\r
834       it '実素材を保存している' do\r
835         Picture.any_instance.should_receive(:store).with(any_args).exactly(1)\r
836         r = @rp.new_picture @imager\r
837       end\r
838     end\r
839     context 'つつがなく終わるとき' do\r
840       it '保存された実素材を返す' do\r
841         r = @rp.new_picture @imager\r
842         r.is_a?(Picture).should be_true\r
843       end\r
844     end\r
845     #以下から例外ケース。処理先頭から失敗させていく\r
846     context '実素材の保存に失敗したとき' do\r
847       before do\r
848         Picture.any_instance.stub(:store).with(any_args).and_return(false)\r
849       end\r
850       it 'Falseを返す' do\r
851         r = @rp.new_picture @imager\r
852         r.should be_false\r
853       end\r
854       it '自身の全体エラーメッセージにその旨をセットしている' do\r
855         r = @rp.new_picture @imager\r
856         @rp.errors[:base].should_not be_empty\r
857       end\r
858     end\r
859   end\r
860   \r
861   describe '作成・更新に於いて' do\r
862     before do\r
863       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
864       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
865       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
866       @rp = FactoryGirl.build :resource_picture, attr\r
867       @imager = ImagerTest.load "abc\ndef\nghi"\r
868     end\r
869     context '事前チェック' do\r
870       before do\r
871         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
872         #それで外部のメソッド呼び出しだけに着目してテストする。\r
873         OriginalPicture.any_instance.stub(:save).and_return(true)\r
874         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
875         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)\r
876       end\r
877       it '原画に保存を依頼している' do\r
878         OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
879         r = @rp.store @imager\r
880       end\r
881       it '新規実素材の取得を依頼している' do\r
882         ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1)\r
883         r = @rp.store @imager\r
884       end\r
885       it '自身を保存している' do\r
886         ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)\r
887         r = @rp.store @imager\r
888       end\r
889       it 'gif付き画像ファイルの作成・更新機能で画像を保存している' do\r
890         ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@imager).exactly(1)\r
891         r = @rp.store @imager\r
892       end\r
893     end\r
894     context 'つつがなく終わるとき' do\r
895       before do\r
896         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
897         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
898         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(@imager).and_return(true)\r
899       end\r
900       it 'Trueを返す' do\r
901         r = @rp.store @imager\r
902         r.should be_true\r
903       end\r
904       it '原画の停止日時をクリアする' do\r
905         r = @rp.store @imager\r
906         @rp.original_picture.stopped_at.should be_nil\r
907       end\r
908       it '原画の公開日時に現在時刻をセットする' do\r
909         lambda {\r
910           r = @rp.store @imager\r
911         }.should change(@rp.original_picture, :published_at)\r
912       end\r
913       it '実素材idから最新画像idを取得してセットしている' do\r
914         r = @rp.store @imager\r
915         @rp.picture_id.should eq @p.id\r
916       end\r
917       it '自身が保存されている' do\r
918         lambda {\r
919           r = @rp.store @imager\r
920         }.should change ResourcePicture, :count\r
921       end\r
922     end\r
923     #以下から例外ケース。処理先頭から失敗させていく\r
924     context '原画の保存に失敗したとき' do\r
925       before do\r
926         OriginalPicture.any_instance.stub(:save).and_return(false)\r
927       end\r
928       it 'Falseを返す' do\r
929         r = @rp.unpublish\r
930         r.should be_false\r
931       end\r
932       it 'ロールバックしている' do\r
933         lambda {\r
934           r = @rp.unpublish\r
935         }.should_not change(ResourcePicture, :count)\r
936       end\r
937     end\r
938     context '画像オブジェクトの取得に失敗したとき' do\r
939       before do\r
940         @imager = false\r
941       end\r
942       it 'Falseを返す' do\r
943         r = @rp.store @imager\r
944         r.should be_false\r
945       end\r
946       it '更新されていない' do\r
947         r = @rp.store @imager\r
948         @rp.should be_a_new ResourcePicture\r
949       end\r
950       it '処理を中断してロールバックする' do\r
951         lambda {\r
952           r = @rp.store @imager\r
953         }.should_not change Picture, :count\r
954       end\r
955     end\r
956     context '新規実素材の取得に失敗したとき' do\r
957       before do\r
958         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(false)\r
959       end\r
960       it 'Falseを返す' do\r
961         r = @rp.store @imager\r
962         r.should be_false\r
963       end\r
964       it '更新されていない' do\r
965         r = @rp.store @imager\r
966         @rp.should be_a_new ResourcePicture\r
967       end\r
968       it '処理を中断してロールバックする' do\r
969         lambda {\r
970           r = @rp.store @imager\r
971         }.should_not change Picture, :count\r
972       end\r
973     end\r
974     context '自身の保存に失敗したとき' do\r
975       before do\r
976         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
977         ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)\r
978       end\r
979       it 'Falseを返す' do\r
980         r = @rp.store @imager\r
981         r.should be_false\r
982       end\r
983       it '更新されていない' do\r
984         r = @rp.store @imager\r
985         @rp.should be_a_new ResourcePicture\r
986       end\r
987       it '処理を中断してロールバックする' do\r
988         lambda {\r
989           r = @rp.store @imager\r
990         }.should_not change Picture, :count\r
991       end\r
992     end\r
993     context 'gif付き画像ファイルの作成・更新機能に失敗したとき' do\r
994       before do\r
995         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
996         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(false)\r
997       end\r
998       it 'Falseを返す' do\r
999         r = @rp.store @imager\r
1000         r.should be_false\r
1001       end\r
1002       it '更新されていない' do\r
1003         r = @rp.store @imager\r
1004         @rp.should be_a_new ResourcePicture\r
1005       end\r
1006       it '処理を中断してロールバックする' do\r
1007         lambda {\r
1008           r = @rp.store @imager\r
1009         }.should_not change Picture, :count\r
1010       end\r
1011     end\r
1012   end\r
1013   \r
1014   describe 'gif付き画像ファイルの作成・更新に於いて' do\r
1015     before do\r
1016       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1017       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1018       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
1019       @rp = FactoryGirl.build :resource_picture, attr\r
1020       @imager = ImagerTest.load "abc\ndef\nghi"\r
1021     end\r
1022     context '事前チェック' do\r
1023       before do\r
1024         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
1025         #それで外部のメソッド呼び出しだけに着目してテストする。\r
1026         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
1027         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
1028         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
1029         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
1030       end\r
1031       it '画像ファイルの作成・更新機能で画像を保存している' do\r
1032         #二回目の保存はgif変換の結果を渡す。\r
1033         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(2)\r
1034         r = @rp.store_picture_with_gif @imager\r
1035       end\r
1036       it '自身にフォーマット変換対象かを問い合わせている' do\r
1037         ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)\r
1038         r = @rp.store_picture_with_gif @imager\r
1039       end\r
1040       it '画像ライブラリにGifフォーマット変換を依頼している' do\r
1041         ImagerTest.any_instance.should_receive(:to_gif).with(any_args).exactly(1)\r
1042         r = @rp.store_picture_with_gif @imager\r
1043       end\r
1044     end\r
1045     context 'つつがなく終わるとき' do\r
1046       before do\r
1047         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
1048         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
1049         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
1050         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
1051         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
1052       end\r
1053       it 'Trueを返す' do\r
1054         r = @rp.store_picture_with_gif @imager\r
1055         r.should be_true\r
1056       end\r
1057     end\r
1058     context 'gif変換なしで、つつがなく終わるとき' do\r
1059       before do\r
1060         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
1061         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
1062         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)\r
1063         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
1064         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
1065       end\r
1066       it 'Trueを返す' do\r
1067         r = @rp.store_picture_with_gif @imager\r
1068         r.should be_true\r
1069       end\r
1070       it 'gif保存は呼ばれていない' do\r
1071         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
1072         r = @rp.store_picture_with_gif @imager\r
1073       end\r
1074     end\r
1075     #以下から例外ケース。処理先頭から失敗させていく\r
1076     context '画像の保存に失敗したとき' do\r
1077       before do\r
1078         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)\r
1079       end\r
1080       it 'Falseを返す' do\r
1081         r = @rp.store_picture_with_gif @imager\r
1082         r.should be_false\r
1083       end\r
1084       it '全体エラーメッセージがセットされている' do\r
1085         lambda {\r
1086           r = @rp.store_picture_with_gif @imager\r
1087         }.should change(@rp.errors[:base], :count)\r
1088       end\r
1089       it 'gif変換判定は呼ばれていない' do\r
1090         ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)\r
1091         r = @rp.store_picture_with_gif @imager\r
1092       end\r
1093     end\r
1094     context 'gif変換に失敗したとき' do\r
1095       before do\r
1096         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
1097         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
1098         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(false)\r
1099       end\r
1100       it 'Falseを返す' do\r
1101         r = @rp.store_picture_with_gif @imager\r
1102         r.should be_false\r
1103       end\r
1104       it '全体エラーメッセージがセットされている' do\r
1105         lambda {\r
1106           r = @rp.store_picture_with_gif @imager\r
1107         }.should change(@rp.errors[:base], :count)\r
1108       end\r
1109       it 'gif画像の保存は呼ばれていない' do\r
1110         #本画像の保存があるので、一度は呼ばれる\r
1111         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
1112         r = @rp.store_picture_with_gif @imager\r
1113       end\r
1114     end\r
1115     context 'gif画像の保存に失敗したとき' do\r
1116       before do\r
1117         ResourcePicture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true)\r
1118         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
1119         @gifimager = @imager.to_gif\r
1120         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(@gifimager)\r
1121         ResourcePicture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false)\r
1122         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
1123         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
1124       end\r
1125       it 'Falseを返す' do\r
1126         r = @rp.store_picture_with_gif @imager\r
1127         r.should be_false\r
1128       end\r
1129       it '全体エラーメッセージがセットされている' do\r
1130         lambda {\r
1131           r = @rp.store_picture_with_gif @imager\r
1132         }.should change(@rp.errors[:base], :count)\r
1133       end\r
1134     end\r
1135   end\r
1136   \r
1137   describe '画像ファイルの作成・更新に於いて' do\r
1138     #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。\r
1139     before do\r
1140       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1141       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1142       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
1143       @rp = FactoryGirl.build :resource_picture, attr\r
1144       @rp.overwrite @op\r
1145       @imager = ImagerTest.load "abc\ndef\nghi"\r
1146     end\r
1147     context '事前チェック' do\r
1148       before do\r
1149         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
1150         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
1151         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1152       end\r
1153       it 'サムネイル化が1回呼ばれる' do\r
1154         ImagerTest.any_instance.should_receive(:to_thumbnail).with(any_args).exactly(1)\r
1155         r = @rp.store_picture(@imager, '1.png')\r
1156       end\r
1157       it '画像ファイルの保存が2回呼ばれる' do\r
1158         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2)\r
1159         r = @rp.store_picture(@imager, '1.gif')\r
1160       end\r
1161       it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do\r
1162         PictureIO.resource_picture_io.should_receive(:put).with(@imager.to_thumbnail.binary, '1.gif').exactly(1)\r
1163         r = @rp.store_picture(@imager, '1.gif')\r
1164       end\r
1165       it '画像ファイルのfullへの保存が1回呼ばれる' do\r
1166         PictureIO.resource_picture_io.should_receive(:put).with(@imager.binary, '1.gif', 'full').exactly(1)\r
1167         r = @rp.store_picture(@imager, '1.gif')\r
1168       end\r
1169     end\r
1170     context 'つつがなく終わるとき' do\r
1171       before do\r
1172         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
1173         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
1174         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1175       end\r
1176       it 'Trueを返す' do\r
1177         r = @rp.store_picture(@imager, '1.gif')\r
1178         r.should be_true\r
1179       end\r
1180     end\r
1181     #以下から例外ケース。処理先頭から失敗させていく\r
1182     context 'サムネイル化に失敗したとき' do\r
1183       before do\r
1184         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1185         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(false)\r
1186       end\r
1187       it 'Falseを返す' do\r
1188         r = @rp.store_picture(@imager, '1.gif')\r
1189         r.should be_false\r
1190       end\r
1191       it '画像ファイルの保存は呼ばれていない' do\r
1192         PictureIO.resource_picture_io.should_not_receive(:put).with(any_args)\r
1193         r = @rp.store_picture(@imager, '1.gif')\r
1194       end\r
1195     end\r
1196     context 'サムネイル画像の保存に失敗したとき' do\r
1197       before do\r
1198         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1199         @tmbimager = @imager.to_thumbnail\r
1200         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
1201         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_raise(PictureIO::Error)\r
1202       end\r
1203       it 'Falseを返す' do\r
1204         r = @rp.store_picture(@imager, '1.gif')\r
1205         r.should be_false\r
1206       end\r
1207       it '画像ファイルの保存は呼ばれていない' do\r
1208         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(1)\r
1209         r = @rp.store_picture(@imager, '1.gif')\r
1210       end\r
1211     end\r
1212     context '画像の保存に失敗したとき' do\r
1213       before do\r
1214         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
1215         @tmbimager = @imager.to_thumbnail\r
1216         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
1217         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(true)\r
1218         PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_raise(PictureIO::Error)\r
1219       end\r
1220       it 'Falseを返す' do\r
1221         r = @rp.store_picture(@imager, '1.gif')\r
1222         r.should be_false\r
1223       end\r
1224     end\r
1225     \r
1226   end\r
1227   \r
1228   describe '公開停止に於いて' do\r
1229     before do\r
1230       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1231       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1232       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
1233       PictureIO.resource_picture_io.stub(:exist?).with(@rp.filename).and_return(true)\r
1234       PictureIO.resource_picture_io.stub(:exist?).with(@rp.filename, 'full').and_return(true)\r
1235     end\r
1236     context '事前チェックしておく' do\r
1237       before do\r
1238         OriginalPicture.any_instance.stub(:save).and_return(true)\r
1239         ResourcePicture.any_instance.stub(:destroy).and_return(true)\r
1240         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true)\r
1241         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true)\r
1242       end\r
1243       it '原画に保存を依頼している' do\r
1244         OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
1245         r = @rp.unpublish\r
1246       end\r
1247       it '素材モデルに削除を依頼している' do\r
1248         ResourcePicture.any_instance.should_receive(:destroy).exactly(1)\r
1249         r = @rp.unpublish\r
1250       end\r
1251       it '保管庫にサムネイルの画像データ削除を依頼している' do\r
1252         PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename).exactly(1)\r
1253         r = @rp.unpublish\r
1254       end\r
1255       it '保管庫にフルサイズの画像データ削除を依頼している' do\r
1256         PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename, 'full').exactly(1)\r
1257         r = @rp.unpublish\r
1258       end\r
1259     end\r
1260     context 'つつがなく終わるとき' do\r
1261       it '自身を削除する' do\r
1262         lambda {\r
1263           r = @rp.unpublish\r
1264         }.should change(ResourcePicture, :count).by(-1)\r
1265         lambda {\r
1266           r = ResourcePicture.find @rp.id\r
1267         }.should raise_error\r
1268       end\r
1269       it '原画の公開日時をクリアする' do\r
1270         r = @rp.store @imager\r
1271         @rp.original_picture.published_at.should be_nil\r
1272       end\r
1273       it '原画の停止日時に現在時刻をセットする' do\r
1274         lambda {\r
1275           r = @rp.unpublish\r
1276         }.should change(@rp.original_picture, :stopped_at)\r
1277       end\r
1278       it 'Trueを返す' do\r
1279         r = @rp.unpublish\r
1280         r.should be_true\r
1281       end\r
1282     end\r
1283     context '原画の保存に失敗したとき' do\r
1284       before do\r
1285         OriginalPicture.any_instance.stub(:save).and_return(false)\r
1286       end\r
1287       it 'Falseを返す' do\r
1288         r = @rp.unpublish\r
1289         r.should be_false\r
1290       end\r
1291       it 'ロールバックしている' do\r
1292         lambda {\r
1293           r = @rp.unpublish\r
1294         }.should_not change(ResourcePicture, :count)\r
1295       end\r
1296     end\r
1297     context '自身の削除に失敗したとき' do\r
1298       before do\r
1299         ResourcePicture.any_instance.stub(:destroy).with(any_args).and_return(false)\r
1300       end\r
1301       it 'Falseを返す' do\r
1302         r = @rp.unpublish\r
1303         r.should be_false\r
1304       end\r
1305       it 'ロールバックしている' do\r
1306         lambda {\r
1307           r = @rp.unpublish\r
1308         }.should_not change(ResourcePicture, :count)\r
1309       end\r
1310     end\r
1311     context 'サムネイル画像の削除に失敗したとき' do\r
1312       before do\r
1313         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_raise(PictureIO::Error)\r
1314         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true)\r
1315       end\r
1316       it 'Falseを返す' do\r
1317         r = @rp.unpublish\r
1318         r.should be_false\r
1319       end\r
1320       it 'ロールバックしている' do\r
1321         lambda {\r
1322           r = @rp.unpublish\r
1323         }.should_not change(ResourcePicture, :count)\r
1324       end\r
1325     end\r
1326     context 'フルサイズ画像の削除に失敗したとき' do\r
1327       before do\r
1328         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true)\r
1329         PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_raise(PictureIO::Error)\r
1330       end\r
1331       it 'Falseを返す' do\r
1332         r = @rp.unpublish\r
1333         r.should be_false\r
1334       end\r
1335       it 'ロールバックしている' do\r
1336         lambda {\r
1337           r = @rp.unpublish\r
1338         }.should_not change(ResourcePicture, :count)\r
1339       end\r
1340     end\r
1341   end\r
1342   \r
1343   describe 'クレジットデータに於いて' do\r
1344     before do\r
1345       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1346       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
1347       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{"system_picture_id": 2}', :settings => {}.to_json.to_s}\r
1348       @rp = FactoryGirl.build :resource_picture, attr\r
1349     end\r
1350     it 'system_picture_idが入っている' do\r
1351       res = @rp.credit_data\r
1352       res["system_picture_id"].should eq 2\r
1353     end\r
1354   end\r
1355   \r
1356 end\r
1357 \r