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)
79 describe 'デフォルト値補充に於いて' do
80 it 'visibleが0になっている' do
81 @comic = FactoryGirl.build :comic, :visible => nil
83 @comic.visible.should eq 0
87 describe '上書き補充に於いて' do
89 @comic = FactoryGirl.build :comic, :author_id => nil
90 @comic.overwrite @author
91 @comic.author_id.should eq @author.id
95 describe 'ロールリストからの作家取得に於いて' do
96 context 'ロールがユーザのとき' do
97 it 'ユーザから作家を取得して、それを返す' do
98 r = Comic.get_author_from_roles([@user])
102 context 'ロールが作家のとき' do
104 r = Comic.get_author_from_roles([@author])
108 context 'ロールが絵師のとき' do
109 it '絵師から作家を取得できれば、それをを返す' do
110 r = Comic.get_author_from_roles([@artist])
113 it '絵師から作家を取得できないときnilを返す' do
114 @artist.author_id = nil
116 r = Comic.get_author_from_roles([@artist])
120 context 'ロールが管理者のとき' do
122 r = Comic.get_author_from_roles([@admin])
128 describe 'ロールリストからの絵師取得に於いて' do
129 context 'ロールがユーザのとき' do
130 it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
131 r = Comic.get_artist_from_roles([@user])
134 it '作家から絵師を取得できないときnilを返す' do
135 @artist.author_id = nil
137 r = Comic.get_artist_from_roles([@user])
141 context 'ロールが作家のとき' do
142 it 'そこから絵師を取得できれば、それを返す' do
143 r = Comic.get_artist_from_roles([@author])
146 it '作家から絵師を取得できないときnilを返す' do
147 @artist.author_id = nil
149 r = Comic.get_artist_from_roles([@author])
153 context 'ロールが絵師のとき' do
155 r = Comic.get_artist_from_roles([@artist])
159 context 'ロールが管理者のとき' do
161 r = Comic.get_artist_from_roles([@admin])
167 describe '所持判定に於いて' do
169 @comic = FactoryGirl.build :comic, :author_id => @author.id
171 context '事前チェックする' do
172 it '自身にロールリストからの作家取得を依頼している' do
173 Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
174 r = @comic.own?([@author])
177 context 'ロール内作家が取得できるとき' do
180 it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
181 Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
182 r = @comic.own?([@author])
185 it 'ロール内作家のidが自身の作家idと一致しないならno' do
186 Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
187 @comic.own?(@other_author).should be_false
190 context 'ロール内作家が取得できないとき' do
192 Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
195 r = @comic.own?([@author])
201 describe '読者用ロールチェックに於いて' do
203 @comic = FactoryGirl.build :comic, :author_id => @author.id
205 context 'ロールリストに作家が含まれるとき' do
206 it 'ロールリストがリストではないとき、リストにする trueを返す' do
207 r = @comic.reader_role_check(@author)
211 r = @comic.reader_role_check([@author])
215 context 'ロールリストに絵師が含まれるとき' do
217 r = @comic.reader_role_check([@artist])
221 context 'ロールリストにユーザが含まれるとき' do
223 r = @comic.reader_role_check([@user])
227 context 'ロールリストに管理者が含まれるとき' do
229 r = @comic.reader_role_check([@admin])
233 context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
235 r = @comic.reader_role_check([nil])
241 describe '素材読者用ロールチェックに於いて' do
243 @comic = FactoryGirl.build :comic, :author_id => @author.id
245 context 'ロールリストに作家が含まれるとき' do
246 it 'ロールリストがリストではないとき、リストにする trueを返す' do
247 r = @comic.resource_reader_role_check(@author)
251 r = @comic.resource_reader_role_check([@author])
255 context 'ロールリストに絵師が含まれるとき' do
257 r = @comic.resource_reader_role_check([@artist])
261 context 'ロールリストにユーザが含まれるとき' do
263 r = @comic.resource_reader_role_check([@user])
267 context 'ロールリストに管理者が含まれるとき' do
269 r = @comic.resource_reader_role_check([@admin])
273 context 'ロールリストに借手が含まれるとき' do
275 r = @comic.resource_reader_role_check([@demand_user])
279 context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
281 r = @comic.resource_reader_role_check([nil])
287 describe '閲覧許可に於いて' do
289 @comic = FactoryGirl.build :comic, :author_id => @author.id
291 context 'オープンモードのとき' do
293 MagicNumber['run_mode'] = 0
295 it '自身にゲスト用ロールチェックを問い合わせしている' do
296 Comic.any_instance.stub(:guest_role_check).and_return(true)
297 Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
298 r = @comic.visible?([@author])
300 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
301 Comic.any_instance.stub(:guest_role_check).and_return(false)
302 r = @comic.visible?([@author])
306 context 'クローズドモードのとき' do
308 MagicNumber['run_mode'] = 1
310 it '自身に読者用ロールチェックを問い合わせしている' do
311 Comic.any_instance.stub(:reader_role_check).and_return(true)
312 Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
313 r = @comic.visible?([@author])
315 it '読者用ロールチェックが失敗したとき、falseを返す' do
316 Comic.any_instance.stub(:reader_role_check).and_return(false)
317 r = @comic.visible?([@author])
321 context '事前チェックする' do
323 MagicNumber['run_mode'] = 1
324 Comic.any_instance.stub(:reader_role_check).and_return(true)
325 Comic.any_instance.stub(:own?).and_return(true)
327 it '自身に所持判定を問い合わせしている' do
328 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
329 r = @comic.visible?([@author])
332 context 'つつがなく終わるとき' do
334 MagicNumber['run_mode'] = 1
335 Comic.any_instance.stub(:reader_role_check).and_return(true)
337 it '自分のコミックなら許可する' do
338 Comic.any_instance.stub(:own?).and_return(true)
339 Comic.any_instance.stub(:visible).and_return(0)
340 r = @comic.visible?([@author])
343 it '他人の非公開コミックなら許可しない' do
344 Comic.any_instance.stub(:own?).and_return(false)
345 Comic.any_instance.stub(:visible).and_return(0)
346 r = @comic.visible?([@author])
349 it '他人のコミックでも公開なら許可する' do
350 Comic.any_instance.stub(:own?).and_return(false)
351 Comic.any_instance.stub(:visible).and_return(1)
352 r = @comic.visible?([@author])
358 describe '一覧取得に於いて' do
360 @comic = FactoryGirl.create :comic, :author_id => @author.id
362 context 'page補正について' do
363 it '文字列から数値に変換される' do
364 Comic.page('8').should eq 8
367 Comic.page().should eq 1
370 Comic.page('0').should eq 1
373 context 'page_size補正について' do
374 it '文字列から数値に変換される' do
375 Comic.page_size('7').should eq 7
377 it 'nilの場合はComic.default_page_sizeになる' do
378 Comic.page_size().should eq Comic.default_page_size
380 it '0以下の場合はComic.default_page_sizeになる' do
381 Comic.page_size('0').should eq Comic.default_page_size
383 it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
384 Comic.page_size('1000').should eq Comic.max_page_size
387 context 'つつがなく終わるとき' do
388 it '一覧取得オプションを利用している' do
389 Comic.stub(:list_opt).with(any_args).and_return({})
390 Comic.should_receive(:list_opt).with(any_args).exactly(1)
398 it '非公開コミックは(自分のコミックであっても)含んでいない' do
399 FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
404 #公開コミックは(他人のコミックであっても)含んでいる
405 v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
407 c.should eq [v, @comic]
409 context 'DBに5件あって1ページの件数を2件に変えたとして' do
411 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
412 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
413 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
414 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
415 Comic.stub(:default_page_size).and_return(2)
419 c.should have(2).items
421 it 'page=1なら末尾2件を返す' do
424 c.should eq [@comic5, @comic4]
426 it 'page=2なら中間2件を返す' do
428 c.should eq [@comic3, @comic2]
430 it 'page=3なら先頭1件を返す' do
435 context 'DBに5件あって1ページの件数を2件に変えたとして' do
437 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
438 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
439 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
440 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
441 Comic.stub(:default_page_size).and_return(2)
443 it '件数0は全件(5件)を返す' do
445 r.should have(5).items
449 describe '一覧取得オプションに於いて' do
450 it 'includeキーを含んでいる' do
452 r.has_key?(:include).should be_true
455 r = Comic.list_opt[:include]
456 r.should have(2).items
459 r = Comic.list_opt[:include]
460 r.has_key?(:stories).should be_true
462 it 'ストーリーはコマを含んでいる' do
463 r = Comic.list_opt[:include]
464 r[:stories].has_key?(:panel).should be_true
467 r = Comic.list_opt[:include]
468 r.has_key?(:author).should be_true
471 describe 'json一覧出力オプションに於いて' do
473 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
474 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
475 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
476 @sbt = FactoryGirl.create :speech_balloon_template
477 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
478 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
479 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
482 r = Comic.list.to_json Comic.list_json_opt
485 i.has_key?('stories').should be_true
487 it 'ストーリーはコマを含んでいる' do
488 r = Comic.list.to_json Comic.list_json_opt
491 s = i['stories'].first
492 s.has_key?('panel').should be_true
495 r = Comic.list.to_json Comic.list_json_opt
498 i.has_key?('author').should be_true
502 describe '自分のコミック一覧取得に於いて' do
504 @comic = FactoryGirl.create :comic, :author_id => @author.id
506 context 'つつがなく終わるとき' do
507 it '一覧取得オプションを利用している' do
508 Comic.stub(:list_opt).with(any_args).and_return({})
509 Comic.should_receive(:list_opt).with(any_args).exactly(1)
510 r = Comic.mylist @author
514 c = Comic.mylist @author
518 nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
519 cl = Comic.mylist @author
520 cl.should eq [nc, @comic]
522 it '他人のコミックは公開でも含まない' do
523 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
524 cl = Comic.mylist @author
525 cl.should eq [@comic]
527 it '自分のコミックは非公開でも含んでいる' do
528 nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
529 cl = Comic.mylist @author
530 cl.should eq [nc, @comic]
532 context 'DBに5件あって1ページの件数を2件に変えたとして' do
534 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
535 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
536 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
537 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
540 c = Comic.mylist @author, 1, 2
541 c.should have(2).items
543 it 'page=1なら末尾2件を返す' do
545 c = Comic.mylist(@author, 1, 2)
546 c.should eq [@comic5, @comic4]
548 it 'page=2なら中間2件を返す' do
549 c = Comic.mylist(@author, 2, 2)
550 c.should eq [@comic3, @comic2]
552 it 'page=3なら先頭1件を返す' do
553 c = Comic.mylist(@author, 3, 2)
557 context 'DBに5件あって1ページの件数を0件に変えたとして' do
559 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
560 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
561 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
562 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
563 Author.stub(:default_comic_page_size).and_return(2)
566 r = Comic.mylist @author, 5, 0
567 r.should have(5).items
572 describe '単体取得に於いて' do
574 @comic = FactoryGirl.create :comic, :author_id => @author.id
576 context 'つつがなく終わるとき' do
577 it '単体取得オプションを利用している' do
578 Comic.stub(:show_opt).with(any_args).and_return({})
579 Comic.should_receive(:show_opt).with(any_args).exactly(1)
580 r = Comic.show @comic.id, @author
582 it '閲覧許可を問い合わせている' do
583 Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
584 Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
585 r = Comic.show @comic.id, @author
589 c = Comic.show @comic.id, @author
592 context '閲覧許可が出なかったとき' do
593 it '403Forbidden例外を返す' do
594 Comic.any_instance.stub(:visible?).and_return(false)
596 Comic.show @comic.id, @author
597 }.should raise_error(ActiveRecord::Forbidden)
600 context '存在しないコミックを開こうとしたとき' do
601 it '404RecordNotFound例外を返す' do
603 Comic.show 110, @author
604 }.should raise_error(ActiveRecord::RecordNotFound)
609 describe '編集取得に於いて' do
611 @comic = FactoryGirl.create :comic, :author_id => @author.id
613 context 'つつがなく終わるとき' do
614 it '単体取得オプションを利用している' do
615 Comic.stub(:show_opt).with(any_args).and_return({})
616 Comic.should_receive(:show_opt).with(any_args).exactly(1)
617 r = Comic.edit @comic.id, @author
619 it '所持判定を問い合わせている' do
620 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
621 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
622 r = Comic.edit @comic.id, @author
626 Comic.any_instance.stub(:own?).and_return(true)
627 c = Comic.edit @comic.id, @author.id
630 context '他人のコミックを開こうとしたとき' do
631 it '403Forbidden例外を返す' do
632 Comic.any_instance.stub(:own?).and_return(false)
634 Comic.edit @comic.id, @author
635 }.should raise_error(ActiveRecord::Forbidden)
638 context '存在しないコミックを開こうとしたとき' do
639 it '404RecordNotFound例外を返す' do
641 Comic.edit 110, @author
642 }.should raise_error(ActiveRecord::RecordNotFound)
646 describe '単体取得オプションに於いて' do
647 it 'includeキーを含んでいる' do
649 r.has_key?(:include).should be_true
652 r = Comic.show_opt[:include]
653 r.should have(2).items
656 r = Comic.show_opt[:include]
657 r.has_key?(:author).should be_true
660 r = Comic.show_opt[:include]
661 r.has_key?(:stories).should be_true
663 it 'ストーリーはコマを含んでいる' do
664 r = Comic.show_opt[:include]
665 r[:stories].has_key?(:panel).should be_true
668 describe 'json単体出力オプションに於いて' do
670 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
671 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
672 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
673 @sbt = FactoryGirl.create :speech_balloon_template
674 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
675 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
676 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
679 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
682 i.has_key?('stories').should be_true
684 it 'ストーリーはコマを含んでいる' do
685 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
688 s = i['stories'].first
689 s.has_key?('panel').should be_true
692 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
695 i.has_key?('author').should be_true
701 @comic = FactoryGirl.create :comic, :author_id => @author.id
702 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
703 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
704 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
705 @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
707 context 'つつがなく終わるとき' do
710 r = @comic.destroy_with_story
711 }.should change(Comic, :count).by(-1)
713 r = Comic.find @comic.id
716 it '自身にリンクしているストーリーをすべて削除する' do
718 r = @comic.destroy_with_story
719 }.should change(Story, :count).by(-1)
721 r = Story.find @story.id
725 r = @comic.destroy_with_story
729 context '削除に失敗したとき' do
731 Story.any_instance.stub(:destroy).with(any_args).and_return(false)
734 r = @comic.destroy_with_story
739 r = @comic.destroy_with_story
740 }.should_not change(Comic, :count)
742 r = @comic.destroy_with_story
743 }.should_not change(Story, :count)