OSDN Git Service

t#29705:create story show
[pettanr/pettanr.git] / spec / models / story_spec.rb
index 0837d3a..5983b87 100644 (file)
@@ -3,7 +3,7 @@ require 'spec_helper'
 #ストーリー
 describe Story do
   before do
-    FactoryGirl.create :admin
+    @admin = FactoryGirl.create :admin
     @sp = FactoryGirl.create :system_picture
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
@@ -151,6 +151,67 @@ describe Story do
       @story.own?(nil).should == false
     end
   end
+  
+  describe '閲覧許可に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @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\r
+    context '検査対象がnil(ゲスト)のとき' do
+      context 'クローズドモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 1
+        end
+        it '不許可を返す。' do\r
+          r = @story.visible?(nil)
+          r.should be_false
+        end\r
+      end\r
+      context 'オープンモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 0
+        end
+        it '公開コミックのストーリーなら許可する' do\r
+          Comic.any_instance.stub(:visible?).with(nil).and_return(true)\r
+          r = @story.visible?(nil)
+          r.should be_true
+        end\r
+        it '非公開コミックのストーリーなら許可しない' do\r
+          Comic.any_instance.stub(:visible?).with(nil).and_return(false)\r
+          r = @story.visible?(nil)
+          r.should be_false
+        end\r
+      end\r
+    end\r
+    context '検査対象が作家のとき' do
+      it '自分のコミックのストーリーなら許可する' do\r
+        Comic.any_instance.stub(:own?).with(@author).and_return(true)\r
+        Comic.any_instance.stub(:visible?).with(@author).and_return(true)\r
+        r = @story.visible?(@author)
+        r.should == true
+      end\r
+      it '他人の非公開コミックのストーリーなら許可しない' do\r
+        Comic.any_instance.stub(:own?).with(@other_author).and_return(false)\r
+        Comic.any_instance.stub(:visible?).with(@other_author).and_return(false)\r
+        r = @story.visible?(@other_author)
+        r.should == false
+      end\r
+      it '他人のコミックのストーリーでも公開なら許可する' do\r
+        Comic.any_instance.stub(:own?).with(@other_author).and_return(false)\r
+        Comic.any_instance.stub(:visible?).with(@other_author).and_return(true)\r
+        r = @story.visible?(@other_author)
+        r.should == true
+      end\r
+    end\r
+    context '検査対象がそれ以外のとき' do
+      it '不許可を返す。' do\r
+        r = @story.visible?(@admin)
+        r.should be_false
+      end\r
+    end\r
+  end\r
+  \r
   describe '一覧取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
@@ -376,6 +437,45 @@ describe Story do
     end
   end
   
+  describe '単体取得に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @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 'つつがなく終わるとき' do\r
+      it '単体取得オプションを利用している' do\r
+        Story.stub(:show_opt).with(any_args).and_return({})\r
+        Story.should_receive(:show_opt).with(any_args).exactly(1)\r
+        r = Story.show @story.id, @author
+      end\r
+      it '閲覧許可を問い合わせている' do\r
+        Story.any_instance.stub(:visible?).with(@author).and_return(true)\r
+        Story.any_instance.should_receive(:visible?).with(@author).exactly(1)\r
+        r = Story.show @story.id, @author
+      end\r
+    end\r
+    it '指定のストーリーを返す' do
+      l = Story.show @story.id, @author
+      l.should eq @story
+    end
+    context '他人のストーリーを開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        Story.any_instance.stub(:visible?).with(@other_author).and_return(false)
+        lambda{
+          Story.show @story.id, @other_author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないストーリーを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Story.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  
   describe '編集取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id