1 # -*- encoding: utf-8 -*-
4 describe PanelPicture do
6 @admin = FactoryGirl.create :admin
7 @user = FactoryGirl.create( :user_yas)
8 @author = FactoryGirl.create :author, :user_id => @user.id
9 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
10 @other_user = FactoryGirl.create( :user_yas)
11 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
12 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
13 @sp = FactoryGirl.create :system_picture
14 @lg = FactoryGirl.create :license_group
15 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
16 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
17 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
18 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
19 @panel = FactoryGirl.create :panel, :author_id => @author.id
24 @pp = FactoryGirl.build :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
25 :width => @p.width, :height => @p.height
26 Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
27 Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
28 Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
31 context 'オーソドックスなデータのとき' do
33 @pp.link = 'http://test.jp/'
43 @pp.link = 'http://test.jp/aaaaa' + 'a' * 180
54 context 'panel_idを検証するとき' do
55 #ネストの保存はnilを許可しなければならないので数値チェックだけ
58 @pp.should_not be_valid
61 context 'linkを検証するとき' do
68 @pp.should_not be_valid
70 it 'url形式でないなら失敗する' do
72 @pp.should_not be_valid
78 @pp.should_not be_valid
82 @pp.should_not be_valid
96 @pp.should_not be_valid
100 @pp.should_not be_valid
111 context 'widthを検証するとき' do
114 @pp.should_not be_valid
118 @pp.should_not be_valid
122 @pp.should_not be_valid
134 Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
138 @pp.should_not be_valid
141 context 'サイズ変更許可のとき' do
144 it '実素材のサイズと違っても通る' do
145 @pp.width = @p.width-1
149 context 'サイズ変更禁止のとき' do
151 Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
153 it '実素材のサイズと違うなら失敗する' do
154 @pp.width = @p.width-1
155 @pp.should_not be_valid
158 context '縦横比変更許可のとき' do
161 it '実素材の縦横比と違っても通る' do
162 @pp.width = @p.width / 2
166 context '縦横比変更禁止のとき' do
168 Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
170 it '実素材の縦横比と違うなら失敗する' do
171 @pp.width = @p.width / 2
172 @pp.should_not be_valid
176 context 'heightを検証するとき' do
179 @pp.should_not be_valid
183 @pp.should_not be_valid
187 @pp.should_not be_valid
203 Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
207 @pp.should_not be_valid
210 context 'サイズ変更許可のとき' do
213 it '実素材のサイズと違っても通る' do
214 @pp.height = @p.height-1
218 context 'サイズ変更禁止のとき' do
220 Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
222 it '実素材のサイズと違うなら失敗する' do
223 @pp.height = @p.height-1
224 @pp.should_not be_valid
227 context '縦横比変更許可のとき' do
230 it '実素材の縦横比と違っても通る' do
231 @pp.height = @p.height / 2
235 context '縦横比変更禁止のとき' do
237 Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
239 it '実素材の縦横比と違うなら失敗する' do
240 @pp.height = @p.height / 2
241 @pp.should_not be_valid
245 context 'zを検証するとき' do
248 @pp.should_not be_valid
252 @pp.should_not be_valid
256 @pp.should_not be_valid
260 @pp.should_not be_valid
263 context 'tを検証するとき' do
266 @pp.should_not be_valid
270 @pp.should_not be_valid
274 @pp.should_not be_valid
277 context 'picture_idを検証するとき' do
280 @pp.should_not be_valid
284 @pp.should_not be_valid
286 it '存在する実素材でなければ失敗する' do
288 @pp.should_not be_valid
293 describe '文字コード検証に於いて' do
295 @pp = FactoryGirl.build :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
296 :width => @p.width, :height => @p.height
299 context 'linkを検証するとき' do
300 it 'Shift JISなら失敗する' do
301 @pp.link = "\x83G\x83r\x83]\x83D"
304 }.should raise_error(Pettanr::BadRequest)
308 context 'captionを検証するとき' do
309 it 'Shift JISなら失敗する' do
310 @pp.caption = "\x83G\x83r\x83]\x83D"
313 }.should raise_error(Pettanr::BadRequest)
318 describe '閲覧許可に於いて' do
320 @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
321 :width => @p.width, :height => @p.height
323 context 'オープンモードのとき' do
325 MagicNumber['run_mode'] = 0
327 it '自身にゲスト用ロールチェックを問い合わせしている' do
328 PanelPicture.any_instance.stub(:guest_role_check).and_return(true)
329 PanelPicture.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
330 r = @pp.visible?([@author])
332 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
333 PanelPicture.any_instance.stub(:guest_role_check).and_return(false)
334 r = @pp.visible?([@author])
338 context 'クローズドモードのとき' do
340 MagicNumber['run_mode'] = 1
342 it '自身に読者用ロールチェックを問い合わせしている' do
343 PanelPicture.any_instance.stub(:reader_role_check).and_return(true)
344 PanelPicture.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
345 r = @pp.visible?([@author])
347 it '読者用ロールチェックが失敗したとき、falseを返す' do
348 PanelPicture.any_instance.stub(:reader_role_check).and_return(false)
349 r = @pp.visible?([@author])
353 context '事前チェックする' do
355 MagicNumber['run_mode'] = 1
356 PanelPicture.any_instance.stub(:reader_role_check).and_return(true)
358 it '自身のコマに所持判定を問い合わせしている' do
359 Panel.any_instance.stub(:own?).and_return(true)
360 Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
361 r = @pp.visible?([@author])
363 it '自身のコマに閲覧許可を問い合わせしている' do
364 Panel.any_instance.stub(:own?).and_return(false)
365 Panel.any_instance.stub(:visible?).and_return(true)
366 Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
367 r = @pp.visible?([@author])
370 context 'つつがなく終わるとき' do
372 MagicNumber['run_mode'] = 1
373 Panel.any_instance.stub(:reader_role_check).and_return(true)
375 it '自分のコマのコマ絵なら許可する' do
376 Panel.any_instance.stub(:own?).and_return(true)
377 Panel.any_instance.stub(:visible?).and_return(false)
378 r = @pp.visible?([@author])
381 it '他人の非公開コマのコマ絵なら許可しない' do
382 Panel.any_instance.stub(:own?).and_return(false)
383 Panel.any_instance.stub(:visible?).and_return(false)
384 r = @pp.visible?([@author])
387 it '他人のコマのコマ絵でも公開なら許可する' do
388 Panel.any_instance.stub(:own?).and_return(false)
389 Panel.any_instance.stub(:visible?).and_return(true)
390 r = @pp.visible?([@author])
397 describe '一覧取得に於いて' do
399 @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
400 :width => @p.width, :height => @p.height
402 context 'page補正について' do
403 it '文字列から数値に変換される' do
404 PanelPicture.page('8').should eq 8
407 PanelPicture.page().should eq 1
410 PanelPicture.page('0').should eq 1
413 context 'page_size補正について' do
414 it '文字列から数値に変換される' do
415 PanelPicture.page_size('7').should eq 7
417 it 'nilの場合はPanelPicture.default_page_sizeになる' do
418 PanelPicture.page_size().should eq PanelPicture.default_page_size
420 it '0以下の場合はPanelPicture.default_page_sizeになる' do
421 PanelPicture.page_size('0').should eq PanelPicture.default_page_size
423 it 'PanelPicture.max_page_sizeを超えた場合はPanelPicture.max_page_sizeになる' do
424 PanelPicture.page_size('1000').should eq PanelPicture.max_page_size
427 context 'つつがなく終わるとき' do
428 it '一覧取得オプションを利用している' do
429 PanelPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
430 PanelPicture.should_receive(:list_opt).with(any_args).exactly(1)
431 r = PanelPicture.list
435 r = PanelPicture.list
439 #公開されたコマのコマ絵は(他人のコマ絵であっても)含んでいる
440 hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
441 npl = FactoryGirl.create :panel_picture, :panel_id => hc.id, :t => 0, :picture_id => @p.id,
442 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
443 r = PanelPicture.list
444 r.should eq [npl, @pp]
446 it '非公開のコマのコマ絵は自分のコマ絵であっても含まない' do
447 hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
448 npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
449 :width => @p.width, :height => @p.height
450 pl = PanelPicture.list
453 context 'DBに5件あって1ページの件数を2件に変えたとして' do
455 @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
456 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
457 @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
458 :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
459 @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
460 :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
461 @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
462 :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
463 PanelPicture.stub(:default_page_size).and_return(2)
466 pl = PanelPicture.list
467 pl.should have(2).items
469 it 'page=1なら末尾2件を返す' do
471 pl = PanelPicture.list(1)
472 pl.should eq [@npl5, @npl4]
474 it 'page=2なら中間2件を返す' do
475 pl = PanelPicture.list(2)
476 pl.should eq [@npl3, @npl2]
478 it 'page=3なら先頭1件を返す' do
479 pl = PanelPicture.list(3)
483 context 'DBに5件あって1ページの件数を0件に変えたとして' do
485 @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
486 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
487 @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
488 :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
489 @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
490 :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
491 @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
492 :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
493 PanelPicture.stub(:default_page_size).and_return(2)
496 r = PanelPicture.list 5, 0
497 r.should have(5).items
501 describe '一覧取得オプションに於いて' do
502 it 'includeキーを含んでいる' do
503 r = PanelPicture.list_opt
504 r.has_key?(:include).should be_true
507 r = PanelPicture.list_opt[:include]
508 r.should have(2).items
511 r = PanelPicture.list_opt[:include]
512 r.has_key?(:panel).should be_true
515 r = PanelPicture.list_opt[:include]
516 r[:panel].has_key?(:author).should be_true
519 r = PanelPicture.list_opt[:include]
520 r.has_key?(:picture).should be_true
523 r = PanelPicture.list_opt[:include]
524 r[:picture].has_key?(:artist).should be_true
526 it '実素材はライセンスを含んでいる' do
527 r = PanelPicture.list_opt[:include]
528 r[:picture].has_key?(:license).should be_true
531 describe 'json一覧出力オプションに於いて' do
533 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
534 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
535 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
536 @sbt = FactoryGirl.create :speech_balloon_template
537 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
538 @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
539 :width => @p.width, :height => @p.height
542 r = PanelPicture.list.to_json PanelPicture.list_json_opt
545 i.has_key?('panel').should be_true
548 r = PanelPicture.list.to_json PanelPicture.list_json_opt
552 s.has_key?('author').should be_true
555 r = PanelPicture.list.to_json PanelPicture.list_json_opt
558 i.has_key?('picture').should be_true
561 r = PanelPicture.list.to_json PanelPicture.list_json_opt
565 s.has_key?('artist').should be_true
567 it '実素材はライセンスを含んでいる' do
568 r = PanelPicture.list.to_json PanelPicture.list_json_opt
572 s.has_key?('license').should be_true
576 describe '自分のコマ絵一覧取得に於いて' do
578 @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
579 :width => @p.width, :height => @p.height
581 context 'つつがなく終わるとき' do
582 it '一覧取得オプションを利用している' do
583 PanelPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
584 PanelPicture.should_receive(:list_opt).with(any_args).exactly(1)
585 r = PanelPicture.mylist @author
589 pl = PanelPicture.mylist @author
593 npl = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
594 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
595 pl = PanelPicture.mylist @author
596 pl.should eq [npl, @pp]
598 it '他人のコマのコマ絵は公開コマでも含まない' do
599 hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
600 npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
601 :width => @p.width, :height => @p.height
602 pl = PanelPicture.mylist @author
605 it '自分のコマのコマ絵は非公開コマでも含んでいる' do
606 pl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
607 ni = FactoryGirl.create :panel_picture, :panel_id => pl.id, :picture_id => @p.id,
608 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
609 r = PanelPicture.mylist @author
610 r.should eq [ni, @pp]
612 context 'DBに5件あって1ページの件数を2件に変えたとして' do
614 @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
615 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
616 @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
617 :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
618 @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
619 :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
620 @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
621 :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
624 r = PanelPicture.mylist @author, 1, 2
625 r.should have(2).items
627 it 'page=1なら末尾2件を返す' do
629 r = PanelPicture.mylist(@author, 1, 2)
630 r.should eq [@npl5, @npl4]
632 it 'page=2なら中間2件を返す' do
633 r = PanelPicture.mylist(@author, 2, 2)
634 r.should eq [@npl3, @npl2]
636 it 'page=3なら先頭1件を返す' do
637 r = PanelPicture.mylist(@author, 3, 2)
641 context 'DBに5件あって1ページの件数を0件に変えたとして' do
643 @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
644 :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
645 @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
646 :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
647 @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
648 :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
649 @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
650 :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
651 Author.stub(:default_panel_picture_page_size).and_return(2)
654 r = PanelPicture.mylist @author, 5, 0
655 r.should have(5).items
660 describe '単体取得に於いて' do
662 @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
663 :width => @p.width, :height => @p.height
665 context 'つつがなく終わるとき' do
666 it '単体取得オプションを利用している' do
667 PanelPicture.stub(:show_opt).with(any_args).and_return({:include => :panel})
668 PanelPicture.should_receive(:show_opt).with(any_args).exactly(1)
669 r = PanelPicture.show @pp.id, @author
671 it '閲覧許可を問い合わせている' do
672 PanelPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
673 PanelPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
674 r = PanelPicture.show @pp.id, @author
678 PanelPicture.any_instance.stub(:visible?).and_return(true)
679 pl = PanelPicture.show @pp.id, @author
682 context '閲覧許可が出なかったとき' do
683 it '403Forbidden例外を返す' do
684 PanelPicture.any_instance.stub(:visible?).and_return(false)
686 PanelPicture.show @pp.id, @author
687 }.should raise_error(ActiveRecord::Forbidden)
690 context '存在しないコマ絵を開こうとしたとき' do
691 it '404RecordNotFound例外を返す' do
693 PanelPicture.show 110, @author
694 }.should raise_error(ActiveRecord::RecordNotFound)
698 describe '単体取得オプションに於いて' do
699 it 'includeキーを含んでいる' do
700 r = PanelPicture.show_opt
701 r.has_key?(:include).should be_true
704 r = PanelPicture.show_opt[:include]
705 r.should have(2).items
708 r = PanelPicture.show_opt[:include]
709 r.has_key?(:panel).should be_true
712 r = PanelPicture.show_opt[:include]
713 r[:panel].has_key?(:author).should be_true
716 r = PanelPicture.show_opt[:include]
717 r.has_key?(:picture).should be_true
720 r = PanelPicture.show_opt[:include]
721 r[:picture].has_key?(:artist).should be_true
723 it '実素材はライセンスを含んでいる' do
724 r = PanelPicture.show_opt[:include]
725 r[:picture].has_key?(:license).should be_true
728 describe 'json単体出力オプションに於いて' do
730 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
731 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
732 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
733 @sbt = FactoryGirl.create :speech_balloon_template
734 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
735 @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
736 :width => @p.width, :height => @p.height
739 r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
742 i.has_key?('panel').should be_true
745 r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
749 s.has_key?('author').should be_true
752 r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
755 i.has_key?('picture').should be_true
758 r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
762 s.has_key?('artist').should be_true
764 it '実素材はライセンスを含んでいる' do
765 r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
769 s.has_key?('license').should be_true