OSDN Git Service

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