OSDN Git Service

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