OSDN Git Service

t#30169:show, index action create all resource
[pettanr/pettanr.git] / spec / models / speech_balloon_spec.rb
index 395c77e..48955bf 100644 (file)
@@ -8,6 +8,12 @@ describe SpeechBalloon do
     @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
     
     @panel = FactoryGirl.create :panel, :author_id => @author.id
     @speech_balloon_template = FactoryGirl.create :speech_balloon_template
@@ -82,4 +88,335 @@ describe SpeechBalloon do
     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 '検査対象がnil(ゲスト)のとき' do
+      context 'クローズドモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 1
+        end
+        it '不許可を返す。' do
+          r = @sb.visible?(nil)
+          r.should be_false
+        end
+      end
+      context 'オープンモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 0
+        end
+        it '公開されたコマのフキダシなら許可する' do
+          Panel.any_instance.stub(:publish?).with(any_args).and_return(true)
+          r = @sb.visible?(nil)
+          r.should be_true
+        end
+        it 'れ以外なら不許可を返す' do
+          Panel.any_instance.stub(:publish?).with(any_args).and_return(false)
+          r = @sb.visible?(nil)
+          r.should be_false
+        end
+      end
+    end
+    context '検査対象が作家のとき' do
+      it '公開されたコマのフキダシなら許可する' do
+        Panel.any_instance.stub(:publish?).with(any_args).and_return(true)
+        r = @sb.visible?(@author)
+        r.should be_true
+      end
+      it 'れ以外なら不許可を返す' do
+        Panel.any_instance.stub(:publish?).with(any_args).and_return(false)
+        r = @sb.visible?(@author)
+        r.should be_false
+      end
+    end
+    context '検査対象が管理者のとき' do
+      it '許可を返す。' do
+        Panel.any_instance.stub(:publish?).with(any_args).and_return(false)
+        r = @sb.visible?(@admin)
+        r.should be_true
+      end
+    end
+    context '検査対象がそれ以外のとき' do
+      it '不許可を返す。' do
+        r = @sb.visible?(1)
+        r.should be_false
+      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
+        SpeechBalloon.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        SpeechBalloon.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        SpeechBalloon.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        SpeechBalloon.page_size('7').should eq 7
+      end
+      it 'nilの場合はSpeechBalloon.default_page_sizeになる' do
+        SpeechBalloon.page_size().should eq SpeechBalloon.default_page_size
+      end
+      it '0以下の場合はSpeechBalloon.default_page_sizeになる' do
+        SpeechBalloon.page_size('0').should eq SpeechBalloon.default_page_size
+      end
+      it 'SpeechBalloon.max_page_sizeを超えた場合はSpeechBalloon.max_page_sizeになる' do
+        SpeechBalloon.page_size('1000').should eq SpeechBalloon.max_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
+        SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
+        r = SpeechBalloon.list
+      end
+    end
+    it 'リストを返す' do
+      r = SpeechBalloon.list
+      r.should eq [@sb]
+    end
+    it '時系列で並んでいる' do
+      #公開されたコマのフキダシは(他人のフキダシであっても)含んでいる
+      hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
+      npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+      r = SpeechBalloon.list
+      r.should eq [npl, @sb]
+    end
+    it '非公開のコマのフキダシは自分のフキダシであっても含まない' do
+      hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
+      npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+      pl = SpeechBalloon.list
+      pl.should eq [@sb]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
+        @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
+        @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
+        @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
+        SpeechBalloon.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        pl = SpeechBalloon.list
+        pl.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        pl = SpeechBalloon.list(1)
+        pl.should eq [@sb5, @sb4]
+      end
+      it 'page=2なら中間2件を返す' do
+        pl = SpeechBalloon.list(2)
+        pl.should eq [@sb3, @sb2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        pl = SpeechBalloon.list(3)
+        pl.should eq [@sb]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
+        @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
+        @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
+        @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
+        SpeechBalloon.stub(:default_page_size).and_return(2)
+      end
+      it '通常は全件(5件)を返す' do
+        r = SpeechBalloon.list 5, 0
+        r.should have(5).items 
+      end
+    end
+  end
+  describe '一覧取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = SpeechBalloon.list_opt
+      r.has_key?(:include).should be_true
+    end
+    it '4つの項目を含んでいる' do
+      r = SpeechBalloon.list_opt[:include]
+      r.should have(4).items
+    end
+    it 'コマを含んでいる' do
+      r = SpeechBalloon.list_opt[:include]
+      r.has_key?(:panel).should be_true
+    end
+      it 'コマは作家を含んでいる' do
+        r = SpeechBalloon.list_opt[:include]
+        r[:panel].has_key?(:author).should be_true
+      end
+    it 'フキダシ枠を含んでいる' do
+      r = SpeechBalloon.list_opt[:include]
+      r.has_key?(:balloons).should be_true
+    end
+    it 'セリフを含んでいる' do
+      r = SpeechBalloon.list_opt[:include]
+      r.has_key?(:speeches).should be_true
+    end
+    it 'フキダシテンプレートを含んでいる' do
+      r = SpeechBalloon.list_opt[:include]
+      r.has_key?(:speech_balloon_template).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    before do
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @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 = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('panel').should be_true
+    end
+      it 'コマは作家を含んでいる' do
+        r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
+        j = JSON.parse r
+        i = j.first
+        s = i['panel']
+        s.has_key?('author').should be_true
+      end
+    it 'フキダシ枠を含んでいる' do
+      r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('balloons').should be_true
+    end
+    it 'セリフを含んでいる' do
+      r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('speeches').should be_true
+    end
+    it 'フキダシテンプレートを含んでいる' do
+      r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.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
+        SpeechBalloon.stub(:show_opt).with(any_args).and_return({:include => :panel})
+        SpeechBalloon.should_receive(:show_opt).with(any_args).exactly(1)
+        r = SpeechBalloon.show @sb.id, @author
+      end
+      it '閲覧許可を問い合わせている' do
+        SpeechBalloon.any_instance.stub(:visible?).with(any_args).and_return(true)
+        SpeechBalloon.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+        r = SpeechBalloon.show @sb.id, @author
+      end
+    end
+    it '指定のフキダシを返す' do
+      SpeechBalloon.any_instance.stub(:visible?).and_return(true)
+      pl = SpeechBalloon.show @sb.id, @author
+      pl.should eq @sb
+    end
+    context '閲覧許可が出なかったとき' do
+      it '403Forbidden例外を返す' do
+        SpeechBalloon.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          SpeechBalloon.show @sb.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないフキダシを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          SpeechBalloon.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '単体取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = SpeechBalloon.show_opt
+      r.has_key?(:include).should be_true
+    end
+    it '4つの項目を含んでいる' do
+      r = SpeechBalloon.show_opt[:include]
+      r.should have(4).items
+    end
+    it 'コマを含んでいる' do
+      r = SpeechBalloon.show_opt[:include]
+      r.has_key?(:panel).should be_true
+    end
+      it 'コマは作家を含んでいる' do
+        r = SpeechBalloon.show_opt[:include]
+        r[:panel].has_key?(:author).should be_true
+      end
+    it 'フキダシ枠を含んでいる' do
+      r = SpeechBalloon.show_opt[:include]
+      r.has_key?(:balloons).should be_true
+    end
+    it 'セリフを含んでいる' do
+      r = SpeechBalloon.show_opt[:include]
+      r.has_key?(:speeches).should be_true
+    end
+    it 'フキダシテンプレートを含んでいる' do
+      r = SpeechBalloon.show_opt[:include]
+      r.has_key?(:speech_balloon_template).should be_true
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    before do
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @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 = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('panel').should be_true
+    end
+      it 'コマは作家を含んでいる' do
+        r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
+        j = JSON.parse r
+        i = j
+        s = i['panel']
+        s.has_key?('author').should be_true
+      end
+    it 'フキダシ枠を含んでいる' do
+      r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('balloons').should be_true
+    end
+    it 'セリフを含んでいる' do
+      r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('speeches').should be_true
+    end
+    it 'フキダシテンプレートを含んでいる' do
+      r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('speech_balloon_template').should be_true
+    end
+  end
 end