OSDN Git Service

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