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
\r
174 OriginalPicture.any_instance.stub(:own?).and_return(true)
\r
175 r = @op.visible?(@artist)
178 it '他人の原画なら許可しない' do
\r
179 OriginalPicture.any_instance.stub(:own?).and_return(false)
\r
180 r = @op.visible?(@artist)
185 #大文字混じりは検証ではねているので、無用になっている
186 describe '拡張子に於いて' do
188 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id, :ext => 'jpeg'
190 it '画像ファイル拡張子を英数小文字で返す' do
\r
196 describe 'ファイル名に於いて' do
198 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
199 OriginalPicture.any_instance.stub(:dext).and_return('gif')
202 OriginalPicture.any_instance.should_receive(:dext).exactly(1)
205 it 'id+拡張子のフォーマットで返す' do
\r
207 r.should eq "#{@op.id}.gif"
211 describe 'MimeTypeに於いて' do
213 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
214 OriginalPicture.any_instance.stub(:dext).and_return('gif')
217 OriginalPicture.any_instance.should_receive(:dext).exactly(1)
220 it 'image/拡張子のフォーマットで返す' do
\r
222 r.should eq "image/gif"
226 describe 'ファイルのurlに於いて' do
228 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
229 OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
231 it 'ファイル名取得を依頼している' do
232 OriginalPicture.any_instance.should_receive(:filename).exactly(1)
235 it '/original_pictures/3.gifのフォーマットで返す' do
\r
237 r.should eq "/original_pictures/3.gif"
241 describe '画像タグオプションに於いて' do
243 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
244 OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
245 ResourcePicture.stub(:fix_size_both).with(any_args).and_return([40, 30])
247 it 'サムネイル画像の幅高さ取得を依頼している' do
248 ResourcePicture.should_receive(:fix_size_both).with(any_args).exactly(1)
251 it '戻り値はHashで返す' do
\r
253 r.is_a?(Hash).should be_true
257 r.has_key?(:src).should be_true
258 r[:src].should eq '/original_pictures/3.gif'
260 it 'widthキーを含んでいる' do
262 r.has_key?(:width).should be_true
263 r[:width].should eq 40
265 it 'heightキーを含んでいる' do
267 r.has_key?(:height).should be_true
268 r[:height].should eq 30
272 describe '一覧取得に於いて' do
274 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
276 context 'page補正について' do
277 it '文字列から数値に変換される' do
278 OriginalPicture.page('8').should eq 8
281 OriginalPicture.page().should eq 1
284 OriginalPicture.page('0').should eq 1
287 context 'page_size補正について' do
288 it '文字列から数値に変換される' do
289 OriginalPicture.page_size('7').should eq 7
291 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
292 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
294 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
295 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
297 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
298 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
302 describe 'list関連テーブルプションに於いて' do
303 it 'includeキーを含んでいる' do
304 r = OriginalPicture.list_opt
305 r.has_key?(:include).should be_true
308 r = OriginalPicture.list_opt[:include]
309 r.should have(2).items
312 r = OriginalPicture.list_opt[:include]
313 r.has_key?(:resource_picture).should be_true
316 r = OriginalPicture.list_opt[:include]
317 r.has_key?(:pictures).should be_true
320 describe 'json一覧出力オプションに於いて' do
321 it 'includeキーを含んでいる' do
322 r = OriginalPicture.list_json_opt
323 r.has_key?(:include).should be_true
326 r = OriginalPicture.list_json_opt[:include]
327 r.should have(2).items
330 r = OriginalPicture.list_json_opt[:include]
331 r.has_key?(:resource_picture).should be_true
334 r = OriginalPicture.list_json_opt[:include]
335 r.has_key?(:pictures).should be_true
339 describe '自分のコミック一覧取得に於いて' do
341 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
343 context 'つつがなく終わるとき' do
\r
344 it '一覧取得オプションを利用している' do
\r
345 OriginalPicture.stub(:list_opt).with(any_args).and_return({})
\r
346 OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
\r
347 r = OriginalPicture.mylist @artist
351 r = OriginalPicture.mylist @artist
355 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
356 r = OriginalPicture.mylist @artist
357 r.should eq [nc, @op]
359 it '他人のコミックはxxxでも含まない' do
360 nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
361 r = OriginalPicture.mylist @artist
364 it '自分のコミックはxxxでも含んでいる' do
365 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
366 r = OriginalPicture.mylist @artist
367 r.should eq [nc, @op]
369 context 'DBに5件あって1ページの件数を2件に変えたとして' do
371 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
372 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
373 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
374 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
377 r = OriginalPicture.mylist @artist, 1, 2
378 r.should have(2).items
380 it 'page=1なら末尾2件を返す' do
382 r = OriginalPicture.mylist @artist, 1, 2
383 r.should eq [@op5, @op4]
385 it 'page=2なら中間2件を返す' do
386 r = OriginalPicture.mylist @artist, 2, 2
387 r.should eq [@op3, @op2]
389 it 'page=3なら先頭1件を返す' do
390 r = OriginalPicture.mylist @artist, 3, 2
394 context 'DBに5件あって1ページの件数を0件に変えたとして' do
396 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
397 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
398 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
399 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
402 r = OriginalPicture.mylist @artist, 5, 0
403 r.should have(5).items
408 describe '更新履歴一覧取得に於いて' do
410 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
411 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
412 :original_picture_id => @op.id
413 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
419 it '他の原画の実素材は含んでいない' do
420 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
421 :original_picture_id => @op2.id
425 it 'revisionで並んでいる' do
426 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
427 :original_picture_id => @op.id
429 r.should eq [@p2, @p]
433 describe '単体取得に於いて' do
435 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
437 context 'つつがなく終わるとき' do
\r
438 it '単体取得オプションを利用している' do
\r
439 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
\r
440 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
\r
441 r = OriginalPicture.show @op.id, @artist
443 it '閲覧許可を問い合わせている' do
\r
444 OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
\r
445 OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
\r
446 r = OriginalPicture.show @op.id, @artist
450 pic = OriginalPicture.show @op.id, @artist
453 context '他人の原画を開こうとしたとき' do
454 it '403Forbidden例外を返す' do
455 OriginalPicture.any_instance.stub(:visible?).and_return(false)
\r
457 pic = OriginalPicture.show @op.id, @other_artist
458 }.should raise_error(ActiveRecord::Forbidden)
461 context '存在しない原画を開こうとしたとき' do
462 it '404RecordNotFound例外を返す' do
464 pic = OriginalPicture.show 0, @artist
465 }.should raise_error(ActiveRecord::RecordNotFound)
469 describe '編集取得に於いて' do
471 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
473 context 'つつがなく終わるとき' do
\r
474 it '単体取得オプションを利用している' do
\r
475 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
\r
476 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
\r
477 r = OriginalPicture.edit @op.id, @artist
479 it '所持判定を問い合わせている' do
\r
480 OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
\r
481 OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
\r
482 r = OriginalPicture.edit @op.id, @artist
486 pic = OriginalPicture.edit @op.id, @artist
489 context '他人の原画を開こうとしたとき' do
490 it '403Forbidden例外を返す' do
491 OriginalPicture.any_instance.stub(:own?).and_return(false)
493 r = OriginalPicture.edit @op.id, @other_artist
494 }.should raise_error(ActiveRecord::Forbidden)
497 context '存在しない原画を開こうとしたとき' do
498 it '404RecordNotFound例外を返す' do
500 r = OriginalPicture.edit 0, @artist
501 }.should raise_error(ActiveRecord::RecordNotFound)
505 describe '単体出力オプションに於いて' do
506 it 'includeキーを含んでいる' do
507 r = OriginalPicture.show_opt
508 r.has_key?(:include).should be_true
511 r = OriginalPicture.show_opt[:include]
512 r.should have(2).items
515 r = OriginalPicture.show_opt[:include]
516 r.has_key?(:resource_picture).should be_true
519 r = OriginalPicture.show_opt[:include]
520 r.has_key?(:pictures).should be_true
523 describe 'json単体出力オプションに於いて' do
524 it 'includeキーを含んでいる' do
525 r = OriginalPicture.show_json_opt
526 r.has_key?(:include).should be_true
529 r = OriginalPicture.show_json_opt[:include]
530 r.should have(2).items
533 r = OriginalPicture.show_json_opt[:include]
534 r.has_key?(:resource_picture).should be_true
537 r = OriginalPicture.show_json_opt[:include]
538 r.has_key?(:pictures).should be_true
541 describe 'RMagick変換に於いて' do
543 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
545 context 'つつがなく終わるとき' do
546 it '画像データをオブジェクト化している' do
547 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
548 @op.data_to_mgk('mgkbin').should eq 'mgkobj'
553 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
554 @op.data_to_mgk('mgkbin').should be_false
556 it '全体エラーがセットされている' do
557 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
559 @op.data_to_mgk('mgkbin')
560 }.should change(@op.errors[:base], :count)
564 describe '作成・更新に於いて' do
566 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
567 @bd = File.open(Rails.root + 'spec/StandardA.gif', 'rb').read
571 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
572 #それで外部のメソッド呼び出しだけに着目してテストする。
573 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
574 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
576 it 'RMagick変換を依頼している' do
577 OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
578 @op.store @bd, @artist
580 it '自身に属性をセットしている' do
581 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
582 @op.store @bd, @artist
585 OriginalPicture.any_instance.should_receive(:save).exactly(1)
586 @op.store @bd, @artist
588 it 'PictureIoに画像データの保存を依頼している' do
589 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
590 @op.store @bd, @artist
593 context 'つつがなく終わるとき' do
595 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
596 # OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
597 # PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
599 it '自身に属性をセットしている' do
601 @op.store @bd, @artist
602 }.should change @op, :filesize
604 it '原画モデルが作成されている' do
606 @op.store @bd, @artist
607 }.should change OriginalPicture, :count
610 @op.store @bd, @artist
611 OriginalPicture.find(@op).should_not be_nil
614 @op.store(@bd, @artist).should eq true
617 #以下から例外ケース。処理先頭から失敗させていく
618 context 'RMagick変換が失敗したとき' do
620 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
623 @op.store(@bd, @artist).should be_false
625 it '自身の保存は呼ばれていない' do
626 OriginalPicture.any_instance.should_not_receive(:save)
627 @op.store(@bd, @artist)
630 context '自身の保存に失敗したとき' do
632 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
635 @op.store(@bd, @artist).should be_false
638 @op.store(@bd, @artist)
639 @op.should be_a_new OriginalPicture
641 it '原画の保存は呼ばれていない' do
642 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
645 context '原画の保存に失敗したとき' do
647 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
648 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
651 @op.store(@bd, @artist).should be_false
654 @op.store(@bd, @artist)
655 @op.should be_a_new OriginalPicture
657 it '対象素材オブジェクトの保存は呼ばれていない' do
658 ResourcePicture.any_instance.should_not_receive(:save)
659 @op.store(@bd, @artist)
661 it '全体エラーメッセージがセットされている' do
663 @op.store(@bd, @artist)
664 }.should change(@op.errors[:base], :count)