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
444 context 'DBに5件あって1ページの件数を2件に変えたとして' do
446 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
447 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
448 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
449 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
450 Comic.stub(:default_page_size).and_return(2)
452 it '件数0は全件(5件)を返す' do
454 r.should have(5).items
458 describe '一覧取得オプションに於いて' do
459 it 'includeキーを含んでいる' do
461 r.has_key?(:include).should be_true
464 r = Comic.list_opt[:include]
465 r.should have(2).items
468 r = Comic.list_opt[:include]
469 r.has_key?(:stories).should be_true
471 it 'ストーリーはコマを含んでいる' do
472 r = Comic.list_opt[:include]
473 r[:stories].has_key?(:panel).should be_true
476 r = Comic.list_opt[:include]
477 r.has_key?(:author).should be_true
480 describe 'json一覧出力オプションに於いて' do
482 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
483 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
484 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
485 @sbt = FactoryGirl.create :speech_balloon_template
486 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
487 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
488 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
491 r = Comic.list.to_json Comic.list_json_opt
494 i.has_key?('stories').should be_true
496 it 'ストーリーはコマを含んでいる' do
497 r = Comic.list.to_json Comic.list_json_opt
500 s = i['stories'].first
501 s.has_key?('panel').should be_true
504 r = Comic.list.to_json Comic.list_json_opt
507 i.has_key?('author').should be_true
511 describe '自分のコミック一覧取得に於いて' do
513 @comic = FactoryGirl.create :comic, :author_id => @author.id
515 context 'つつがなく終わるとき' do
516 it '一覧取得オプションを利用している' do
517 Comic.stub(:list_opt).with(any_args).and_return({})
518 Comic.should_receive(:list_opt).with(any_args).exactly(1)
519 r = Comic.mylist @author
523 c = Comic.mylist @author
527 nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
528 cl = Comic.mylist @author
529 cl.should eq [nc, @comic]
531 it '他人のコミックは公開でも含まない' do
532 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
533 cl = Comic.mylist @author
534 cl.should eq [@comic]
536 it '自分のコミックは非公開でも含んでいる' do
537 nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
538 cl = Comic.mylist @author
539 cl.should eq [nc, @comic]
541 context 'DBに5件あって1ページの件数を2件に変えたとして' do
543 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
544 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
545 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
546 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
549 c = Comic.mylist @author, 1, 2
550 c.should have(2).items
552 it 'page=1なら末尾2件を返す' do
554 c = Comic.mylist(@author, 1, 2)
555 c.should eq [@comic5, @comic4]
557 it 'page=2なら中間2件を返す' do
558 c = Comic.mylist(@author, 2, 2)
559 c.should eq [@comic3, @comic2]
561 it 'page=3なら先頭1件を返す' do
562 c = Comic.mylist(@author, 3, 2)
566 context 'DBに5件あって1ページの件数を0件に変えたとして' do
568 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
569 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
570 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
571 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
572 Author.stub(:default_comic_page_size).and_return(2)
575 r = Comic.mylist @author, 5, 0
576 r.should have(5).items
581 describe '他作家のコミック一覧取得に於いて' do
583 @comic = FactoryGirl.create :comic, :author_id => @author.id
584 @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
586 context 'つつがなく終わるとき' do
587 it '一覧取得オプションを利用している' do
588 Comic.stub(:list_opt).with(any_args).and_return({})
589 Comic.should_receive(:list_opt).with(any_args).exactly(1)
590 r = Comic.himlist @other_author
593 it '指定した作家のリストを返す' do
594 r = Comic.himlist @other_author
595 r.should eq [@other_comic]
598 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
599 r = Comic.himlist @other_author
600 r.should eq [nc, @other_comic]
603 hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
604 r = Comic.himlist @other_author
605 r.should eq [@other_comic]
607 context 'DBに5件あって1ページの件数を2件に変えたとして' do
609 @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
610 @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
611 @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
612 @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
615 c = Comic.himlist @other_author, 1, 2
616 c.should have(2).items
618 it 'page=1なら末尾2件を返す' do
620 c = Comic.himlist(@other_author, 1, 2)
621 c.should eq [@other_comic5, @other_comic4]
623 it 'page=2なら中間2件を返す' do
624 c = Comic.himlist(@other_author, 2, 2)
625 c.should eq [@other_comic3, @other_comic2]
627 it 'page=3なら先頭1件を返す' do
628 c = Comic.himlist(@other_author, 3, 2)
629 c.should eq [@other_comic]
632 context 'DBに5件あって1ページの件数を0件に変えたとして' do
634 @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
635 @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
636 @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
637 @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
638 Author.stub(:default_comic_page_size).and_return(2)
641 r = Comic.himlist @other_author, 5, 0
642 r.should have(5).items
647 describe '単体取得に於いて' do
649 @comic = FactoryGirl.create :comic, :author_id => @author.id
651 context 'つつがなく終わるとき' do
652 it '単体取得オプションを利用している' do
653 Comic.stub(:show_opt).with(any_args).and_return({})
654 Comic.should_receive(:show_opt).with(any_args).exactly(1)
655 r = Comic.show @comic.id, @author
657 it '閲覧許可を問い合わせている' do
658 Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
659 Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
660 r = Comic.show @comic.id, @author
664 c = Comic.show @comic.id, @author
667 context '閲覧許可が出なかったとき' do
668 it '403Forbidden例外を返す' do
669 Comic.any_instance.stub(:visible?).and_return(false)
671 Comic.show @comic.id, @author
672 }.should raise_error(ActiveRecord::Forbidden)
675 context '存在しないコミックを開こうとしたとき' do
676 it '404RecordNotFound例外を返す' do
678 Comic.show 110, @author
679 }.should raise_error(ActiveRecord::RecordNotFound)
684 describe '編集取得に於いて' do
686 @comic = FactoryGirl.create :comic, :author_id => @author.id
688 context 'つつがなく終わるとき' do
689 it '単体取得オプションを利用している' do
690 Comic.stub(:show_opt).with(any_args).and_return({})
691 Comic.should_receive(:show_opt).with(any_args).exactly(1)
692 r = Comic.edit @comic.id, @author
694 it '所持判定を問い合わせている' do
695 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
696 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
697 r = Comic.edit @comic.id, @author
701 Comic.any_instance.stub(:own?).and_return(true)
702 c = Comic.edit @comic.id, @author.id
705 context '他人のコミックを開こうとしたとき' do
706 it '403Forbidden例外を返す' do
707 Comic.any_instance.stub(:own?).and_return(false)
709 Comic.edit @comic.id, @author
710 }.should raise_error(ActiveRecord::Forbidden)
713 context '存在しないコミックを開こうとしたとき' do
714 it '404RecordNotFound例外を返す' do
716 Comic.edit 110, @author
717 }.should raise_error(ActiveRecord::RecordNotFound)
721 describe '単体取得オプションに於いて' do
722 it 'includeキーを含んでいる' do
724 r.has_key?(:include).should be_true
727 r = Comic.show_opt[:include]
728 r.should have(2).items
731 r = Comic.show_opt[:include]
732 r.has_key?(:author).should be_true
735 r = Comic.show_opt[:include]
736 r.has_key?(:stories).should be_true
738 it 'ストーリーはコマを含んでいる' do
739 r = Comic.show_opt[:include]
740 r[:stories].has_key?(:panel).should be_true
743 describe 'json単体出力オプションに於いて' do
745 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
746 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
747 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
748 @sbt = FactoryGirl.create :speech_balloon_template
749 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
750 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
751 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
754 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
757 i.has_key?('stories').should be_true
759 it 'ストーリーはコマを含んでいる' do
760 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
763 s = i['stories'].first
764 s.has_key?('panel').should be_true
767 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
770 i.has_key?('author').should be_true
776 @comic = FactoryGirl.create :comic, :author_id => @author.id
777 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
778 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
779 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
780 @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
782 context 'つつがなく終わるとき' do
785 r = @comic.destroy_with_story
786 }.should change(Comic, :count).by(-1)
788 r = Comic.find @comic.id
791 it '自身にリンクしているストーリーをすべて削除する' do
793 r = @comic.destroy_with_story
794 }.should change(Story, :count).by(-1)
796 r = Story.find @story.id
800 r = @comic.destroy_with_story
804 context '削除に失敗したとき' do
806 Story.any_instance.stub(:destroy).with(any_args).and_return(false)
809 r = @comic.destroy_with_story
814 r = @comic.destroy_with_story
815 }.should_not change(Comic, :count)
817 r = @comic.destroy_with_story
818 }.should_not change(Story, :count)