1 # -*- encoding: utf-8 -*-
7 @admin = FactoryGirl.create :admin
8 @demand_user = FactoryGirl.create :demand_user
9 @sp = FactoryGirl.create :system_picture
10 @lg = FactoryGirl.create :license_group
11 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
12 @user = FactoryGirl.create( :user_yas)
13 @author = FactoryGirl.create :author, :user_id => @user.id
14 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
15 @other_user = FactoryGirl.create( :user_yas)
16 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
17 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
22 @comic = FactoryGirl.build :comic, :author_id => @author.id
25 context 'オーソドックスなデータのとき' do
29 @comic.should be_valid
32 @comic.title = 'a'*100
34 @comic.should be_valid
38 context 'titleを検証するとき' do
41 @comic.should_not be_valid
44 @comic.title = 'a'*101
45 @comic.should_not be_valid
48 context 'visibleを検証するとき' do
51 @comic.should_not be_valid
55 @comic.should_not be_valid
59 @comic.should_not be_valid
64 describe '文字コード検証に於いて' do
66 @comic = FactoryGirl.build :comic, :author_id => @author.id
69 context 'titleを検証するとき' do
70 it 'Shift JISなら失敗する' do
71 @comic.title = "\x83G\x83r\x83]\x83D"
74 }.should raise_error(Pettanr::BadRequest)
78 context 'descriptionを検証するとき' do
79 it 'Shift JISなら失敗する' do
80 @comic.description = "\x83G\x83r\x83]\x83D"
83 }.should raise_error(Pettanr::BadRequest)
88 describe 'デフォルト値補充に於いて' do
89 it 'visibleが0になっている' do
90 @comic = FactoryGirl.build :comic, :visible => nil
92 @comic.visible.should eq 0
96 describe '上書き補充に於いて' do
98 @comic = FactoryGirl.build :comic, :author_id => nil
99 @comic.overwrite @author
100 @comic.author_id.should eq @author.id
104 describe 'ロールリストからの作家取得に於いて' do
105 context 'ロールがユーザのとき' do
106 it 'ユーザから作家を取得して、それを返す' do
107 r = Comic.get_author_from_roles([@user])
111 context 'ロールが作家のとき' do
113 r = Comic.get_author_from_roles([@author])
117 context 'ロールが絵師のとき' do
118 it '絵師から作家を取得できれば、それをを返す' do
119 r = Comic.get_author_from_roles([@artist])
122 it '絵師から作家を取得できないときnilを返す' do
123 @artist.author_id = nil
125 r = Comic.get_author_from_roles([@artist])
129 context 'ロールが管理者のとき' do
131 r = Comic.get_author_from_roles([@admin])
137 describe 'ロールリストからの絵師取得に於いて' do
138 context 'ロールがユーザのとき' do
139 it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
140 r = Comic.get_artist_from_roles([@user])
143 it '作家から絵師を取得できないときnilを返す' do
144 @artist.author_id = nil
146 r = Comic.get_artist_from_roles([@user])
150 context 'ロールが作家のとき' do
151 it 'そこから絵師を取得できれば、それを返す' do
152 r = Comic.get_artist_from_roles([@author])
155 it '作家から絵師を取得できないときnilを返す' do
156 @artist.author_id = nil
158 r = Comic.get_artist_from_roles([@author])
162 context 'ロールが絵師のとき' do
164 r = Comic.get_artist_from_roles([@artist])
168 context 'ロールが管理者のとき' do
170 r = Comic.get_artist_from_roles([@admin])
176 describe '所持判定に於いて' do
178 @comic = FactoryGirl.build :comic, :author_id => @author.id
180 context '事前チェックする' do
181 it '自身にロールリストからの作家取得を依頼している' do
182 Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
183 r = @comic.own?([@author])
186 context 'ロール内作家が取得できるとき' do
189 it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
190 Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
191 r = @comic.own?([@author])
194 it 'ロール内作家のidが自身の作家idと一致しないならno' do
195 Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
196 @comic.own?(@other_author).should be_false
199 context 'ロール内作家が取得できないとき' do
201 Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
204 r = @comic.own?([@author])
210 describe '読者用ロールチェックに於いて' do
212 @comic = FactoryGirl.build :comic, :author_id => @author.id
214 context 'ロールリストに作家が含まれるとき' do
215 it 'ロールリストがリストではないとき、リストにする trueを返す' do
216 r = @comic.reader_role_check(@author)
220 r = @comic.reader_role_check([@author])
224 context 'ロールリストに絵師が含まれるとき' do
226 r = @comic.reader_role_check([@artist])
230 context 'ロールリストにユーザが含まれるとき' do
232 r = @comic.reader_role_check([@user])
236 context 'ロールリストに管理者が含まれるとき' do
238 r = @comic.reader_role_check([@admin])
242 context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
244 r = @comic.reader_role_check([nil])
250 describe '素材読者用ロールチェックに於いて' do
252 @comic = FactoryGirl.build :comic, :author_id => @author.id
254 context 'ロールリストに作家が含まれるとき' do
255 it 'ロールリストがリストではないとき、リストにする trueを返す' do
256 r = @comic.resource_reader_role_check(@author)
260 r = @comic.resource_reader_role_check([@author])
264 context 'ロールリストに絵師が含まれるとき' do
266 r = @comic.resource_reader_role_check([@artist])
270 context 'ロールリストにユーザが含まれるとき' do
272 r = @comic.resource_reader_role_check([@user])
276 context 'ロールリストに管理者が含まれるとき' do
278 r = @comic.resource_reader_role_check([@admin])
282 context 'ロールリストに借手が含まれるとき' do
284 r = @comic.resource_reader_role_check([@demand_user])
288 context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
290 r = @comic.resource_reader_role_check([nil])
296 describe '閲覧許可に於いて' do
298 @comic = FactoryGirl.build :comic, :author_id => @author.id
300 context 'オープンモードのとき' do
302 MagicNumber['run_mode'] = 0
304 it '自身にゲスト用ロールチェックを問い合わせしている' do
305 Comic.any_instance.stub(:guest_role_check).and_return(true)
306 Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
307 r = @comic.visible?([@author])
309 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
310 Comic.any_instance.stub(:guest_role_check).and_return(false)
311 r = @comic.visible?([@author])
315 context 'クローズドモードのとき' do
317 MagicNumber['run_mode'] = 1
319 it '自身に読者用ロールチェックを問い合わせしている' do
320 Comic.any_instance.stub(:reader_role_check).and_return(true)
321 Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
322 r = @comic.visible?([@author])
324 it '読者用ロールチェックが失敗したとき、falseを返す' do
325 Comic.any_instance.stub(:reader_role_check).and_return(false)
326 r = @comic.visible?([@author])
330 context '事前チェックする' do
332 MagicNumber['run_mode'] = 1
333 Comic.any_instance.stub(:reader_role_check).and_return(true)
334 Comic.any_instance.stub(:own?).and_return(true)
336 it '自身に所持判定を問い合わせしている' do
337 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
338 r = @comic.visible?([@author])
341 context 'つつがなく終わるとき' do
343 MagicNumber['run_mode'] = 1
344 Comic.any_instance.stub(:reader_role_check).and_return(true)
346 it '自分のコミックなら許可する' do
347 Comic.any_instance.stub(:own?).and_return(true)
348 Comic.any_instance.stub(:visible).and_return(0)
349 r = @comic.visible?([@author])
352 it '他人の非公開コミックなら許可しない' do
353 Comic.any_instance.stub(:own?).and_return(false)
354 Comic.any_instance.stub(:visible).and_return(0)
355 r = @comic.visible?([@author])
358 it '他人のコミックでも公開なら許可する' do
359 Comic.any_instance.stub(:own?).and_return(false)
360 Comic.any_instance.stub(:visible).and_return(1)
361 r = @comic.visible?([@author])
367 describe '一覧取得に於いて' do
369 @comic = FactoryGirl.create :comic, :author_id => @author.id
371 context 'page補正について' do
372 it '文字列から数値に変換される' do
373 Comic.page('8').should eq 8
376 Comic.page().should eq 1
379 Comic.page('0').should eq 1
382 context 'page_size補正について' do
383 it '文字列から数値に変換される' do
384 Comic.page_size('7').should eq 7
386 it 'nilの場合はComic.default_page_sizeになる' do
387 Comic.page_size().should eq Comic.default_page_size
389 it '0以下の場合はComic.default_page_sizeになる' do
390 Comic.page_size('0').should eq Comic.default_page_size
392 it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
393 Comic.page_size('1000').should eq Comic.max_page_size
396 context 'つつがなく終わるとき' do
397 it '一覧取得オプションを利用している' do
398 Comic.stub(:list_opt).with(any_args).and_return({})
399 Comic.should_receive(:list_opt).with(any_args).exactly(1)
407 it '非公開コミックは(自分のコミックであっても)含んでいない' do
408 FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
413 #公開コミックは(他人のコミックであっても)含んでいる
414 v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
416 c.should eq [v, @comic]
418 context 'DBに5件あって1ページの件数を2件に変えたとして' do
420 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
421 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
422 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
423 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
424 Comic.stub(:default_page_size).and_return(2)
428 c.should have(2).items
430 it 'page=1なら末尾2件を返す' do
433 c.should eq [@comic5, @comic4]
435 it 'page=2なら中間2件を返す' do
437 c.should eq [@comic3, @comic2]
439 it 'page=3なら先頭1件を返す' do
446 describe '自分のコミック一覧取得に於いて' do
448 @comic = FactoryGirl.create :comic, :author_id => @author.id
450 context 'つつがなく終わるとき' do
451 it '一覧取得オプションを利用している' do
452 Comic.stub(:list_opt).with(any_args).and_return({})
453 Comic.should_receive(:list_opt).with(any_args).exactly(1)
454 r = Comic.mylist @author
458 c = Comic.mylist @author
462 nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
463 cl = Comic.mylist @author
464 cl.should eq [nc, @comic]
466 it '他人のコミックは公開でも含まない' do
467 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
468 cl = Comic.mylist @author
469 cl.should eq [@comic]
471 it '自分のコミックは非公開でも含んでいる' do
472 nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
473 cl = Comic.mylist @author
474 cl.should eq [nc, @comic]
476 context 'DBに5件あって1ページの件数を2件に変えたとして' do
478 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
479 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
480 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
481 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
484 c = Comic.mylist @author, 1, 2
485 c.should have(2).items
487 it 'page=1なら末尾2件を返す' do
489 c = Comic.mylist(@author, 1, 2)
490 c.should eq [@comic5, @comic4]
492 it 'page=2なら中間2件を返す' do
493 c = Comic.mylist(@author, 2, 2)
494 c.should eq [@comic3, @comic2]
496 it 'page=3なら先頭1件を返す' do
497 c = Comic.mylist(@author, 3, 2)
503 describe '他作家のコミック一覧取得に於いて' do
505 @comic = FactoryGirl.create :comic, :author_id => @author.id
506 @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
508 context 'つつがなく終わるとき' do
509 it '一覧取得オプションを利用している' do
510 Comic.stub(:list_opt).with(any_args).and_return({})
511 Comic.should_receive(:list_opt).with(any_args).exactly(1)
512 r = Comic.himlist @other_author
515 it '指定した作家のリストを返す' do
516 r = Comic.himlist @other_author
517 r.should eq [@other_comic]
520 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
521 r = Comic.himlist @other_author
522 r.should eq [nc, @other_comic]
525 hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
526 r = Comic.himlist @other_author
527 r.should eq [@other_comic]
529 context 'DBに5件あって1ページの件数を2件に変えたとして' do
531 @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
532 @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
533 @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
534 @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
537 c = Comic.himlist @other_author, 1, 2
538 c.should have(2).items
540 it 'page=1なら末尾2件を返す' do
542 c = Comic.himlist(@other_author, 1, 2)
543 c.should eq [@other_comic5, @other_comic4]
545 it 'page=2なら中間2件を返す' do
546 c = Comic.himlist(@other_author, 2, 2)
547 c.should eq [@other_comic3, @other_comic2]
549 it 'page=3なら先頭1件を返す' do
550 c = Comic.himlist(@other_author, 3, 2)
551 c.should eq [@other_comic]
556 describe 'コミック一覧ページ制御に於いて' do
558 Comic.stub(:count).with(any_args).and_return(100)
561 r = Comic.list_paginate
562 r.is_a?(Kaminari::PaginatableArray).should be_true
564 it 'コミック一覧の取得条件を利用している' do
565 Comic.stub(:list_where).with(any_args).and_return('')
566 Comic.should_receive(:list_where).with(any_args).exactly(1)
567 r = Comic.list_paginate
569 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
570 r = Comic.list_paginate 3, 10
571 r.limit_value.should eq 10
572 r.offset_value.should eq 20
576 describe '自分のコミック一覧ページ制御に於いて' do
578 Comic.stub(:count).with(any_args).and_return(100)
581 r = Comic.mylist_paginate @author
582 r.is_a?(Kaminari::PaginatableArray).should be_true
584 it '自分のコミック一覧の取得条件を利用している' do
585 Comic.stub(:mylist_where).with(any_args).and_return('')
586 Comic.should_receive(:mylist_where).with(any_args).exactly(1)
587 r = Comic.mylist_paginate @author
589 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
590 r = Comic.mylist_paginate @author, 3, 10
591 r.limit_value.should eq 10
592 r.offset_value.should eq 20
596 describe '他作家のコミック一覧ページ制御に於いて' do
598 Comic.stub(:count).with(any_args).and_return(100)
601 r = Comic.himlist_paginate @other_author
602 r.is_a?(Kaminari::PaginatableArray).should be_true
604 it '他作家のコミック一覧の取得条件を利用している' do
605 Comic.stub(:himlist_where).with(any_args).and_return('')
606 Comic.should_receive(:himlist_where).with(any_args).exactly(1)
607 r = Comic.himlist_paginate @other_author
609 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
610 r = Comic.himlist_paginate @other_author, 3, 10
611 r.limit_value.should eq 10
612 r.offset_value.should eq 20
616 describe '一覧取得オプションに於いて' do
619 r.should have(2).items
623 r.has_key?(:stories).should be_true
625 it 'ストーリーはコマを含んでいる' do
627 r[:stories].has_key?(:panel).should be_true
631 r.has_key?(:author).should be_true
634 describe 'json一覧出力オプションに於いて' do
636 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
637 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
638 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
639 @sbt = FactoryGirl.create :speech_balloon_template
640 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
641 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
642 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
645 r = Comic.list.to_json Comic.list_json_opt
648 i.has_key?('stories').should be_true
650 it 'ストーリーはコマを含んでいる' do
651 r = Comic.list.to_json Comic.list_json_opt
654 s = i['stories'].first
655 s.has_key?('panel').should be_true
658 r = Comic.list.to_json Comic.list_json_opt
661 i.has_key?('author').should be_true
665 describe '単体取得に於いて' do
667 @comic = FactoryGirl.create :comic, :author_id => @author.id
669 context 'つつがなく終わるとき' do
670 it '単体取得オプションを利用している' do
671 Comic.stub(:show_opt).with(any_args).and_return({})
672 Comic.should_receive(:show_opt).with(any_args).exactly(1)
673 r = Comic.show @comic.id, @author
675 it '閲覧許可を問い合わせている' do
676 Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
677 Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
678 r = Comic.show @comic.id, @author
682 c = Comic.show @comic.id, @author
685 context '閲覧許可が出なかったとき' do
686 it '403Forbidden例外を返す' do
687 Comic.any_instance.stub(:visible?).and_return(false)
689 Comic.show @comic.id, @author
690 }.should raise_error(ActiveRecord::Forbidden)
693 context '存在しないコミックを開こうとしたとき' do
694 it '404RecordNotFound例外を返す' do
696 Comic.show 110, @author
697 }.should raise_error(ActiveRecord::RecordNotFound)
702 describe '編集取得に於いて' do
704 @comic = FactoryGirl.create :comic, :author_id => @author.id
706 context 'つつがなく終わるとき' do
707 it '単体取得オプションを利用している' do
708 Comic.stub(:show_opt).with(any_args).and_return({})
709 Comic.should_receive(:show_opt).with(any_args).exactly(1)
710 r = Comic.edit @comic.id, @author
712 it '所持判定を問い合わせている' do
713 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
714 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
715 r = Comic.edit @comic.id, @author
719 Comic.any_instance.stub(:own?).and_return(true)
720 c = Comic.edit @comic.id, @author.id
723 context '他人のコミックを開こうとしたとき' do
724 it '403Forbidden例外を返す' do
725 Comic.any_instance.stub(:own?).and_return(false)
727 Comic.edit @comic.id, @author
728 }.should raise_error(ActiveRecord::Forbidden)
731 context '存在しないコミックを開こうとしたとき' do
732 it '404RecordNotFound例外を返す' do
734 Comic.edit 110, @author
735 }.should raise_error(ActiveRecord::RecordNotFound)
739 describe '単体取得オプションに於いて' do
740 it 'includeキーを含んでいる' do
742 r.has_key?(:include).should be_true
745 r = Comic.show_opt[:include]
746 r.should have(2).items
749 r = Comic.show_opt[:include]
750 r.has_key?(:author).should be_true
753 r = Comic.show_opt[:include]
754 r.has_key?(:stories).should be_true
756 it 'ストーリーはコマを含んでいる' do
757 r = Comic.show_opt[:include]
758 r[:stories].has_key?(:panel).should be_true
761 describe 'json単体出力オプションに於いて' do
763 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
764 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
765 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
766 @sbt = FactoryGirl.create :speech_balloon_template
767 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
768 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
769 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
772 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
775 i.has_key?('stories').should be_true
777 it 'ストーリーはコマを含んでいる' do
778 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
781 s = i['stories'].first
782 s.has_key?('panel').should be_true
785 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
788 i.has_key?('author').should be_true
794 @comic = FactoryGirl.create :comic, :author_id => @author.id
795 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
796 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
797 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
798 @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
800 context 'つつがなく終わるとき' do
803 r = @comic.destroy_with_story
804 }.should change(Comic, :count).by(-1)
806 r = Comic.find @comic.id
809 it '自身にリンクしているストーリーをすべて削除する' do
811 r = @comic.destroy_with_story
812 }.should change(Story, :count).by(-1)
814 r = Story.find @story.id
818 r = @comic.destroy_with_story
822 context '削除に失敗したとき' do
824 Story.any_instance.stub(:destroy).with(any_args).and_return(false)
827 r = @comic.destroy_with_story
832 r = @comic.destroy_with_story
833 }.should_not change(Comic, :count)
835 r = @comic.destroy_with_story
836 }.should_not change(Story, :count)