OSDN Git Service

t#31085:raise err when input invalid encoding string
[pettanr/pettanr.git] / spec / models / story_spec.rb
index 9c747d7..089c437 100644 (file)
@@ -141,14 +141,33 @@ describe Story do
       @panelo = FactoryGirl.create :panel, :author_id => @other_author.id
       @storyo = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @comico.id, :panel_id => @panelo.id
     end
-    it '自分のストーリーならyes' do
-      @story.own?(@author).should == true
+    context '事前チェックする' do
+      it '自身にロールリストからの作家取得を依頼している' do
+        Story.should_receive(:get_author_from_roles).with(any_args).exactly(1)
+        r = @story.own?([@author])
+      end
     end
-    it '他人のストーリーならno' do
-      @storyo.own?(@author).should == false
+    context 'ロール内作家が取得できるとき' do
+      before do
+      end
+      it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
+        Story.stub(:get_author_from_roles).with(any_args).and_return(@author)
+        r = @story.own?([@author])
+        r.should be_true
+      end
+      it 'ロール内作家のidが自身の作家idと一致しないならno' do
+        Story.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
+        @story.own?(@other_author).should be_false
+      end
     end
-    it 'パラメータが作家でないならno' do
-      @story.own?(nil).should == false
+    context 'ロール内作家が取得できないとき' do
+      before do
+        Story.stub(:get_author_from_roles).with(any_args).and_return(nil)
+      end
+      it 'Falseを返す' do
+        r = @story.own?([@author])
+        r.should be_false
+      end
     end
   end
   
@@ -158,57 +177,76 @@ describe Story do
       @panel = FactoryGirl.create :panel, :author_id => @author.id
       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
     end
-    context '検査対象がnil(ゲスト)のとき' do
-      context 'クローズドモードのとき' do
-        before do
-          MagicNumber['run_mode'] = 1
-        end
-        it '不許可を返す。' do
-          r = @story.visible?(nil)
-          r.should be_false
-        end
+    context 'オープンモードのとき' do
+      before do
+        MagicNumber['run_mode'] = 0
       end
-      context 'オープンモードのとき' do
-        before do
-          MagicNumber['run_mode'] = 0
-        end
-        it '公開コミックのストーリーなら許可する' do
-          Comic.any_instance.stub(:visible?).with(nil).and_return(true)
-          r = @story.visible?(nil)
-          r.should be_true
-        end
-        it '非公開コミックのストーリーなら許可しない' do
-          Comic.any_instance.stub(:visible?).with(nil).and_return(false)
-          r = @story.visible?(nil)
-          r.should be_false
-        end
+      it '自身にゲスト用ロールチェックを問い合わせしている' do
+        Story.any_instance.stub(:guest_role_check).and_return(true)
+        Story.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
+        r = @story.visible?([@author])
+      end
+      it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
+        Story.any_instance.stub(:guest_role_check).and_return(false)
+        r = @story.visible?([@author])
+        r.should be_false
       end
     end
-    context '検査対象が作家のとき' do
-      it '自分のコミックのストーリーなら許可する' do
-        Comic.any_instance.stub(:own?).with(@author).and_return(true)
-        Comic.any_instance.stub(:visible?).with(@author).and_return(true)
-        r = @story.visible?(@author)
-        r.should == true
+    context 'クローズドモードのとき' do
+      before do
+        MagicNumber['run_mode'] = 1
       end
-      it '他人の非公開コミックのストーリーなら許可しない' do
-        Comic.any_instance.stub(:own?).with(@other_author).and_return(false)
-        Comic.any_instance.stub(:visible?).with(@other_author).and_return(false)
-        r = @story.visible?(@other_author)
-        r.should == false
+      it '自身に読者用ロールチェックを問い合わせしている' do
+        Story.any_instance.stub(:reader_role_check).and_return(true)
+        Story.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
+        r = @story.visible?([@author])
       end
-      it '他人のコミックのストーリーでも公開なら許可する' do
-        Comic.any_instance.stub(:own?).with(@other_author).and_return(false)
-        Comic.any_instance.stub(:visible?).with(@other_author).and_return(true)
-        r = @story.visible?(@other_author)
-        r.should == true
+      it '読者用ロールチェックが失敗したとき、falseを返す' do
+        Story.any_instance.stub(:reader_role_check).and_return(false)
+        r = @story.visible?([@author])
+        r.should be_false
       end
     end
-    context '検査対象がそれ以外のとき' do
-      it '不許可を返す。' do
-        r = @story.visible?(@admin)
+    context '事前チェックする' do
+      before do
+        MagicNumber['run_mode'] = 1
+        Story.any_instance.stub(:reader_role_check).and_return(true)
+      end
+      it '自身のコミックに所持判定を問い合わせしている' do
+        Comic.any_instance.stub(:own?).and_return(true)
+        Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
+        r = @story.visible?([@author])
+      end
+      it '自身のコミックに閲覧許可を問い合わせしている' do
+        Comic.any_instance.stub(:own?).and_return(false)
+        Comic.any_instance.stub(:visible?).and_return(true)
+        Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+        r = @story.visible?([@author])
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        MagicNumber['run_mode'] = 1
+        Story.any_instance.stub(:reader_role_check).and_return(true)
+      end
+      it '自分のコミックのストーリーなら許可する' do
+        Comic.any_instance.stub(:own?).and_return(true)
+        Comic.any_instance.stub(:visible).and_return(0)
+        r = @story.visible?([@author])
+        r.should be_true
+      end
+      it '他人の非公開コミックのストーリーなら許可しない' do
+        Comic.any_instance.stub(:own?).and_return(false)
+        Comic.any_instance.stub(:visible).and_return(0)
+        r = @story.visible?([@author])
         r.should be_false
       end
+      it '他人のコミックのストーリーでも公開なら許可する' do
+        Comic.any_instance.stub(:own?).and_return(false)
+        Comic.any_instance.stub(:visible).and_return(1)
+        r = @story.visible?([@author])
+        r.should be_true
+      end
     end
   end
   
@@ -782,6 +820,18 @@ describe Story do
       r[@pp.picture_id].should eq @p
       r[@pp2.picture_id].should eq @p2
     end
+    context 'コマが削除されているときき' do
+      before do
+        @panel2.destroy
+      end
+      it '削除されているコマは無視する' do
+        r = Story.licensed_pictures [@story, @story2]
+        r.is_a?(Hash).should be_true
+        r.should have(1).items
+        r[@pp.picture_id].should eq @p
+        r[@pp2.picture_id].should be_nil
+      end
+    end
   end
   
   describe 't補充値に於いて' do
@@ -1398,26 +1448,32 @@ describe Story do
     end
     context 'コミックで引っかかるとき' do
       it 'falseを返す' do
-        @story.comic_id = nil
+        Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
+        Comic.any_instance.stub(:own?).with(any_args).and_return(false)
         r = @story.allow?
         r.should be_false
       end
+    end
+    context 'コマで引っかかるとき' do
       it 'falseを返す' do
-        Comic.any_instance.stub(:own?).with(any_args).and_return(false)
+        Comic.any_instance.stub(:own?).with(any_args).and_return(true)
+        Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
         r = @story.allow?
         r.should be_false
       end
     end
-    context 'コマで引っかかるとき' do
-      it 'falseを返す' do
+    context 'コミックまたはコマが指定されていなかったとき' do
+      it 'nilを返す' do
+        Comic.any_instance.stub(:own?).with(any_args).and_return(true)
         @story.panel_id = nil
         r = @story.allow?
-        r.should be_false
+        r.should eq nil
       end
-      it 'falseを返す' do
-        Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
+      it 'nilを返す' do
+        Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
+        @story.comic_id = nil
         r = @story.allow?
-        r.should be_false
+        r.should eq nil
       end
     end
   end
@@ -1685,6 +1741,24 @@ describe Story do
           @story.destroy_and_shorten
         }.should change(Story, :count ).by(-1)
       end
+      it 'Trueを返す' do
+        r = @story.destroy_and_shorten
+        r.should be_true 
+      end
+    end
+    context '削除に失敗したとき' do
+      before do
+        Story.any_instance.stub(:destroy).and_return(false)
+      end
+      it 'ロールバックされる' do
+        lambda{
+          @story.destroy_and_shorten
+        }.should_not change(Story, :count )
+      end
+      it 'Falseを返す' do
+        r = @story.destroy_and_shorten
+        r.should be_false
+      end
     end
     #連携テスト。切り詰めが直接DBをいじる
     context '2件で先頭を削除したとき' do