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
65 it 'visibleが0になっている' do
66 @comic = FactoryGirl.build :comic, :visible => nil
68 @comic.visible.should eq 0
72 describe '上書き補充に於いて' do
74 @comic = FactoryGirl.build :comic, :author_id => nil
75 @comic.overwrite @author
76 @comic.author_id.should eq @author.id
80 describe 'ロールリストからの作家取得に於いて' do
81 context 'ロールがユーザのとき' do
82 it 'ユーザから作家を取得して、それを返す' do
83 r = Comic.get_author_from_roles([@user])
87 context 'ロールが作家のとき' do
89 r = Comic.get_author_from_roles([@author])
93 context 'ロールが絵師のとき' do
94 it '絵師から作家を取得できれば、それをを返す' do
95 r = Comic.get_author_from_roles([@artist])
98 it '絵師から作家を取得できないときnilを返す' do
99 @artist.author_id = nil
101 r = Comic.get_author_from_roles([@artist])
105 context 'ロールが管理者のとき' do
107 r = Comic.get_author_from_roles([@admin])
113 describe 'ロールリストからの絵師取得に於いて' do
114 context 'ロールがユーザのとき' do
115 it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
116 r = Comic.get_artist_from_roles([@user])
119 it '作家から絵師を取得できないときnilを返す' do
120 @artist.author_id = nil
122 r = Comic.get_artist_from_roles([@user])
126 context 'ロールが作家のとき' do
127 it 'そこから絵師を取得できれば、それを返す' do
128 r = Comic.get_artist_from_roles([@author])
131 it '作家から絵師を取得できないときnilを返す' do
132 @artist.author_id = nil
134 r = Comic.get_artist_from_roles([@author])
138 context 'ロールが絵師のとき' do
140 r = Comic.get_artist_from_roles([@artist])
144 context 'ロールが管理者のとき' do
146 r = Comic.get_artist_from_roles([@admin])
152 describe '所持判定に於いて' do
154 @comic = FactoryGirl.build :comic, :author_id => @author.id
156 context '事前チェックする' do
157 it '自身にロールリストからの作家取得を依頼している' do
158 Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
159 r = @comic.own?([@author])
162 context 'ロール内作家が取得できるとき' do
165 it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
166 Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
167 r = @comic.own?([@author])
170 it 'ロール内作家のidが自身の作家idと一致しないならno' do
171 Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
172 @comic.own?(@other_author).should be_false
175 context 'ロール内作家が取得できないとき' do
177 Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
180 r = @comic.own?([@author])
186 describe '読者用ロールチェックに於いて' do
188 @comic = FactoryGirl.build :comic, :author_id => @author.id
190 context 'ロールリストに作家が含まれるとき' do
191 it 'ロールリストがリストではないとき、リストにする trueを返す' do
192 r = @comic.reader_role_check(@author)
196 r = @comic.reader_role_check([@author])
200 context 'ロールリストに絵師が含まれるとき' do
202 r = @comic.reader_role_check([@artist])
206 context 'ロールリストにユーザが含まれるとき' do
208 r = @comic.reader_role_check([@user])
212 context 'ロールリストに管理者が含まれるとき' do
214 r = @comic.reader_role_check([@admin])
218 context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
220 r = @comic.reader_role_check([nil])
226 describe '素材読者用ロールチェックに於いて' do
228 @comic = FactoryGirl.build :comic, :author_id => @author.id
230 context 'ロールリストに作家が含まれるとき' do
231 it 'ロールリストがリストではないとき、リストにする trueを返す' do
232 r = @comic.resource_reader_role_check(@author)
236 r = @comic.resource_reader_role_check([@author])
240 context 'ロールリストに絵師が含まれるとき' do
242 r = @comic.resource_reader_role_check([@artist])
246 context 'ロールリストにユーザが含まれるとき' do
248 r = @comic.resource_reader_role_check([@user])
252 context 'ロールリストに管理者が含まれるとき' do
254 r = @comic.resource_reader_role_check([@admin])
258 context 'ロールリストに借手が含まれるとき' do
260 r = @comic.resource_reader_role_check([@demand_user])
264 context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
266 r = @comic.resource_reader_role_check([nil])
272 describe '閲覧許可に於いて' do
274 @comic = FactoryGirl.build :comic, :author_id => @author.id
276 context 'オープンモードのとき' do
278 MagicNumber['run_mode'] = 0
280 it '自身にゲスト用ロールチェックを問い合わせしている' do
281 Comic.any_instance.stub(:guest_role_check).and_return(true)
282 Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
283 r = @comic.visible?([@author])
285 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
286 Comic.any_instance.stub(:guest_role_check).and_return(false)
287 r = @comic.visible?([@author])
291 context 'クローズドモードのとき' do
293 MagicNumber['run_mode'] = 1
295 it '自身に読者用ロールチェックを問い合わせしている' do
296 Comic.any_instance.stub(:reader_role_check).and_return(true)
297 Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
298 r = @comic.visible?([@author])
300 it '読者用ロールチェックが失敗したとき、falseを返す' do
301 Comic.any_instance.stub(:reader_role_check).and_return(false)
302 r = @comic.visible?([@author])
306 context '事前チェックする' do
308 MagicNumber['run_mode'] = 1
309 Comic.any_instance.stub(:reader_role_check).and_return(true)
310 Comic.any_instance.stub(:own?).and_return(true)
312 it '自身に所持判定を問い合わせしている' do
313 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
314 r = @comic.visible?([@author])
317 context 'つつがなく終わるとき' do
319 MagicNumber['run_mode'] = 1
320 Comic.any_instance.stub(:reader_role_check).and_return(true)
322 it '自分のコミックなら許可する' do
323 Comic.any_instance.stub(:own?).and_return(true)
324 Comic.any_instance.stub(:visible).and_return(0)
325 r = @comic.visible?([@author])
328 it '他人の非公開コミックなら許可しない' do
329 Comic.any_instance.stub(:own?).and_return(false)
330 Comic.any_instance.stub(:visible).and_return(0)
331 r = @comic.visible?([@author])
334 it '他人のコミックでも公開なら許可する' do
335 Comic.any_instance.stub(:own?).and_return(false)
336 Comic.any_instance.stub(:visible).and_return(1)
337 r = @comic.visible?([@author])
343 describe '一覧取得に於いて' do
345 @comic = FactoryGirl.create :comic, :author_id => @author.id
347 context 'page補正について' do
348 it '文字列から数値に変換される' do
349 Comic.page('8').should eq 8
352 Comic.page().should eq 1
355 Comic.page('0').should eq 1
358 context 'page_size補正について' do
359 it '文字列から数値に変換される' do
360 Comic.page_size('7').should eq 7
362 it 'nilの場合はComic.default_page_sizeになる' do
363 Comic.page_size().should eq Comic.default_page_size
365 it '0以下の場合はComic.default_page_sizeになる' do
366 Comic.page_size('0').should eq Comic.default_page_size
368 it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
369 Comic.page_size('1000').should eq Comic.max_page_size
372 context 'つつがなく終わるとき' do
373 it '一覧取得オプションを利用している' do
374 Comic.stub(:list_opt).with(any_args).and_return({})
375 Comic.should_receive(:list_opt).with(any_args).exactly(1)
383 it '非公開コミックは(自分のコミックであっても)含んでいない' do
384 FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
389 #公開コミックは(他人のコミックであっても)含んでいる
390 v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
392 c.should eq [v, @comic]
394 context 'DBに5件あって1ページの件数を2件に変えたとして' do
396 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
397 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
398 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
399 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
400 Comic.stub(:default_page_size).and_return(2)
404 c.should have(2).items
406 it 'page=1なら末尾2件を返す' do
409 c.should eq [@comic5, @comic4]
411 it 'page=2なら中間2件を返す' do
413 c.should eq [@comic3, @comic2]
415 it 'page=3なら先頭1件を返す' do
420 context 'DBに5件あって1ページの件数を2件に変えたとして' do
422 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
423 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
424 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
425 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
426 Comic.stub(:default_page_size).and_return(2)
428 it '件数0は全件(5件)を返す' do
430 r.should have(5).items
434 describe '一覧取得オプションに於いて' do
435 it 'includeキーを含んでいる' do
437 r.has_key?(:include).should be_true
440 r = Comic.list_opt[:include]
441 r.should have(2).items
444 r = Comic.list_opt[:include]
445 r.has_key?(:stories).should be_true
447 it 'ストーリーはコマを含んでいる' do
448 r = Comic.list_opt[:include]
449 r[:stories].has_key?(:panel).should be_true
452 r = Comic.list_opt[:include]
453 r.has_key?(:author).should be_true
456 describe 'json一覧出力オプションに於いて' do
458 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
459 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
460 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
461 @sbt = FactoryGirl.create :speech_balloon_template
462 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
463 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
464 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
467 r = Comic.list.to_json Comic.list_json_opt
470 i.has_key?('stories').should be_true
472 it 'ストーリーはコマを含んでいる' do
473 r = Comic.list.to_json Comic.list_json_opt
476 s = i['stories'].first
477 s.has_key?('panel').should be_true
480 r = Comic.list.to_json Comic.list_json_opt
483 i.has_key?('author').should be_true
487 describe '自分のコミック一覧取得に於いて' do
489 @comic = FactoryGirl.create :comic, :author_id => @author.id
491 context 'つつがなく終わるとき' do
492 it '一覧取得オプションを利用している' do
493 Comic.stub(:list_opt).with(any_args).and_return({})
494 Comic.should_receive(:list_opt).with(any_args).exactly(1)
495 r = Comic.mylist @author
499 c = Comic.mylist @author
503 nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
504 cl = Comic.mylist @author
505 cl.should eq [nc, @comic]
507 it '他人のコミックは公開でも含まない' do
508 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
509 cl = Comic.mylist @author
510 cl.should eq [@comic]
512 it '自分のコミックは非公開でも含んでいる' do
513 nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
514 cl = Comic.mylist @author
515 cl.should eq [nc, @comic]
517 context 'DBに5件あって1ページの件数を2件に変えたとして' do
519 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
520 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
521 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
522 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
525 c = Comic.mylist @author, 1, 2
526 c.should have(2).items
528 it 'page=1なら末尾2件を返す' do
530 c = Comic.mylist(@author, 1, 2)
531 c.should eq [@comic5, @comic4]
533 it 'page=2なら中間2件を返す' do
534 c = Comic.mylist(@author, 2, 2)
535 c.should eq [@comic3, @comic2]
537 it 'page=3なら先頭1件を返す' do
538 c = Comic.mylist(@author, 3, 2)
542 context 'DBに5件あって1ページの件数を0件に変えたとして' do
544 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
545 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
546 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
547 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
548 Author.stub(:default_comic_page_size).and_return(2)
551 r = Comic.mylist @author, 5, 0
552 r.should have(5).items
557 describe '単体取得に於いて' do
559 @comic = FactoryGirl.create :comic, :author_id => @author.id
561 context 'つつがなく終わるとき' do
562 it '単体取得オプションを利用している' do
563 Comic.stub(:show_opt).with(any_args).and_return({})
564 Comic.should_receive(:show_opt).with(any_args).exactly(1)
565 r = Comic.show @comic.id, @author
567 it '閲覧許可を問い合わせている' do
568 Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
569 Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
570 r = Comic.show @comic.id, @author
574 c = Comic.show @comic.id, @author
577 context '閲覧許可が出なかったとき' do
578 it '403Forbidden例外を返す' do
579 Comic.any_instance.stub(:visible?).and_return(false)
581 Comic.show @comic.id, @author
582 }.should raise_error(ActiveRecord::Forbidden)
585 context '存在しないコミックを開こうとしたとき' do
586 it '404RecordNotFound例外を返す' do
588 Comic.show 110, @author
589 }.should raise_error(ActiveRecord::RecordNotFound)
594 describe '編集取得に於いて' do
596 @comic = FactoryGirl.create :comic, :author_id => @author.id
598 context 'つつがなく終わるとき' do
599 it '単体取得オプションを利用している' do
600 Comic.stub(:show_opt).with(any_args).and_return({})
601 Comic.should_receive(:show_opt).with(any_args).exactly(1)
602 r = Comic.edit @comic.id, @author
604 it '所持判定を問い合わせている' do
605 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
606 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
607 r = Comic.edit @comic.id, @author
611 Comic.any_instance.stub(:own?).and_return(true)
612 c = Comic.edit @comic.id, @author.id
615 context '他人のコミックを開こうとしたとき' do
616 it '403Forbidden例外を返す' do
617 Comic.any_instance.stub(:own?).and_return(false)
619 Comic.edit @comic.id, @author
620 }.should raise_error(ActiveRecord::Forbidden)
623 context '存在しないコミックを開こうとしたとき' do
624 it '404RecordNotFound例外を返す' do
626 Comic.edit 110, @author
627 }.should raise_error(ActiveRecord::RecordNotFound)
631 describe '単体取得オプションに於いて' do
632 it 'includeキーを含んでいる' do
634 r.has_key?(:include).should be_true
637 r = Comic.show_opt[:include]
638 r.should have(2).items
641 r = Comic.show_opt[:include]
642 r.has_key?(:author).should be_true
645 r = Comic.show_opt[:include]
646 r.has_key?(:stories).should be_true
648 it 'ストーリーはコマを含んでいる' do
649 r = Comic.show_opt[:include]
650 r[:stories].has_key?(:panel).should be_true
653 describe 'json単体出力オプションに於いて' do
655 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
656 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
657 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
658 @sbt = FactoryGirl.create :speech_balloon_template
659 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
660 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
661 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
664 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
667 i.has_key?('stories').should be_true
669 it 'ストーリーはコマを含んでいる' do
670 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
673 s = i['stories'].first
674 s.has_key?('panel').should be_true
677 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
680 i.has_key?('author').should be_true
686 @comic = FactoryGirl.create :comic, :author_id => @author.id
687 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
688 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
689 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
690 @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
692 context 'つつがなく終わるとき' do
695 r = @comic.destroy_with_story
696 }.should change(Comic, :count).by(-1)
698 r = Comic.find @comic.id
701 it '自身にリンクしているストーリーをすべて削除する' do
703 r = @comic.destroy_with_story
704 }.should change(Story, :count).by(-1)
706 r = Story.find @story.id
710 r = @comic.destroy_with_story
714 context '削除に失敗したとき' do
716 Story.any_instance.stub(:destroy).with(any_args).and_return(false)
719 r = @comic.destroy_with_story
724 r = @comic.destroy_with_story
725 }.should_not change(Comic, :count)
727 r = @comic.destroy_with_story
728 }.should_not change(Story, :count)