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 PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
226 it 'サムネイル画像の幅高さ取得を依頼している' do
227 PettanImager.should_receive(:thumbnail_size).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 '一覧取得オプションに於いて' 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
301 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
302 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
303 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
\r
304 @sbt = FactoryGirl.create :speech_balloon_template
\r
305 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
306 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
307 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
310 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
313 i.has_key?('resource_picture').should be_true
316 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
319 i.has_key?('pictures').should be_true
323 describe '自分のコミック一覧取得に於いて' do
325 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
327 context 'つつがなく終わるとき' do
328 it '一覧取得オプションを利用している' do
329 OriginalPicture.stub(:list_opt).with(any_args).and_return({})
330 OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
331 r = OriginalPicture.mylist @artist
335 r = OriginalPicture.mylist @artist
339 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
340 r = OriginalPicture.mylist @artist
341 r.should eq [nc, @op]
343 it '他人のコミックはxxxでも含まない' do
344 nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
345 r = OriginalPicture.mylist @artist
348 it '自分のコミックはxxxでも含んでいる' do
349 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
350 r = OriginalPicture.mylist @artist
351 r.should eq [nc, @op]
353 context 'DBに5件あって1ページの件数を2件に変えたとして' do
355 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
356 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
357 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
358 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
361 r = OriginalPicture.mylist @artist, 1, 2
362 r.should have(2).items
364 it 'page=1なら末尾2件を返す' do
366 r = OriginalPicture.mylist @artist, 1, 2
367 r.should eq [@op5, @op4]
369 it 'page=2なら中間2件を返す' do
370 r = OriginalPicture.mylist @artist, 2, 2
371 r.should eq [@op3, @op2]
373 it 'page=3なら先頭1件を返す' do
374 r = OriginalPicture.mylist @artist, 3, 2
378 context 'DBに5件あって1ページの件数を0件に変えたとして' do
380 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
381 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
382 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
383 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
386 r = OriginalPicture.mylist @artist, 5, 0
387 r.should have(5).items
392 describe '更新履歴一覧取得に於いて' do
394 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
395 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
396 :original_picture_id => @op.id
397 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
403 it '他の原画の実素材は含んでいない' do
404 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
405 :original_picture_id => @op2.id
409 it 'revisionで並んでいる' do
410 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
411 :original_picture_id => @op.id
413 r.should eq [@p2, @p]
417 describe '単体取得に於いて' do
419 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
421 context 'つつがなく終わるとき' do
422 it '単体取得オプションを利用している' do
423 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
424 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
425 r = OriginalPicture.show @op.id, @artist
427 it '閲覧許可を問い合わせている' do
428 OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
429 OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
430 r = OriginalPicture.show @op.id, @artist
434 pic = OriginalPicture.show @op.id, @artist
437 context '他人の原画を開こうとしたとき' do
438 it '403Forbidden例外を返す' do
439 OriginalPicture.any_instance.stub(:visible?).and_return(false)
441 pic = OriginalPicture.show @op.id, @other_artist
442 }.should raise_error(ActiveRecord::Forbidden)
445 context '存在しない原画を開こうとしたとき' do
446 it '404RecordNotFound例外を返す' do
448 pic = OriginalPicture.show 0, @artist
449 }.should raise_error(ActiveRecord::RecordNotFound)
453 describe '編集取得に於いて' do
455 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
457 context 'つつがなく終わるとき' do
458 it '単体取得オプションを利用している' do
459 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
460 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
461 r = OriginalPicture.edit @op.id, @artist
463 it '所持判定を問い合わせている' do
464 OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
465 OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
466 r = OriginalPicture.edit @op.id, @artist
470 pic = OriginalPicture.edit @op.id, @artist
473 context '他人の原画を開こうとしたとき' do
474 it '403Forbidden例外を返す' do
475 OriginalPicture.any_instance.stub(:own?).and_return(false)
477 r = OriginalPicture.edit @op.id, @other_artist
478 }.should raise_error(ActiveRecord::Forbidden)
481 context '存在しない原画を開こうとしたとき' do
482 it '404RecordNotFound例外を返す' do
484 r = OriginalPicture.edit 0, @artist
485 }.should raise_error(ActiveRecord::RecordNotFound)
489 describe '単体取得オプションに於いて' do
490 it 'includeキーを含んでいる' do
491 r = OriginalPicture.show_opt
492 r.has_key?(:include).should be_true
495 r = OriginalPicture.show_opt[:include]
496 r.should have(2).items
499 r = OriginalPicture.show_opt[:include]
500 r.has_key?(:resource_picture).should be_true
503 r = OriginalPicture.show_opt[:include]
504 r.has_key?(:pictures).should be_true
507 describe 'json単体出力オプションに於いて' do
509 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
510 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
511 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
\r
512 @sbt = FactoryGirl.create :speech_balloon_template
\r
513 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
514 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
515 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
518 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
521 i.has_key?('resource_picture').should be_true
524 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
527 i.has_key?('pictures').should be_true
530 describe '作成・更新に於いて' do
532 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
533 @imager = ImagerTest.load "abc\ndef\nghi"
537 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
538 #それで外部のメソッド呼び出しだけに着目してテストする。
539 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
540 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
542 it '自身に属性をセットしている' do
543 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
547 OriginalPicture.any_instance.should_receive(:save).exactly(1)
550 it 'PictureIoに画像データの保存を依頼している' do
551 PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
555 context 'つつがなく終わるとき' do
558 it '自身に属性をセットしている' do
560 @op.width.should eq 3
561 @op.height.should eq 3
562 @op.filesize.should eq 9
564 it '原画モデルが作成されている' do
567 }.should change OriginalPicture, :count
571 OriginalPicture.find(@op).should_not be_nil
574 @op.store(@imager).should eq true
577 #以下から例外ケース。処理先頭から失敗させていく
578 context 'imagerが初期化に失敗したとき' do
582 @op.store(false).should be_false
584 it '自身の保存は呼ばれていない' do
585 OriginalPicture.any_instance.should_not_receive(:save)
588 it '全体エラーメッセージがセットされている' do
591 }.should change(@op.errors[:base], :count)
594 context '自身の保存に失敗したとき' do
596 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
599 @op.store(@imager).should be_false
603 @op.should be_a_new OriginalPicture
605 it '原画の保存は呼ばれていない' do
606 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
609 context '原画の保存に失敗したとき' do
611 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
612 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
615 @op.store(@imager).should be_false
619 @op.should be_a_new OriginalPicture
621 it '全体エラーメッセージがセットされている' do
624 }.should change(@op.errors[:base], :count)
630 describe 'エクスポートに於いて' do
632 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
633 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
634 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
636 @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
638 context 'つつがなく終わるとき' do
639 it 'ライセンスグループに依頼してリストを取得している' do
640 LicenseGroup.stub(:list).with(any_args).and_return([@lg])
641 LicenseGroup.should_receive(:list).with(any_args).exactly(1)
642 r = OriginalPicture.export(@artist)
644 it '原画に依頼してリストを取得している' do
645 OriginalPicture.stub(:list).with(any_args).and_return([@op])
646 OriginalPicture.should_receive(:list).with(any_args).exactly(1)
647 r = OriginalPicture.export(@artist)
650 r = OriginalPicture.export(@artist)
651 r.is_a?(Hash).should be_true
653 it 'Hashはライセンスグループを含んでいる' do
654 r = OriginalPicture.export(@artist)
655 r.include?(:license_groups).should be_true
657 it 'Hashは原画を含んでいる' do
658 r = OriginalPicture.export(@artist)
659 r.include?(:original_pictures).should be_true
661 #素材がライセンスされていないケースもある
662 it 'Hashの原画は素材を含んでいる' do
663 r = OriginalPicture.export(@artist)
664 r[:original_pictures].first.resource_picture.should_not be_nil
666 it 'Hashの原画は実素材を含んでいる' do
667 r = OriginalPicture.export(@artist)
668 r[:original_pictures].first.pictures.should be_nil
671 context '実データ単体のとき' do
672 it 'ライセンスは配列構造になっている' do
673 r = OriginalPicture.export(@artist)
674 r[:license_groups].is_a?(Array).should be_true
677 r = OriginalPicture.export(@artist)
678 r[:license_groups].size.should eq 1
679 r[:license_groups].first.should eq @lg
681 it '原画は配列構造になっている' do
682 r = OriginalPicture.export(@artist)
683 r[:original_pictures].is_a?(Array).should be_true
686 r = OriginalPicture.export(@artist)
687 r[:original_pictures].size.should eq 1
688 r[:original_pictures].first.should eq @op
690 it '原画に素材が関連付いている' do
691 r = OriginalPicture.export(@artist)
692 i = r[:original_pictures].first
693 i.resource_picture.should eq @rp
695 it '原画に実素材が関連付いている' do
696 r = OriginalPicture.export(@artist)
697 i = r[:original_pictures].first
698 i.picture.should eq @p
701 context '実データ複数のとき' do
703 @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
704 @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
705 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
706 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
707 @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
709 it 'ライセンスは配列構造になっている' do
710 r = OriginalPicture.export(@artist)
711 r[:license_groups].is_a?(Array).should be_true
714 r = OriginalPicture.export(@artist)
715 r[:license_groups].size.should eq 2
716 r[:license_groups].first.should eq @lg
717 r[:license_groups].last.should eq @lg2
719 it '原画は配列構造になっている' do
720 r = OriginalPicture.export(@artist)
721 r[:original_pictures].is_a?(Array).should be_true
724 r = OriginalPicture.export(@artist)
725 r[:original_pictures].size.should eq 2
726 r[:original_pictures].first.should eq @op
727 r[:original_pictures].last.should eq @op2
729 it '原画に素材が関連付いている' do
730 r = OriginalPicture.export(@artist)
731 i = r[:original_pictures].first
732 i.resource_picture.should eq @rp
733 i2 = r[:original_pictures].last
734 i2.resource_picture.should eq @rp2
736 it '原画に実素材が関連付いている' do
737 r = OriginalPicture.export(@artist)
738 i = r[:original_pictures].first
739 i.picture.should eq @p
740 i2 = r[:original_pictures].last
741 i2.picture.should eq @p2
746 describe 'エクスポートオプションに於いて' do
749 describe 'インポートに於いて' do
751 @imports = {:licenses => {}, :artist_id => @artist.id}
753 context '事前チェックしておく' do