1 # -*- encoding: utf-8 -*-
5 describe OriginalPicture do
7 @admin = 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(false)
189 r = @op.visible?(@admin)
192 it '所持判定を問い合わせ、自分の原画なら許可する' do
193 OriginalPicture.any_instance.stub(:own?).and_return(true)
194 r = @op.visible?(@artist)
198 OriginalPicture.any_instance.stub(:own?).and_return(false)
199 r = @op.visible?(@artist)
204 describe 'ファイル名に於いて' do
206 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
208 it 'id+拡張子のフォーマットで返す' do
210 r.should eq "#{@op.id}.png"
214 describe 'MimeTypeに於いて' do
216 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
218 it 'image/拡張子のフォーマットで返す' do
220 r.should eq "image/png"
224 describe 'ファイルのurlに於いて' do
226 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
227 OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
229 it 'ファイル名取得を依頼している' do
230 OriginalPicture.any_instance.should_receive(:filename).exactly(1)
233 it '/original_pictures/3.gifのフォーマットで返す' do
235 r.should eq "/original_pictures/3.gif"
239 describe 'サムネイル画像タグオプションに於いて' 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])
245 it 'サムネイル画像の幅高さ取得を依頼している' do
246 PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
250 r = @op.tmb_opt_img_tag
251 r.is_a?(Hash).should be_true
254 r = @op.tmb_opt_img_tag
255 r.has_key?(:src).should be_true
256 r[:src].should eq '/original_pictures/3.gif'
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
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
270 describe '画像タグオプションに於いて' do
272 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
273 OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
277 r.is_a?(Hash).should be_true
281 r.has_key?(:src).should be_true
282 r[:src].should eq '/original_pictures/3.gif'
284 it 'widthキーを含んでいる' do
286 r.has_key?(:width).should be_true
287 r[:width].should eq @op.width
289 it 'heightキーを含んでいる' do
291 r.has_key?(:height).should be_true
292 r[:height].should eq @op.height
296 describe '一覧取得に於いて' do
298 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
300 context 'page補正について' do
301 it '文字列から数値に変換される' do
302 OriginalPicture.page('8').should eq 8
305 OriginalPicture.page().should eq 1
308 OriginalPicture.page('0').should eq 1
311 context 'page_size補正について' do
312 it '文字列から数値に変換される' do
313 OriginalPicture.page_size('7').should eq 7
315 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
316 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
318 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
319 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
321 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
322 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
326 describe '一覧取得オプションに於いて' do
327 it 'includeキーを含んでいる' do
328 r = OriginalPicture.list_opt
329 r.has_key?(:include).should be_true
332 r = OriginalPicture.list_opt[:include]
333 r.should have(2).items
336 r = OriginalPicture.list_opt[:include]
337 r.has_key?(:resource_picture).should be_true
340 r = OriginalPicture.list_opt[:include]
341 r.has_key?(:pictures).should be_true
344 describe 'json一覧出力オプションに於いて' do
346 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
347 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
348 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
349 @sbt = FactoryGirl.create :speech_balloon_template
350 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
351 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
352 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
355 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
358 i.has_key?('resource_picture').should be_true
361 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
364 i.has_key?('pictures').should be_true
368 describe '自分のコミック一覧取得に於いて' do
370 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
372 context 'つつがなく終わるとき' do
373 it '一覧取得オプションを利用している' do
374 OriginalPicture.stub(:list_opt).with(any_args).and_return({})
375 OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
376 r = OriginalPicture.mylist @artist
380 r = OriginalPicture.mylist @artist
384 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
385 r = OriginalPicture.mylist @artist
386 r.should eq [nc, @op]
388 it '他人のコミックはxxxでも含まない' do
389 nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
390 r = OriginalPicture.mylist @artist
393 it '自分のコミックはxxxでも含んでいる' do
394 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
395 r = OriginalPicture.mylist @artist
396 r.should eq [nc, @op]
398 context 'DBに5件あって1ページの件数を2件に変えたとして' do
400 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
401 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
402 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
403 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
406 r = OriginalPicture.mylist @artist, 1, 2
407 r.should have(2).items
409 it 'page=1なら末尾2件を返す' do
411 r = OriginalPicture.mylist @artist, 1, 2
412 r.should eq [@op5, @op4]
414 it 'page=2なら中間2件を返す' do
415 r = OriginalPicture.mylist @artist, 2, 2
416 r.should eq [@op3, @op2]
418 it 'page=3なら先頭1件を返す' do
419 r = OriginalPicture.mylist @artist, 3, 2
423 context 'DBに5件あって1ページの件数を0件に変えたとして' do
425 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
426 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
427 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
428 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
429 OriginalPicture.stub(:default_page_size).and_return(2)
432 r = OriginalPicture.mylist @artist, 5, 0
433 r.should have(5).items
438 describe '更新履歴一覧取得に於いて' do
440 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
441 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
442 :original_picture_id => @op.id
443 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
449 it '他の原画の実素材は含んでいない' do
450 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
451 :original_picture_id => @op2.id
455 it 'revisionで並んでいる' do
456 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
457 :original_picture_id => @op.id
459 r.should eq [@p2, @p]
463 describe '単体取得に於いて' do
465 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
467 context 'つつがなく終わるとき' do
468 it '単体取得オプションを利用している' do
469 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
470 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
471 r = OriginalPicture.show @op.id, @artist
473 it '閲覧許可を問い合わせている' do
474 OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
475 OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
476 r = OriginalPicture.show @op.id, @artist
480 pic = OriginalPicture.show @op.id, @artist
483 context '他人の原画を開こうとしたとき' do
484 it '403Forbidden例外を返す' do
485 OriginalPicture.any_instance.stub(:visible?).and_return(false)
487 pic = OriginalPicture.show @op.id, @other_artist
488 }.should raise_error(ActiveRecord::Forbidden)
491 context '存在しない原画を開こうとしたとき' do
492 it '404RecordNotFound例外を返す' do
494 pic = OriginalPicture.show 0, @artist
495 }.should raise_error(ActiveRecord::RecordNotFound)
499 describe '編集取得に於いて' do
501 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
503 context 'つつがなく終わるとき' do
504 it '単体取得オプションを利用している' do
505 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
506 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
507 r = OriginalPicture.edit @op.id, @artist
509 it '所持判定を問い合わせている' do
510 OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
511 OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
512 r = OriginalPicture.edit @op.id, @artist
516 pic = OriginalPicture.edit @op.id, @artist
519 context '他人の原画を開こうとしたとき' do
520 it '403Forbidden例外を返す' do
521 OriginalPicture.any_instance.stub(:own?).and_return(false)
523 r = OriginalPicture.edit @op.id, @other_artist
524 }.should raise_error(ActiveRecord::Forbidden)
527 context '存在しない原画を開こうとしたとき' do
528 it '404RecordNotFound例外を返す' do
530 r = OriginalPicture.edit 0, @artist
531 }.should raise_error(ActiveRecord::RecordNotFound)
535 describe '単体取得オプションに於いて' do
536 it 'includeキーを含んでいる' do
537 r = OriginalPicture.show_opt
538 r.has_key?(:include).should be_true
541 r = OriginalPicture.show_opt[:include]
542 r.should have(2).items
545 r = OriginalPicture.show_opt[:include]
546 r.has_key?(:resource_picture).should be_true
549 r = OriginalPicture.show_opt[:include]
550 r.has_key?(:pictures).should be_true
553 describe 'json単体出力オプションに於いて' do
555 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
556 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
557 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
558 @sbt = FactoryGirl.create :speech_balloon_template
559 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
560 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
561 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
564 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
567 i.has_key?('resource_picture').should be_true
570 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
573 i.has_key?('pictures').should be_true
576 describe '作成・更新に於いて' do
578 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
579 @imager = ImagerTest.load "abc\ndef\nghi"
583 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
584 #それで外部のメソッド呼び出しだけに着目してテストする。
585 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
586 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
588 it '自身に属性をセットしている' do
589 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
593 OriginalPicture.any_instance.should_receive(:save).exactly(1)
596 it 'PictureIoに画像データの保存を依頼している' do
597 PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
601 context 'つつがなく終わるとき' do
604 it '自身に属性をセットしている' do
606 @op.width.should eq 3
607 @op.height.should eq 3
608 @op.filesize.should eq 9
610 it '原画モデルが作成されている' do
613 }.should change OriginalPicture, :count
617 OriginalPicture.find(@op).should_not be_nil
620 @op.store(@imager).should eq true
623 #以下から例外ケース。処理先頭から失敗させていく
624 context 'imagerが初期化に失敗したとき' do
628 @op.store(false).should be_false
630 it '自身の保存は呼ばれていない' do
631 OriginalPicture.any_instance.should_not_receive(:save)
634 it '全体エラーメッセージがセットされている' do
637 }.should change(@op.errors[:base], :count)
640 context '自身の保存に失敗したとき' do
642 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
645 @op.store(@imager).should be_false
649 @op.should be_a_new OriginalPicture
651 it '原画の保存は呼ばれていない' do
652 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
655 context '原画の保存に失敗したとき' do
657 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
658 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
661 @op.store(@imager).should be_false
665 @op.should be_a_new OriginalPicture
667 it '全体エラーメッセージがセットされている' do
670 }.should change(@op.errors[:base], :count)
676 describe 'エクスポートに於いて' do
678 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
679 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
680 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
682 @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
684 context 'つつがなく終わるとき' do
685 it 'ライセンスグループに依頼してリストを取得している' do
686 LicenseGroup.stub(:list).with(any_args).and_return([@lg])
687 LicenseGroup.should_receive(:list).with(any_args).exactly(1)
688 r = OriginalPicture.export(@artist)
690 it '原画に依頼してリストを取得している' do
691 OriginalPicture.stub(:list).with(any_args).and_return([@op])
692 OriginalPicture.should_receive(:list).with(any_args).exactly(1)
693 r = OriginalPicture.export(@artist)
696 r = OriginalPicture.export(@artist)
697 r.is_a?(Hash).should be_true
699 it 'Hashはライセンスグループを含んでいる' do
700 r = OriginalPicture.export(@artist)
701 r.include?(:license_groups).should be_true
703 it 'Hashは原画を含んでいる' do
704 r = OriginalPicture.export(@artist)
705 r.include?(:original_pictures).should be_true
707 #素材がライセンスされていないケースもある
708 it 'Hashの原画は素材を含んでいる' do
709 r = OriginalPicture.export(@artist)
710 r[:original_pictures].first.resource_picture.should_not be_nil
712 it 'Hashの原画は実素材を含んでいる' do
713 r = OriginalPicture.export(@artist)
714 r[:original_pictures].first.pictures.should be_nil
717 context '実データ単体のとき' do
718 it 'ライセンスは配列構造になっている' do
719 r = OriginalPicture.export(@artist)
720 r[:license_groups].is_a?(Array).should be_true
723 r = OriginalPicture.export(@artist)
724 r[:license_groups].size.should eq 1
725 r[:license_groups].first.should eq @lg
727 it '原画は配列構造になっている' do
728 r = OriginalPicture.export(@artist)
729 r[:original_pictures].is_a?(Array).should be_true
732 r = OriginalPicture.export(@artist)
733 r[:original_pictures].size.should eq 1
734 r[:original_pictures].first.should eq @op
736 it '原画に素材が関連付いている' do
737 r = OriginalPicture.export(@artist)
738 i = r[:original_pictures].first
739 i.resource_picture.should eq @rp
741 it '原画に実素材が関連付いている' do
742 r = OriginalPicture.export(@artist)
743 i = r[:original_pictures].first
744 i.picture.should eq @p
747 context '実データ複数のとき' do
749 @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
750 @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
751 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
752 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
753 @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
755 it 'ライセンスは配列構造になっている' do
756 r = OriginalPicture.export(@artist)
757 r[:license_groups].is_a?(Array).should be_true
760 r = OriginalPicture.export(@artist)
761 r[:license_groups].size.should eq 2
762 r[:license_groups].first.should eq @lg
763 r[:license_groups].last.should eq @lg2
765 it '原画は配列構造になっている' do
766 r = OriginalPicture.export(@artist)
767 r[:original_pictures].is_a?(Array).should be_true
770 r = OriginalPicture.export(@artist)
771 r[:original_pictures].size.should eq 2
772 r[:original_pictures].first.should eq @op
773 r[:original_pictures].last.should eq @op2
775 it '原画に素材が関連付いている' do
776 r = OriginalPicture.export(@artist)
777 i = r[:original_pictures].first
778 i.resource_picture.should eq @rp
779 i2 = r[:original_pictures].last
780 i2.resource_picture.should eq @rp2
782 it '原画に実素材が関連付いている' do
783 r = OriginalPicture.export(@artist)
784 i = r[:original_pictures].first
785 i.picture.should eq @p
786 i2 = r[:original_pictures].last
787 i2.picture.should eq @p2
792 describe 'エクスポートオプションに於いて' do
795 describe 'インポートに於いて' do
797 @imports = {:licenses => {}, :artist_id => @artist.id}
799 context '事前チェックしておく' do