1 # -*- encoding: utf-8 -*-
7 SpeechBalloonTemplate.delete_all
8 @admin = FactoryGirl.create :admin
9 @demand_user = FactoryGirl.create :demand_user
10 @sp = FactoryGirl.create :system_picture
11 @lg = FactoryGirl.create :license_group
12 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13 @user = FactoryGirl.create( :user_yas)
14 @author = FactoryGirl.create :author, :user_id => @user.id
15 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
16 @other_user = FactoryGirl.create( :user_yas)
17 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
18 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
23 @comic = FactoryGirl.build :comic, :author_id => @author.id
26 context 'オーソドックスなデータのとき' do
30 @comic.should be_valid
33 @comic.title = 'a'*100
35 @comic.should be_valid
39 context 'titleを検証するとき' do
42 @comic.should_not be_valid
45 @comic.title = 'a'*101
46 @comic.should_not be_valid
50 context 'visibleを検証するとき' do
53 @comic.should_not be_valid
57 @comic.should_not be_valid
61 @comic.should_not be_valid
65 context 'author_idを検証するとき' do
67 @comic.author_id = nil
68 @comic.should_not be_valid
71 @comic.author_id = 'a'
72 @comic.should_not be_valid
74 it '存在する作家でなければ失敗する' do
76 @comic.should_not be_valid
82 describe '文字コード検証に於いて' do
84 @comic = FactoryGirl.build :comic, :author_id => @author.id
87 context 'titleを検証するとき' do
88 it 'Shift JISなら失敗する' do
89 @comic.title = "\x83G\x83r\x83]\x83D"
92 }.should raise_error(Pettanr::BadRequest)
96 context 'descriptionを検証するとき' do
97 it 'Shift JISなら失敗する' do
98 @comic.description = "\x83G\x83r\x83]\x83D"
101 }.should raise_error(Pettanr::BadRequest)
106 describe 'デフォルト値補充に於いて' do
107 it 'visibleが0になっている' do
108 @comic = FactoryGirl.build :comic, :visible => nil
109 @comic.supply_default
110 @comic.visible.should eq 0
114 describe '上書き補充に於いて' do
116 @comic = FactoryGirl.build :comic, :author_id => nil
117 @comic.overwrite @author
118 @comic.author_id.should eq @author.id
122 describe 'ロールリストからの作家取得に於いて' do
123 context 'ロールがユーザのとき' do
124 it 'ユーザから作家を取得して、それを返す' do
125 r = Comic.get_author_from_roles([@user])
129 context 'ロールが作家のとき' do
131 r = Comic.get_author_from_roles([@author])
135 context 'ロールが絵師のとき' do
136 it '絵師から作家を取得できれば、それをを返す' do
137 r = Comic.get_author_from_roles([@artist])
140 it '絵師から作家を取得できないときnilを返す' do
141 @artist.author_id = nil
143 r = Comic.get_author_from_roles([@artist])
147 context 'ロールが管理者のとき' do
149 r = Comic.get_author_from_roles([@admin])
155 describe 'ロールリストからの絵師取得に於いて' do
156 context 'ロールがユーザのとき' do
157 it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
158 r = Comic.get_artist_from_roles([@user])
161 it '作家から絵師を取得できないときnilを返す' do
162 @artist.author_id = nil
164 r = Comic.get_artist_from_roles([@user])
168 context 'ロールが作家のとき' do
169 it 'そこから絵師を取得できれば、それを返す' do
170 r = Comic.get_artist_from_roles([@author])
173 it '作家から絵師を取得できないときnilを返す' do
174 @artist.author_id = nil
176 r = Comic.get_artist_from_roles([@author])
180 context 'ロールが絵師のとき' do
182 r = Comic.get_artist_from_roles([@artist])
186 context 'ロールが管理者のとき' do
188 r = Comic.get_artist_from_roles([@admin])
194 describe '所持判定に於いて' do
196 @comic = FactoryGirl.build :comic, :author_id => @author.id
198 context '事前チェックする' do
199 it '自身にロールリストからの作家取得を依頼している' do
200 Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
201 r = @comic.own?([@author])
204 context 'ロール内作家が取得できるとき' do
207 it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
208 Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
209 r = @comic.own?([@author])
212 it 'ロール内作家のidが自身の作家idと一致しないならno' do
213 Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
214 @comic.own?(@other_author).should be_false
217 context 'ロール内作家が取得できないとき' do
219 Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
222 r = @comic.own?([@author])
228 describe '読者用ロールチェックに於いて' do
230 @comic = FactoryGirl.build :comic, :author_id => @author.id
232 context 'ロールリストに作家が含まれるとき' do
233 it 'ロールリストがリストではないとき、リストにする trueを返す' do
234 r = @comic.reader_role_check(@author)
238 r = @comic.reader_role_check([@author])
242 context 'ロールリストに絵師が含まれるとき' do
244 r = @comic.reader_role_check([@artist])
248 context 'ロールリストにユーザが含まれるとき' do
250 r = @comic.reader_role_check([@user])
254 context 'ロールリストに管理者が含まれるとき' do
256 r = @comic.reader_role_check([@admin])
260 context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
262 r = @comic.reader_role_check([nil])
268 describe '素材読者用ロールチェックに於いて' do
270 @comic = FactoryGirl.build :comic, :author_id => @author.id
272 context 'ロールリストに作家が含まれるとき' do
273 it 'ロールリストがリストではないとき、リストにする trueを返す' do
274 r = @comic.resource_reader_role_check(@author)
278 r = @comic.resource_reader_role_check([@author])
282 context 'ロールリストに絵師が含まれるとき' do
284 r = @comic.resource_reader_role_check([@artist])
288 context 'ロールリストにユーザが含まれるとき' do
290 r = @comic.resource_reader_role_check([@user])
294 context 'ロールリストに管理者が含まれるとき' do
296 r = @comic.resource_reader_role_check([@admin])
300 context 'ロールリストに借手が含まれるとき' do
302 r = @comic.resource_reader_role_check([@demand_user])
306 context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
308 r = @comic.resource_reader_role_check([nil])
314 describe '閲覧許可に於いて' do
316 @comic = FactoryGirl.build :comic, :author_id => @author.id
318 context 'オープンモードのとき' do
320 MagicNumber['run_mode'] = 0
322 it '自身にゲスト用ロールチェックを問い合わせしている' do
323 Comic.any_instance.stub(:guest_role_check).and_return(true)
324 Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
325 r = @comic.visible?([@author])
327 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
328 Comic.any_instance.stub(:guest_role_check).and_return(false)
329 r = @comic.visible?([@author])
333 context 'クローズドモードのとき' do
335 MagicNumber['run_mode'] = 1
337 it '自身に読者用ロールチェックを問い合わせしている' do
338 Comic.any_instance.stub(:reader_role_check).and_return(true)
339 Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
340 r = @comic.visible?([@author])
342 it '読者用ロールチェックが失敗したとき、falseを返す' do
343 Comic.any_instance.stub(:reader_role_check).and_return(false)
344 r = @comic.visible?([@author])
348 context '事前チェックする' do
350 MagicNumber['run_mode'] = 1
351 Comic.any_instance.stub(:reader_role_check).and_return(true)
352 Comic.any_instance.stub(:own?).and_return(true)
354 it '自身に所持判定を問い合わせしている' do
355 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
356 r = @comic.visible?([@author])
359 context 'つつがなく終わるとき' do
361 MagicNumber['run_mode'] = 1
362 Comic.any_instance.stub(:reader_role_check).and_return(true)
364 it '自分のコミックなら許可する' do
365 Comic.any_instance.stub(:own?).and_return(true)
366 Comic.any_instance.stub(:visible).and_return(0)
367 r = @comic.visible?([@author])
370 it '他人の非公開コミックなら許可しない' do
371 Comic.any_instance.stub(:own?).and_return(false)
372 Comic.any_instance.stub(:visible).and_return(0)
373 r = @comic.visible?([@author])
376 it '他人のコミックでも公開なら許可する' do
377 Comic.any_instance.stub(:own?).and_return(false)
378 Comic.any_instance.stub(:visible).and_return(1)
379 r = @comic.visible?([@author])
385 describe '一覧取得に於いて' do
387 @comic = FactoryGirl.create :comic, :author_id => @author.id
389 context 'page補正について' do
390 it '文字列から数値に変換される' do
391 Comic.page('8').should eq 8
394 Comic.page().should eq 1
397 Comic.page('0').should eq 1
400 context 'page_size補正について' do
401 it '文字列から数値に変換される' do
402 Comic.page_size('7').should eq 7
404 it 'nilの場合はComic.default_page_sizeになる' do
405 Comic.page_size().should eq Comic.default_page_size
407 it '0以下の場合はComic.default_page_sizeになる' do
408 Comic.page_size('0').should eq Comic.default_page_size
410 it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
411 Comic.page_size('1000').should eq Comic.max_page_size
414 context 'つつがなく終わるとき' do
415 it '一覧取得オプションを利用している' do
416 Comic.stub(:list_opt).with(any_args).and_return({})
417 Comic.should_receive(:list_opt).with(any_args).exactly(1)
425 it '非公開コミックは(自分のコミックであっても)含んでいない' do
426 FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
431 #公開コミックは(他人のコミックであっても)含んでいる
432 v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
434 c.should eq [v, @comic]
436 context 'DBに5件あって1ページの件数を2件に変えたとして' do
438 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
439 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
440 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
441 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
442 Comic.stub(:default_page_size).and_return(2)
446 c.should have(2).items
448 it 'page=1なら末尾2件を返す' do
451 c.should eq [@comic5, @comic4]
453 it 'page=2なら中間2件を返す' do
455 c.should eq [@comic3, @comic2]
457 it 'page=3なら先頭1件を返す' do
464 describe '自分のコミック一覧取得に於いて' do
466 @comic = FactoryGirl.create :comic, :author_id => @author.id
468 context 'つつがなく終わるとき' do
469 it '一覧取得オプションを利用している' do
470 Comic.stub(:list_opt).with(any_args).and_return({})
471 Comic.should_receive(:list_opt).with(any_args).exactly(1)
472 r = Comic.mylist @author
476 c = Comic.mylist @author
480 nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
481 cl = Comic.mylist @author
482 cl.should eq [nc, @comic]
484 it '他人のコミックは公開でも含まない' do
485 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
486 cl = Comic.mylist @author
487 cl.should eq [@comic]
489 it '自分のコミックは非公開でも含んでいる' do
490 nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
491 cl = Comic.mylist @author
492 cl.should eq [nc, @comic]
494 context 'DBに5件あって1ページの件数を2件に変えたとして' do
496 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
497 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
498 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
499 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
502 c = Comic.mylist @author, 1, 2
503 c.should have(2).items
505 it 'page=1なら末尾2件を返す' do
507 c = Comic.mylist(@author, 1, 2)
508 c.should eq [@comic5, @comic4]
510 it 'page=2なら中間2件を返す' do
511 c = Comic.mylist(@author, 2, 2)
512 c.should eq [@comic3, @comic2]
514 it 'page=3なら先頭1件を返す' do
515 c = Comic.mylist(@author, 3, 2)
521 describe '他作家のコミック一覧取得に於いて' do
523 @comic = FactoryGirl.create :comic, :author_id => @author.id
524 @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
526 context 'つつがなく終わるとき' do
527 it '一覧取得オプションを利用している' do
528 Comic.stub(:list_opt).with(any_args).and_return({})
529 Comic.should_receive(:list_opt).with(any_args).exactly(1)
530 r = Comic.himlist @other_author
533 it '指定した作家のリストを返す' do
534 r = Comic.himlist @other_author
535 r.should eq [@other_comic]
538 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
539 r = Comic.himlist @other_author
540 r.should eq [nc, @other_comic]
543 hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
544 r = Comic.himlist @other_author
545 r.should eq [@other_comic]
547 context 'DBに5件あって1ページの件数を2件に変えたとして' do
549 @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
550 @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
551 @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
552 @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
555 c = Comic.himlist @other_author, 1, 2
556 c.should have(2).items
558 it 'page=1なら末尾2件を返す' do
560 c = Comic.himlist(@other_author, 1, 2)
561 c.should eq [@other_comic5, @other_comic4]
563 it 'page=2なら中間2件を返す' do
564 c = Comic.himlist(@other_author, 2, 2)
565 c.should eq [@other_comic3, @other_comic2]
567 it 'page=3なら先頭1件を返す' do
568 c = Comic.himlist(@other_author, 3, 2)
569 c.should eq [@other_comic]
574 describe 'コミック一覧ページ制御に於いて' do
576 Comic.stub(:count).with(any_args).and_return(100)
579 r = Comic.list_paginate
580 r.is_a?(Kaminari::PaginatableArray).should be_true
582 it 'コミック一覧の取得条件を利用している' do
583 Comic.stub(:list_where).with(any_args).and_return('')
584 Comic.should_receive(:list_where).with(any_args).exactly(1)
585 r = Comic.list_paginate
587 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
588 r = Comic.list_paginate 3, 10
589 r.limit_value.should eq 10
590 r.offset_value.should eq 20
594 describe '自分のコミック一覧ページ制御に於いて' do
596 Comic.stub(:count).with(any_args).and_return(100)
599 r = Comic.mylist_paginate @author
600 r.is_a?(Kaminari::PaginatableArray).should be_true
602 it '自分のコミック一覧の取得条件を利用している' do
603 Comic.stub(:mylist_where).with(any_args).and_return('')
604 Comic.should_receive(:mylist_where).with(any_args).exactly(1)
605 r = Comic.mylist_paginate @author
607 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
608 r = Comic.mylist_paginate @author, 3, 10
609 r.limit_value.should eq 10
610 r.offset_value.should eq 20
614 describe '他作家のコミック一覧ページ制御に於いて' do
616 Comic.stub(:count).with(any_args).and_return(100)
619 r = Comic.himlist_paginate @other_author
620 r.is_a?(Kaminari::PaginatableArray).should be_true
622 it '他作家のコミック一覧の取得条件を利用している' do
623 Comic.stub(:himlist_where).with(any_args).and_return('')
624 Comic.should_receive(:himlist_where).with(any_args).exactly(1)
625 r = Comic.himlist_paginate @other_author
627 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
628 r = Comic.himlist_paginate @other_author, 3, 10
629 r.limit_value.should eq 10
630 r.offset_value.should eq 20
634 describe '一覧取得オプションに於いて' do
637 r.should have(2).items
641 r.has_key?(:stories).should be_true
645 r.has_key?(:author).should be_true
648 describe 'json一覧出力オプションに於いて' do
650 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
651 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
652 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
653 @sbt = FactoryGirl.create :speech_balloon_template
654 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
655 @story = FactoryGirl.create :story, :comic_id => @comic.id
658 r = Comic.list.to_json Comic.list_json_opt
661 i.has_key?('stories').should be_true
664 r = Comic.list.to_json Comic.list_json_opt
667 i.has_key?('author').should be_true
671 describe '単体取得に於いて' do
673 @comic = FactoryGirl.create :comic, :author_id => @author.id
675 context 'つつがなく終わるとき' do
676 it '単体取得オプションを利用している' do
677 Comic.stub(:show_opt).with(any_args).and_return({})
678 Comic.should_receive(:show_opt).with(any_args).exactly(1)
679 r = Comic.show @comic.id, @author
681 it '閲覧許可を問い合わせている' do
682 Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
683 Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
684 r = Comic.show @comic.id, @author
688 c = Comic.show @comic.id, @author
691 context '閲覧許可が出なかったとき' do
692 it '403Forbidden例外を返す' do
693 Comic.any_instance.stub(:visible?).and_return(false)
695 Comic.show @comic.id, @author
696 }.should raise_error(ActiveRecord::Forbidden)
699 context '存在しないコミックを開こうとしたとき' do
700 it '404RecordNotFound例外を返す' do
702 Comic.show 110, @author
703 }.should raise_error(ActiveRecord::RecordNotFound)
708 describe '編集取得に於いて' do
710 @comic = FactoryGirl.create :comic, :author_id => @author.id
712 context 'つつがなく終わるとき' do
713 it '単体取得オプションを利用している' do
714 Comic.stub(:show_opt).with(any_args).and_return({})
715 Comic.should_receive(:show_opt).with(any_args).exactly(1)
716 r = Comic.edit @comic.id, @author
718 it '所持判定を問い合わせている' do
719 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
720 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
721 r = Comic.edit @comic.id, @author
725 Comic.any_instance.stub(:own?).and_return(true)
726 c = Comic.edit @comic.id, @author.id
729 context '他人のコミックを開こうとしたとき' do
730 it '403Forbidden例外を返す' do
731 Comic.any_instance.stub(:own?).and_return(false)
733 Comic.edit @comic.id, @author
734 }.should raise_error(ActiveRecord::Forbidden)
737 context '存在しないコミックを開こうとしたとき' do
738 it '404RecordNotFound例外を返す' do
740 Comic.edit 110, @author
741 }.should raise_error(ActiveRecord::RecordNotFound)
745 describe '単体取得オプションに於いて' do
746 it 'includeキーを含んでいる' do
748 r.has_key?(:include).should be_true
751 r = Comic.show_opt[:include]
752 r.should have(2).items
755 r = Comic.show_opt[:include]
756 r.has_key?(:author).should be_true
759 r = Comic.show_opt[:include]
760 r.has_key?(:stories).should be_true
763 describe 'json単体出力オプションに於いて' do
765 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
766 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
767 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
768 @sbt = FactoryGirl.create :speech_balloon_template
769 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
770 @story = FactoryGirl.create :story, :comic_id => @comic.id
773 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
776 i.has_key?('stories').should be_true
779 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
782 i.has_key?('author').should be_true
788 @comic = FactoryGirl.create :comic, :author_id => @author.id
789 @story = FactoryGirl.create :story, :comic_id => @comic.id
790 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
791 @other_story = FactoryGirl.create :story, :comic_id => @other_comic.id
793 context 'つつがなく終わるとき' do
796 r = @comic.destroy_with_story
797 }.should change(Comic, :count).by(-1)
799 r = Comic.find @comic.id
802 it '自身にリンクしているストーリーをすべて削除する' do
804 r = @comic.destroy_with_story
805 }.should change(Story, :count).by(-1)
807 r = Story.find @story.id
811 r = @comic.destroy_with_story
815 context '削除に失敗したとき' do
817 Story.any_instance.stub(:destroy).with(any_args).and_return(false)
820 r = @comic.destroy_with_story
825 r = @comic.destroy_with_story
826 }.should_not change(Comic, :count)
828 r = @comic.destroy_with_story
829 }.should_not change(Story, :count)