X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fmodels%2Fballoon_spec.rb;h=8ceedb10b3e300c2a0560191fd1b41e36fe0318f;hb=a9ec68d632929b0cb3c5391309180e41ef38b048;hp=17f0b58ef6390d38d55cfae4961fef51241a1cb5;hpb=2bd8e046bbbdd6d620af0637c3291cc7d787b0c5;p=pettanr%2Fpettanr.git diff --git a/spec/models/balloon_spec.rb b/spec/models/balloon_spec.rb index 17f0b58e..8ceedb10 100644 --- a/spec/models/balloon_spec.rb +++ b/spec/models/balloon_spec.rb @@ -1,22 +1,28 @@ # -*- encoding: utf-8 -*- require 'spec_helper' -#セリフ +#フキダシ枠 describe Balloon do before do @admin = FactoryGirl.create :admin @user = FactoryGirl.create( :user_yas) @author = FactoryGirl.create :author, :user_id => @user.id @artist = FactoryGirl.create :artist_yas, :author_id => @author.id + @other_user = FactoryGirl.create( :user_yas) + @other_author = FactoryGirl.create :author, :user_id => @other_user.id + @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id + @sp = FactoryGirl.create :system_picture + @lg = FactoryGirl.create :license_group + @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id - @balloon = FactoryGirl.create :panel @speech_balloon_template = FactoryGirl.create :speech_balloon_template - @speech_balloon = FactoryGirl.create :speech_balloon, :panel_id => @balloon.id, :speech_balloon_template_id => @speech_balloon_template.id - @system_picture = FactoryGirl.create :system_picture + @panel = FactoryGirl.create :panel, :author_id => @author.id end describe '検証に於いて' do before do - @balloon = FactoryGirl.build :balloon, :speech_balloon_id => @speech_balloon.id, :system_picture_id => @system_picture.id + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + @balloon = FactoryGirl.build :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id end context 'オーソドックスなデータのとき' do it '下限データが通る' do @@ -24,6 +30,7 @@ describe Balloon do @balloon.y = 0 @balloon.width = 1 @balloon.height = 1 + @balloon.r = 0 @balloon.should be_valid end it '上限データが通る' do @@ -31,6 +38,7 @@ describe Balloon do @balloon.y = 99999 @balloon.width = 99999 @balloon.height = 99999 + @balloon.r = 99999 @balloon.should be_valid end end @@ -38,7 +46,7 @@ describe Balloon do context 'speech_balloon_idを検証するとき' do #ネストの保存はnilを許可しなければならないので数値チェックだけ it 'テストデータの確認' do - @balloon.speech_balloon_id = @speech_balloon.id + @balloon.speech_balloon_id = @sb.id @balloon.should be_valid end it '数値でなければ失敗する' do @@ -132,8 +140,398 @@ describe Balloon do @balloon.should_not be_valid end end + context 'rを検証するとき' do + it 'nullなら失敗する' do + @balloon.r = nil + @balloon.should_not be_valid + end + it '数値でなければ失敗する' do + @balloon.r = 'a' + @balloon.should_not be_valid + end + it '0なら通る' do + @balloon.r = '0' + @balloon.should be_valid + end + it '負でも通る' do + @balloon.r = -1 + @balloon.should be_valid + end + end context 'settingsを検証するとき' do end end + describe '文字コード検証に於いて' do + before do + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + @balloon = FactoryGirl.build :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id + end + + context 'settingsを検証するとき' do + it 'Shift JISなら失敗する' do + @balloon.settings = "\x83G\x83r\x83]\x83D" + lambda{ + @balloon.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + end + + describe '閲覧許可に於いて' do + before do + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + end + context 'オープンモードのとき' do + before do + MagicNumber['run_mode'] = 0 + end + it '自身にゲスト用ロールチェックを問い合わせしている' do + Balloon.any_instance.stub(:guest_role_check).and_return(true) + Balloon.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1) + r = @balloon.visible?([@author]) + end + it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do + Balloon.any_instance.stub(:guest_role_check).and_return(false) + r = @balloon.visible?([@author]) + r.should be_false + end + end + context 'クローズドモードのとき' do + before do + MagicNumber['run_mode'] = 1 + end + it '自身に読者用ロールチェックを問い合わせしている' do + Balloon.any_instance.stub(:reader_role_check).and_return(true) + Balloon.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1) + r = @balloon.visible?([@author]) + end + it '読者用ロールチェックが失敗したとき、falseを返す' do + Balloon.any_instance.stub(:reader_role_check).and_return(false) + r = @balloon.visible?([@author]) + r.should be_false + end + end + context '事前チェックする' do + before do + MagicNumber['run_mode'] = 1 + Balloon.any_instance.stub(:reader_role_check).and_return(true) + end + it '自身のコマに所持判定を問い合わせしている' do + Panel.any_instance.stub(:own?).and_return(true) + Panel.any_instance.should_receive(:own?).with(any_args).exactly(1) + r = @balloon.visible?([@author]) + end + it '自身のコマに閲覧許可を問い合わせしている' do + Panel.any_instance.stub(:own?).and_return(false) + Panel.any_instance.stub(:visible?).and_return(true) + Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1) + r = @balloon.visible?([@author]) + end + end + context 'つつがなく終わるとき' do + before do + MagicNumber['run_mode'] = 1 + Panel.any_instance.stub(:reader_role_check).and_return(true) + end + it '自分のコマのコマ絵なら許可する' do + Panel.any_instance.stub(:own?).and_return(true) + Panel.any_instance.stub(:visible?).and_return(false) + r = @balloon.visible?([@author]) + r.should be_true + end + it '他人の非公開コマのコマ絵なら許可しない' do + Panel.any_instance.stub(:own?).and_return(false) + Panel.any_instance.stub(:visible?).and_return(false) + r = @balloon.visible?([@author]) + r.should be_false + end + it '他人のコマのコマ絵でも公開なら許可する' do + Panel.any_instance.stub(:own?).and_return(false) + Panel.any_instance.stub(:visible?).and_return(true) + r = @balloon.visible?([@author]) + r.should be_true + end + end + end + + describe '一覧取得に於いて' do + before do + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + end + context 'page補正について' do + it '文字列から数値に変換される' do + Balloon.page('8').should eq 8 + end + it 'nilの場合は1になる' do + Balloon.page().should eq 1 + end + it '0以下の場合は1になる' do + Balloon.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + Balloon.page_size('7').should eq 7 + end + it 'nilの場合はBalloon.default_page_sizeになる' do + Balloon.page_size().should eq Balloon.default_page_size + end + it '0以下の場合はBalloon.default_page_sizeになる' do + Balloon.page_size('0').should eq Balloon.default_page_size + end + it 'Balloon.max_page_sizeを超えた場合はBalloon.max_page_sizeになる' do + Balloon.page_size('1000').should eq Balloon.max_page_size + end + end + context 'つつがなく終わるとき' do + it '一覧取得オプションを利用している' do + Balloon.stub(:list_opt).with(any_args).and_return({:include => {:speech_balloon => {:panel => {}}}}) + Balloon.should_receive(:list_opt).with(any_args).exactly(1) + r = Balloon.list + end + end + it 'リストを返す' do + r = Balloon.list + r.should eq [@balloon] + end + it '時系列で並んでいる' do + #公開されたコマのフキダシ枠は(他人のフキダシ枠であっても)含んでいる + hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1 + nsb = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100 + npl = FactoryGirl.create :balloon, :speech_balloon_id => nsb.id, :updated_at => Time.now + 100 + r = Balloon.list + r.should eq [npl, @balloon] + end + it '非公開のコマのフキダシ枠は自分のフキダシ枠であっても含まない' do + hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0 + nsb = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100 + npl = FactoryGirl.create :balloon, :speech_balloon_id => nsb.id, :updated_at => Time.now + 100 + pl = Balloon.list + pl.should eq [@balloon] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @balloon2 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 100 + @balloon3 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 200 + @balloon4 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 300 + @balloon5 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 400 + Balloon.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + pl = Balloon.list + pl.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + pl = Balloon.list(1) + pl.should eq [@balloon5, @balloon4] + end + it 'page=2なら中間2件を返す' do + pl = Balloon.list(2) + pl.should eq [@balloon3, @balloon2] + end + it 'page=3なら先頭1件を返す' do + pl = Balloon.list(3) + pl.should eq [@balloon] + end + end + context 'DBに5件あって1ページの件数を0件に変えたとして' do + before do + @balloon2 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 100 + @balloon3 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 200 + @balloon4 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 300 + @balloon5 = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :updated_at => Time.now + 400 + Balloon.stub(:default_page_size).and_return(2) + end + it '通常は全件(5件)を返す' do + r = Balloon.list 5, 0 + r.should have(5).items + end + end + end + describe '一覧取得オプションに於いて' do + it 'includeキーを含んでいる' do + r = Balloon.list_opt + r.has_key?(:include).should be_true + end + it '1つの項目を含んでいる' do + r = Balloon.list_opt[:include] + r.should have(1).items + end + it 'フキダシを含んでいる' do + r = Balloon.list_opt[:include] + r.has_key?(:speech_balloon).should be_true + end + it 'フキダシはコマを含んでいる' do + r = Balloon.list_opt[:include] + r[:speech_balloon].has_key?(:panel).should be_true + end + it 'コマは作家を含んでいる' do + r = Balloon.list_opt[:include] + r[:speech_balloon][:panel].has_key?(:author).should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Balloon.list_opt[:include] + r[:speech_balloon].has_key?(:speech).should be_true + end + it 'フキダシはフキダシテンプレートを含んでいる' do + r = Balloon.list_opt[:include] + r[:speech_balloon].has_key?(:speech_balloon_template).should be_true + end + end + describe 'json一覧出力オプションに於いて' do + before do + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + end + it 'フキダシを含んでいる' do + r = Balloon.list.to_json Balloon.list_json_opt + j = JSON.parse r + i = j.first + i.has_key?('speech_balloon').should be_true + end + it 'フキダシはコマを含んでいる' do + r = Balloon.list.to_json Balloon.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon'] + s.has_key?('panel').should be_true + end + it 'コマは作家を含んでいる' do + r = Balloon.list.to_json Balloon.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon']['panel'] + s.has_key?('author').should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Balloon.list.to_json Balloon.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon'] + s.has_key?('speech').should be_true + end + it 'フキダシはフキダシテンプレートを含んでいる' do + r = Balloon.list.to_json Balloon.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon'] + s.has_key?('speech_balloon_template').should be_true + end + end + + describe '単体取得に於いて' do + before do + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + end + context 'つつがなく終わるとき' do + it '単体取得オプションを利用している' do + Balloon.stub(:show_opt).with(any_args).and_return({:include => {:speech_balloon => {:panel => {}}}}) + Balloon.should_receive(:show_opt).with(any_args).exactly(1) + r = Balloon.show @balloon.id, @author + end + it '閲覧許可を問い合わせている' do + Balloon.any_instance.stub(:visible?).with(any_args).and_return(true) + Balloon.any_instance.should_receive(:visible?).with(any_args).exactly(1) + r = Balloon.show @balloon.id, @author + end + end + it '指定のフキダシ枠を返す' do + Balloon.any_instance.stub(:visible?).and_return(true) + pl = Balloon.show @balloon.id, @author + pl.should eq @balloon + end + context '閲覧許可が出なかったとき' do + it '403Forbidden例外を返す' do + Balloon.any_instance.stub(:visible?).and_return(false) + lambda{ + Balloon.show @balloon.id, @author + }.should raise_error(ActiveRecord::Forbidden) + end + end + context '存在しないフキダシ枠を開こうとしたとき' do + it '404RecordNotFound例外を返す' do + lambda{ + Balloon.show 110, @author + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + describe '単体取得オプションに於いて' do + it 'includeキーを含んでいる' do + r = Balloon.show_opt + r.has_key?(:include).should be_true + end + it '1つの項目を含んでいる' do + r = Balloon.show_opt[:include] + r.should have(1).items + end + it 'フキダシを含んでいる' do + r = Balloon.show_opt[:include] + r.has_key?(:speech_balloon).should be_true + end + it 'フキダシはコマを含んでいる' do + r = Balloon.show_opt[:include] + r[:speech_balloon].has_key?(:panel).should be_true + end + it 'コマは作家を含んでいる' do + r = Balloon.show_opt[:include] + r[:speech_balloon][:panel].has_key?(:author).should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Balloon.show_opt[:include] + r[:speech_balloon].has_key?(:speech).should be_true + end + it 'フキダシはフキダシテンプレートを含んでいる' do + r = Balloon.show_opt[:include] + r[:speech_balloon].has_key?(:speech_balloon_template).should be_true + end + end + describe 'json単体出力オプションに於いて' do + before do + @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id + @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id + end + it 'フキダシを含んでいる' do + r = Balloon.show(@balloon.id, @author).to_json Balloon.show_json_opt + j = JSON.parse r + i = j + i.has_key?('speech_balloon').should be_true + end + it 'コマを含んでいる' do + r = Balloon.show(@balloon.id, @author).to_json Balloon.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + i.has_key?('panel').should be_true + end + it 'コマは作家を含んでいる' do + r = Balloon.show(@balloon.id, @author).to_json Balloon.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + s = i['panel'] + s.has_key?('author').should be_true + end + it 'セリフを含んでいる' do + r = Balloon.show(@balloon.id, @author).to_json Balloon.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + i.has_key?('speech').should be_true + end + it 'フキダシテンプレートを含んでいる' do + r = Balloon.show(@balloon.id, @author).to_json Balloon.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + i.has_key?('speech_balloon_template').should be_true + end + end end