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
160 op = FactoryGirl.create :original_picture, :artist_id => @artist.id
161 op.own?(@artist).should == true
164 op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
165 op.own?(@artist).should == false
169 describe '閲覧許可に於いて' do
171 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
173 it '自分の原画を見るときは許可する' do
174 OriginalPicture.any_instance.stub(:own?).and_return(true)
175 r = @op.visible?(@artist)
179 OriginalPicture.any_instance.stub(:own?).and_return(false)
180 r = @op.visible?(@artist)
185 describe 'ファイル名に於いて' do
187 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
189 it 'id+拡張子のフォーマットで返す' do
191 r.should eq "#{@op.id}.png"
195 describe 'MimeTypeに於いて' do
197 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
199 it 'image/拡張子のフォーマットで返す' do
201 r.should eq "image/png"
205 describe 'ファイルのurlに於いて' do
207 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
208 OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
210 it 'ファイル名取得を依頼している' do
211 OriginalPicture.any_instance.should_receive(:filename).exactly(1)
214 it '/original_pictures/3.gifのフォーマットで返す' do
216 r.should eq "/original_pictures/3.gif"
220 describe '画像タグオプションに於いて' do
222 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
223 OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
224 ResourcePicture.stub(:fix_size_both).with(any_args).and_return([40, 30])
226 it 'サムネイル画像の幅高さ取得を依頼している' do
227 ResourcePicture.should_receive(:fix_size_both).with(any_args).exactly(1)
232 r.is_a?(Hash).should be_true
236 r.has_key?(:src).should be_true
237 r[:src].should eq '/original_pictures/3.gif'
239 it 'widthキーを含んでいる' do
241 r.has_key?(:width).should be_true
242 r[:width].should eq 40
244 it 'heightキーを含んでいる' do
246 r.has_key?(:height).should be_true
247 r[:height].should eq 30
251 describe '一覧取得に於いて' do
253 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
255 context 'page補正について' do
256 it '文字列から数値に変換される' do
257 OriginalPicture.page('8').should eq 8
260 OriginalPicture.page().should eq 1
263 OriginalPicture.page('0').should eq 1
266 context 'page_size補正について' do
267 it '文字列から数値に変換される' do
268 OriginalPicture.page_size('7').should eq 7
270 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
271 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
273 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
274 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
276 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
277 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
281 describe 'list関連テーブルプションに於いて' do
282 it 'includeキーを含んでいる' do
283 r = OriginalPicture.list_opt
284 r.has_key?(:include).should be_true
287 r = OriginalPicture.list_opt[:include]
288 r.should have(2).items
291 r = OriginalPicture.list_opt[:include]
292 r.has_key?(:resource_picture).should be_true
295 r = OriginalPicture.list_opt[:include]
296 r.has_key?(:pictures).should be_true
299 describe 'json一覧出力オプションに於いて' do
300 it 'includeキーを含んでいる' do
301 r = OriginalPicture.list_json_opt
302 r.has_key?(:include).should be_true
305 r = OriginalPicture.list_json_opt[:include]
306 r.should have(2).items
309 r = OriginalPicture.list_json_opt[:include]
310 r.has_key?(:resource_picture).should be_true
313 r = OriginalPicture.list_json_opt[:include]
314 r.has_key?(:pictures).should be_true
318 describe '自分のコミック一覧取得に於いて' do
320 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
322 context 'つつがなく終わるとき' do
323 it '一覧取得オプションを利用している' do
324 OriginalPicture.stub(:list_opt).with(any_args).and_return({})
325 OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
326 r = OriginalPicture.mylist @artist
330 r = OriginalPicture.mylist @artist
334 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
335 r = OriginalPicture.mylist @artist
336 r.should eq [nc, @op]
338 it '他人のコミックはxxxでも含まない' do
339 nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
340 r = OriginalPicture.mylist @artist
343 it '自分のコミックはxxxでも含んでいる' do
344 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
345 r = OriginalPicture.mylist @artist
346 r.should eq [nc, @op]
348 context 'DBに5件あって1ページの件数を2件に変えたとして' do
350 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
351 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
352 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
353 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
356 r = OriginalPicture.mylist @artist, 1, 2
357 r.should have(2).items
359 it 'page=1なら末尾2件を返す' do
361 r = OriginalPicture.mylist @artist, 1, 2
362 r.should eq [@op5, @op4]
364 it 'page=2なら中間2件を返す' do
365 r = OriginalPicture.mylist @artist, 2, 2
366 r.should eq [@op3, @op2]
368 it 'page=3なら先頭1件を返す' do
369 r = OriginalPicture.mylist @artist, 3, 2
373 context 'DBに5件あって1ページの件数を0件に変えたとして' do
375 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
376 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
377 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
378 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
381 r = OriginalPicture.mylist @artist, 5, 0
382 r.should have(5).items
387 describe '更新履歴一覧取得に於いて' do
389 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
390 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
391 :original_picture_id => @op.id
392 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
398 it '他の原画の実素材は含んでいない' do
399 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
400 :original_picture_id => @op2.id
404 it 'revisionで並んでいる' do
405 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
406 :original_picture_id => @op.id
408 r.should eq [@p2, @p]
412 describe '単体取得に於いて' do
414 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
416 context 'つつがなく終わるとき' do
417 it '単体取得オプションを利用している' do
418 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
419 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
420 r = OriginalPicture.show @op.id, @artist
422 it '閲覧許可を問い合わせている' do
423 OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
424 OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
425 r = OriginalPicture.show @op.id, @artist
429 pic = OriginalPicture.show @op.id, @artist
432 context '他人の原画を開こうとしたとき' do
433 it '403Forbidden例外を返す' do
434 OriginalPicture.any_instance.stub(:visible?).and_return(false)
436 pic = OriginalPicture.show @op.id, @other_artist
437 }.should raise_error(ActiveRecord::Forbidden)
440 context '存在しない原画を開こうとしたとき' do
441 it '404RecordNotFound例外を返す' do
443 pic = OriginalPicture.show 0, @artist
444 }.should raise_error(ActiveRecord::RecordNotFound)
448 describe '編集取得に於いて' do
450 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
452 context 'つつがなく終わるとき' do
453 it '単体取得オプションを利用している' do
454 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
455 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
456 r = OriginalPicture.edit @op.id, @artist
458 it '所持判定を問い合わせている' do
459 OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
460 OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
461 r = OriginalPicture.edit @op.id, @artist
465 pic = OriginalPicture.edit @op.id, @artist
468 context '他人の原画を開こうとしたとき' do
469 it '403Forbidden例外を返す' do
470 OriginalPicture.any_instance.stub(:own?).and_return(false)
472 r = OriginalPicture.edit @op.id, @other_artist
473 }.should raise_error(ActiveRecord::Forbidden)
476 context '存在しない原画を開こうとしたとき' do
477 it '404RecordNotFound例外を返す' do
479 r = OriginalPicture.edit 0, @artist
480 }.should raise_error(ActiveRecord::RecordNotFound)
484 describe '単体出力オプションに於いて' do
485 it 'includeキーを含んでいる' do
486 r = OriginalPicture.show_opt
487 r.has_key?(:include).should be_true
490 r = OriginalPicture.show_opt[:include]
491 r.should have(2).items
494 r = OriginalPicture.show_opt[:include]
495 r.has_key?(:resource_picture).should be_true
498 r = OriginalPicture.show_opt[:include]
499 r.has_key?(:pictures).should be_true
502 describe 'json単体出力オプションに於いて' do
503 it 'includeキーを含んでいる' do
504 r = OriginalPicture.show_json_opt
505 r.has_key?(:include).should be_true
508 r = OriginalPicture.show_json_opt[:include]
509 r.should have(2).items
512 r = OriginalPicture.show_json_opt[:include]
513 r.has_key?(:resource_picture).should be_true
516 r = OriginalPicture.show_json_opt[:include]
517 r.has_key?(:pictures).should be_true
520 describe '作成・更新に於いて' do
522 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
523 @imager = ImagerTest.load "abc\ndef\nghi"
527 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
528 #それで外部のメソッド呼び出しだけに着目してテストする。
529 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
530 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
532 it '自身に属性をセットしている' do
533 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
537 OriginalPicture.any_instance.should_receive(:save).exactly(1)
540 it 'PictureIoに画像データの保存を依頼している' do
541 PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
545 context 'つつがなく終わるとき' do
548 it '自身に属性をセットしている' do
550 @op.width.should eq 3
551 @op.height.should eq 3
552 @op.filesize.should eq 9
554 it '原画モデルが作成されている' do
557 }.should change OriginalPicture, :count
561 OriginalPicture.find(@op).should_not be_nil
564 @op.store(@imager).should eq true
567 #以下から例外ケース。処理先頭から失敗させていく
568 context 'imagerが初期化に失敗したとき' do
572 @op.store(false).should be_false
574 it '自身の保存は呼ばれていない' do
575 OriginalPicture.any_instance.should_not_receive(:save)
578 it '全体エラーメッセージがセットされている' do
581 }.should change(@op.errors[:base], :count)
584 context '自身の保存に失敗したとき' do
586 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
589 @op.store(@imager).should be_false
593 @op.should be_a_new OriginalPicture
595 it '原画の保存は呼ばれていない' do
596 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
599 context '原画の保存に失敗したとき' do
601 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
602 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
605 @op.store(@imager).should be_false
609 @op.should be_a_new OriginalPicture
611 it '全体エラーメッセージがセットされている' do
614 }.should change(@op.errors[:base], :count)
620 describe 'エクスポートに於いて' do
622 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
623 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
624 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
626 @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
628 context 'つつがなく終わるとき' do
629 it 'ライセンスグループに依頼してリストを取得している' do
630 LicenseGroup.stub(:list).with(any_args).and_return([@lg])
631 LicenseGroup.should_receive(:list).with(any_args).exactly(1)
632 r = OriginalPicture.export(@artist)
634 it '原画に依頼してリストを取得している' do
635 OriginalPicture.stub(:list).with(any_args).and_return([@op])
636 OriginalPicture.should_receive(:list).with(any_args).exactly(1)
637 r = OriginalPicture.export(@artist)
640 r = OriginalPicture.export(@artist)
641 r.is_a?(Hash).should be_true
643 it 'Hashはライセンスグループを含んでいる' do
644 r = OriginalPicture.export(@artist)
645 r.include?(:license_groups).should be_true
647 it 'Hashは原画を含んでいる' do
648 r = OriginalPicture.export(@artist)
649 r.include?(:original_pictures).should be_true
651 #素材がライセンスされていないケースもある
652 it 'Hashの原画は素材を含んでいる' do
653 r = OriginalPicture.export(@artist)
654 r[:original_pictures].first.resource_picture.should_not be_nil
656 it 'Hashの原画は実素材を含んでいる' do
657 r = OriginalPicture.export(@artist)
658 r[:original_pictures].first.pictures.should be_nil
661 context '実データ単体のとき' do
662 it 'ライセンスは配列構造になっている' do
663 r = OriginalPicture.export(@artist)
664 r[:license_groups].is_a?(Array).should be_true
667 r = OriginalPicture.export(@artist)
668 r[:license_groups].size.should eq 1
669 r[:license_groups].first.should eq @lg
671 it '原画は配列構造になっている' do
672 r = OriginalPicture.export(@artist)
673 r[:original_pictures].is_a?(Array).should be_true
676 r = OriginalPicture.export(@artist)
677 r[:original_pictures].size.should eq 1
678 r[:original_pictures].first.should eq @op
680 it '原画に素材が関連付いている' do
681 r = OriginalPicture.export(@artist)
682 i = r[:original_pictures].first
683 i.resource_picture.should eq @rp
685 it '原画に実素材が関連付いている' do
686 r = OriginalPicture.export(@artist)
687 i = r[:original_pictures].first
688 i.picture.should eq @p
691 context '実データ複数のとき' do
693 @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
694 @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
695 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
696 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
697 @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
699 it 'ライセンスは配列構造になっている' do
700 r = OriginalPicture.export(@artist)
701 r[:license_groups].is_a?(Array).should be_true
704 r = OriginalPicture.export(@artist)
705 r[:license_groups].size.should eq 2
706 r[:license_groups].first.should eq @lg
707 r[:license_groups].last.should eq @lg2
709 it '原画は配列構造になっている' do
710 r = OriginalPicture.export(@artist)
711 r[:original_pictures].is_a?(Array).should be_true
714 r = OriginalPicture.export(@artist)
715 r[:original_pictures].size.should eq 2
716 r[:original_pictures].first.should eq @op
717 r[:original_pictures].last.should eq @op2
719 it '原画に素材が関連付いている' do
720 r = OriginalPicture.export(@artist)
721 i = r[:original_pictures].first
722 i.resource_picture.should eq @rp
723 i2 = r[:original_pictures].last
724 i2.resource_picture.should eq @rp2
726 it '原画に実素材が関連付いている' do
727 r = OriginalPicture.export(@artist)
728 i = r[:original_pictures].first
729 i.picture.should eq @p
730 i2 = r[:original_pictures].last
731 i2.picture.should eq @p2
736 describe 'エクスポートオプションに於いて' do
739 describe 'インポートに於いて' do
741 @imports = {:licenses => {}, :artist_id => @artist.id}
743 context '事前チェックしておく' do