1 # -*- encoding: utf-8 -*-
5 describe OriginalPicture do
7 FactoryGirl.create :admin
8 @user = FactoryGirl.create( :user_yas)
10 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11 @other_user = FactoryGirl.create( :user_yas)
12 @other_author = @other_user.author
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
21 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
24 context 'オーソドックスなデータのとき' do
26 @op.ext = 'png' #リストにない拡張子は通らないし
37 @op.filesize = 2000000
43 context 'extを検証するとき' do
46 @op.should_not be_valid
50 @op.should_not be_valid
52 it 'png,gif,jpeg以外なら失敗する' do
54 @op.should_not be_valid
57 context 'widthを検証するとき' do
60 @op.should_not be_valid
64 @op.should_not be_valid
68 @op.should_not be_valid
72 @op.should_not be_valid
75 context 'heightを検証するとき' do
78 @op.should_not be_valid
82 @op.should_not be_valid
86 @op.should_not be_valid
90 @op.should_not be_valid
93 context 'filesizeを検証するとき' do
96 @op.should_not be_valid
100 @op.should_not be_valid
104 @op.should_not be_valid
107 @op.filesize = 2000000+1
108 @op.should_not be_valid
111 context 'artist_idを検証するとき' do
114 @op.should_not be_valid
118 @op.should_not be_valid
120 it '存在する絵師でなければ失敗する' do
122 @op.should_not be_valid
125 context 'md5を検証するとき' do
128 @op.should_not be_valid
132 @op.should_not be_valid
136 @op.should_not be_valid
141 describe 'デフォルト値補充に於いて' do
143 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
148 describe '上書き補充に於いて' do
150 @op = FactoryGirl.build :original_picture, :artist_id => nil
151 @op.overwrite @artist
152 @op.artist_id.should eq @artist.id
156 describe '作者判定に於いて' do
158 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
160 context 'パラメータが作家のとき' do
162 @op.own?(@author).should == true
165 @op.own?(@other_author).should == false
168 context 'パラメータが絵師のとき' do
170 @op.own?(@artist).should == true
173 @op.own?(@other_artist).should == false
178 @op.own?(nil).should == false
183 describe '閲覧許可に於いて' do
185 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
187 it '自分の原画を見るときは許可する' do
188 OriginalPicture.any_instance.stub(:own?).and_return(true)
189 r = @op.visible?(@artist)
193 OriginalPicture.any_instance.stub(:own?).and_return(false)
194 r = @op.visible?(@artist)
199 describe 'ファイル名に於いて' do
201 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
203 it 'id+拡張子のフォーマットで返す' do
205 r.should eq "#{@op.id}.png"
209 describe 'MimeTypeに於いて' do
211 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
213 it 'image/拡張子のフォーマットで返す' do
215 r.should eq "image/png"
219 describe 'ファイルのurlに於いて' do
221 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
222 OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
224 it 'ファイル名取得を依頼している' do
225 OriginalPicture.any_instance.should_receive(:filename).exactly(1)
228 it '/original_pictures/3.gifのフォーマットで返す' do
230 r.should eq "/original_pictures/3.gif"
234 describe 'サムネイル画像タグオプションに於いて' do
236 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
237 OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
238 PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
240 it 'サムネイル画像の幅高さ取得を依頼している' do
241 PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
245 r = @op.tmb_opt_img_tag
246 r.is_a?(Hash).should be_true
249 r = @op.tmb_opt_img_tag
250 r.has_key?(:src).should be_true
251 r[:src].should eq '/original_pictures/3.gif'
253 it 'widthキーを含んでいる' do
254 r = @op.tmb_opt_img_tag
255 r.has_key?(:width).should be_true
256 r[:width].should eq 40
258 it 'heightキーを含んでいる' do
259 r = @op.tmb_opt_img_tag
260 r.has_key?(:height).should be_true
261 r[:height].should eq 30
265 describe '画像タグオプションに於いて' do
267 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
268 OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
272 r.is_a?(Hash).should be_true
276 r.has_key?(:src).should be_true
277 r[:src].should eq '/original_pictures/3.gif'
279 it 'widthキーを含んでいる' do
281 r.has_key?(:width).should be_true
282 r[:width].should eq @op.width
284 it 'heightキーを含んでいる' do
286 r.has_key?(:height).should be_true
287 r[:height].should eq @op.height
291 describe '一覧取得に於いて' do
293 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
295 context 'page補正について' do
296 it '文字列から数値に変換される' do
297 OriginalPicture.page('8').should eq 8
300 OriginalPicture.page().should eq 1
303 OriginalPicture.page('0').should eq 1
306 context 'page_size補正について' do
307 it '文字列から数値に変換される' do
308 OriginalPicture.page_size('7').should eq 7
310 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
311 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
313 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
314 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
316 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
317 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
321 describe '一覧取得オプションに於いて' do
322 it 'includeキーを含んでいる' do
323 r = OriginalPicture.list_opt
324 r.has_key?(:include).should be_true
327 r = OriginalPicture.list_opt[:include]
328 r.should have(2).items
331 r = OriginalPicture.list_opt[:include]
332 r.has_key?(:resource_picture).should be_true
335 r = OriginalPicture.list_opt[:include]
336 r.has_key?(:pictures).should be_true
339 describe 'json一覧出力オプションに於いて' do
341 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
342 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
343 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
344 @sbt = FactoryGirl.create :speech_balloon_template
345 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
346 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
347 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
350 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
353 i.has_key?('resource_picture').should be_true
356 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
359 i.has_key?('pictures').should be_true
363 describe '自分のコミック一覧取得に於いて' do
365 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
367 context 'つつがなく終わるとき' do
368 it '一覧取得オプションを利用している' do
369 OriginalPicture.stub(:list_opt).with(any_args).and_return({})
370 OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
371 r = OriginalPicture.mylist @artist
375 r = OriginalPicture.mylist @artist
379 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
380 r = OriginalPicture.mylist @artist
381 r.should eq [nc, @op]
383 it '他人のコミックはxxxでも含まない' do
384 nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
385 r = OriginalPicture.mylist @artist
388 it '自分のコミックはxxxでも含んでいる' do
389 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
390 r = OriginalPicture.mylist @artist
391 r.should eq [nc, @op]
393 context 'DBに5件あって1ページの件数を2件に変えたとして' do
395 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
396 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
397 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
398 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
401 r = OriginalPicture.mylist @artist, 1, 2
402 r.should have(2).items
404 it 'page=1なら末尾2件を返す' do
406 r = OriginalPicture.mylist @artist, 1, 2
407 r.should eq [@op5, @op4]
409 it 'page=2なら中間2件を返す' do
410 r = OriginalPicture.mylist @artist, 2, 2
411 r.should eq [@op3, @op2]
413 it 'page=3なら先頭1件を返す' do
414 r = OriginalPicture.mylist @artist, 3, 2
418 context 'DBに5件あって1ページの件数を0件に変えたとして' do
420 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
421 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
422 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
423 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
424 OriginalPicture.stub(:default_page_size).and_return(2)
427 r = OriginalPicture.mylist @artist, 5, 0
428 r.should have(5).items
433 describe '更新履歴一覧取得に於いて' do
435 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
436 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
437 :original_picture_id => @op.id
438 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
444 it '他の原画の実素材は含んでいない' do
445 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
446 :original_picture_id => @op2.id
450 it 'revisionで並んでいる' do
451 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
452 :original_picture_id => @op.id
454 r.should eq [@p2, @p]
458 describe '単体取得に於いて' do
460 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
462 context 'つつがなく終わるとき' do
463 it '単体取得オプションを利用している' do
464 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
465 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
466 r = OriginalPicture.show @op.id, @artist
468 it '閲覧許可を問い合わせている' do
469 OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
470 OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
471 r = OriginalPicture.show @op.id, @artist
475 pic = OriginalPicture.show @op.id, @artist
478 context '他人の原画を開こうとしたとき' do
479 it '403Forbidden例外を返す' do
480 OriginalPicture.any_instance.stub(:visible?).and_return(false)
482 pic = OriginalPicture.show @op.id, @other_artist
483 }.should raise_error(ActiveRecord::Forbidden)
486 context '存在しない原画を開こうとしたとき' do
487 it '404RecordNotFound例外を返す' do
489 pic = OriginalPicture.show 0, @artist
490 }.should raise_error(ActiveRecord::RecordNotFound)
494 describe '編集取得に於いて' do
496 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
498 context 'つつがなく終わるとき' do
499 it '単体取得オプションを利用している' do
500 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
501 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
502 r = OriginalPicture.edit @op.id, @artist
504 it '所持判定を問い合わせている' do
505 OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
506 OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
507 r = OriginalPicture.edit @op.id, @artist
511 pic = OriginalPicture.edit @op.id, @artist
514 context '他人の原画を開こうとしたとき' do
515 it '403Forbidden例外を返す' do
516 OriginalPicture.any_instance.stub(:own?).and_return(false)
518 r = OriginalPicture.edit @op.id, @other_artist
519 }.should raise_error(ActiveRecord::Forbidden)
522 context '存在しない原画を開こうとしたとき' do
523 it '404RecordNotFound例外を返す' do
525 r = OriginalPicture.edit 0, @artist
526 }.should raise_error(ActiveRecord::RecordNotFound)
530 describe '単体取得オプションに於いて' do
531 it 'includeキーを含んでいる' do
532 r = OriginalPicture.show_opt
533 r.has_key?(:include).should be_true
536 r = OriginalPicture.show_opt[:include]
537 r.should have(2).items
540 r = OriginalPicture.show_opt[:include]
541 r.has_key?(:resource_picture).should be_true
544 r = OriginalPicture.show_opt[:include]
545 r.has_key?(:pictures).should be_true
548 describe 'json単体出力オプションに於いて' do
550 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
551 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
552 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
553 @sbt = FactoryGirl.create :speech_balloon_template
554 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
555 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
556 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
559 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
562 i.has_key?('resource_picture').should be_true
565 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
568 i.has_key?('pictures').should be_true
571 describe '作成・更新に於いて' do
573 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
574 @imager = ImagerTest.load "abc\ndef\nghi"
578 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
579 #それで外部のメソッド呼び出しだけに着目してテストする。
580 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
581 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
583 it '自身に属性をセットしている' do
584 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
588 OriginalPicture.any_instance.should_receive(:save).exactly(1)
591 it 'PictureIoに画像データの保存を依頼している' do
592 PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
596 context 'つつがなく終わるとき' do
599 it '自身に属性をセットしている' do
601 @op.width.should eq 3
602 @op.height.should eq 3
603 @op.filesize.should eq 9
605 it '原画モデルが作成されている' do
608 }.should change OriginalPicture, :count
612 OriginalPicture.find(@op).should_not be_nil
615 @op.store(@imager).should eq true
618 #以下から例外ケース。処理先頭から失敗させていく
619 context 'imagerが初期化に失敗したとき' do
623 @op.store(false).should be_false
625 it '自身の保存は呼ばれていない' do
626 OriginalPicture.any_instance.should_not_receive(:save)
629 it '全体エラーメッセージがセットされている' do
632 }.should change(@op.errors[:base], :count)
635 context '自身の保存に失敗したとき' do
637 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
640 @op.store(@imager).should be_false
644 @op.should be_a_new OriginalPicture
646 it '原画の保存は呼ばれていない' do
647 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
650 context '原画の保存に失敗したとき' do
652 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
653 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
656 @op.store(@imager).should be_false
660 @op.should be_a_new OriginalPicture
662 it '全体エラーメッセージがセットされている' do
665 }.should change(@op.errors[:base], :count)
671 describe 'エクスポートに於いて' do
673 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
674 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
675 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
677 @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
679 context 'つつがなく終わるとき' do
680 it 'ライセンスグループに依頼してリストを取得している' do
681 LicenseGroup.stub(:list).with(any_args).and_return([@lg])
682 LicenseGroup.should_receive(:list).with(any_args).exactly(1)
683 r = OriginalPicture.export(@artist)
685 it '原画に依頼してリストを取得している' do
686 OriginalPicture.stub(:list).with(any_args).and_return([@op])
687 OriginalPicture.should_receive(:list).with(any_args).exactly(1)
688 r = OriginalPicture.export(@artist)
691 r = OriginalPicture.export(@artist)
692 r.is_a?(Hash).should be_true
694 it 'Hashはライセンスグループを含んでいる' do
695 r = OriginalPicture.export(@artist)
696 r.include?(:license_groups).should be_true
698 it 'Hashは原画を含んでいる' do
699 r = OriginalPicture.export(@artist)
700 r.include?(:original_pictures).should be_true
702 #素材がライセンスされていないケースもある
703 it 'Hashの原画は素材を含んでいる' do
704 r = OriginalPicture.export(@artist)
705 r[:original_pictures].first.resource_picture.should_not be_nil
707 it 'Hashの原画は実素材を含んでいる' do
708 r = OriginalPicture.export(@artist)
709 r[:original_pictures].first.pictures.should be_nil
712 context '実データ単体のとき' do
713 it 'ライセンスは配列構造になっている' do
714 r = OriginalPicture.export(@artist)
715 r[:license_groups].is_a?(Array).should be_true
718 r = OriginalPicture.export(@artist)
719 r[:license_groups].size.should eq 1
720 r[:license_groups].first.should eq @lg
722 it '原画は配列構造になっている' do
723 r = OriginalPicture.export(@artist)
724 r[:original_pictures].is_a?(Array).should be_true
727 r = OriginalPicture.export(@artist)
728 r[:original_pictures].size.should eq 1
729 r[:original_pictures].first.should eq @op
731 it '原画に素材が関連付いている' do
732 r = OriginalPicture.export(@artist)
733 i = r[:original_pictures].first
734 i.resource_picture.should eq @rp
736 it '原画に実素材が関連付いている' do
737 r = OriginalPicture.export(@artist)
738 i = r[:original_pictures].first
739 i.picture.should eq @p
742 context '実データ複数のとき' do
744 @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
745 @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
746 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
747 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
748 @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
750 it 'ライセンスは配列構造になっている' do
751 r = OriginalPicture.export(@artist)
752 r[:license_groups].is_a?(Array).should be_true
755 r = OriginalPicture.export(@artist)
756 r[:license_groups].size.should eq 2
757 r[:license_groups].first.should eq @lg
758 r[:license_groups].last.should eq @lg2
760 it '原画は配列構造になっている' do
761 r = OriginalPicture.export(@artist)
762 r[:original_pictures].is_a?(Array).should be_true
765 r = OriginalPicture.export(@artist)
766 r[:original_pictures].size.should eq 2
767 r[:original_pictures].first.should eq @op
768 r[:original_pictures].last.should eq @op2
770 it '原画に素材が関連付いている' do
771 r = OriginalPicture.export(@artist)
772 i = r[:original_pictures].first
773 i.resource_picture.should eq @rp
774 i2 = r[:original_pictures].last
775 i2.resource_picture.should eq @rp2
777 it '原画に実素材が関連付いている' do
778 r = OriginalPicture.export(@artist)
779 i = r[:original_pictures].first
780 i.picture.should eq @p
781 i2 = r[:original_pictures].last
782 i2.picture.should eq @p2
787 describe 'エクスポートオプションに於いて' do
790 describe 'インポートに於いて' do
792 @imports = {:licenses => {}, :artist_id => @artist.id}
794 context '事前チェックしておく' do