OSDN Git Service

t#31281:add comic.description
[pettanr/pettanr.git] / spec / models / panel_spec.rb
index 15e256b..308bc3b 100644 (file)
@@ -171,6 +171,22 @@ describe Panel do
     end\r
   end\r
   \r
+  describe '文字コード検証に於いて' do\r
+    before do\r
+      @panel = FactoryGirl.build :panel, :author_id => @author.id\r
+    end\r
+    \r
+    context 'captionを検証するとき' do\r
+      it 'Shift JISなら失敗する' do\r
+        @panel.caption = "\x83G\x83r\x83]\x83D"\r
+        lambda{\r
+          @panel.valid_encode\r
+        }.should raise_error(Pettanr::BadRequest)\r
+      end\r
+    end\r
+    \r
+  end\r
+  \r
   describe 'デフォルト値補充に於いて' do\r
     before do\r
       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
@@ -201,75 +217,115 @@ describe Panel do
     \r
   end\r
   \r
-  describe '作者判定に於いて' do\r
+  describe '所持判定に於いて' do\r
     before do\r
       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
     end\r
-    it '自作のコマならyes' do\r
-      @panel.own?(@author).should == true\r
+    context '事前チェックする' do\r
+      it '自身にロールリストからの作家取得を依頼している' do\r
+        Panel.should_receive(:get_author_from_roles).with(any_args).exactly(1)\r
+        r = @panel.own?([@author])\r
+      end\r
     end\r
-    it '他人のコマならno' do\r
-      @panel.own?(@other_author).should == false\r
+    context 'ロール内作家が取得できるとき' do\r
+      before do\r
+      end\r
+      it 'ロール内作家のidが自身の作家idと一致するなら許可する' do\r
+        Panel.stub(:get_author_from_roles).with(any_args).and_return(@author)\r
+        r = @panel.own?([@author])\r
+        r.should be_true\r
+      end\r
+      it 'ロール内作家のidが自身の作家idと一致しないならno' do\r
+        Panel.stub(:get_author_from_roles).with(any_args).and_return(@other_author)\r
+        @panel.own?(@other_author).should be_false\r
+      end\r
     end\r
-    it 'パラメータが作家でないならno' do\r
-      @panel.own?(nil).should == false\r
+    context 'ロール内作家が取得できないとき' do\r
+      before do\r
+        Panel.stub(:get_author_from_roles).with(any_args).and_return(nil)\r
+      end\r
+      it 'Falseを返す' do\r
+        r = @panel.own?([@author])\r
+        r.should be_false\r
+      end\r
     end\r
   end\r
   \r
   describe '閲覧許可に於いて' do\r
     before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @author.id\r
       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
     end\r
-    context '検査対象がnil(ゲスト)のとき' do\r
-      context 'クローズドモードのとき' do\r
-        before do\r
-          MagicNumber['run_mode'] = 1\r
-        end\r
-        it '不許可を返す。' do\r
-          r = @panel.visible?(nil)\r
-          r.should be_false\r
-        end\r
+    context 'オープンモードのとき' do\r
+      before do\r
+        MagicNumber['run_mode'] = 0\r
       end\r
-      context 'オープンモードのとき' do\r
-        before do\r
-          MagicNumber['run_mode'] = 0\r
-        end\r
-        it '公開コマなら許可する' do\r
-          Panel.any_instance.stub(:publish?).and_return(true)\r
-          r = @panel.visible?(nil)\r
-          r.should be_true\r
-        end\r
-        it '非公開コマなら許可しない' do\r
-          Panel.any_instance.stub(:publish?).and_return(false)\r
-          r = @panel.visible?(nil)\r
-          r.should be_false\r
-        end\r
+      it '自身にゲスト用ロールチェックを問い合わせしている' do\r
+        Panel.any_instance.stub(:guest_role_check).and_return(true)\r
+        Panel.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)\r
+        r = @panel.visible?([@author])\r
+      end\r
+      it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do\r
+        Panel.any_instance.stub(:guest_role_check).and_return(false)\r
+        r = @panel.visible?([@author])\r
+        r.should be_false\r
       end\r
     end\r
-    context '検査対象が作家のとき' do\r
-      it '自分のコマなら許可する' do\r
+    context 'クローズドモードのとき' do\r
+      before do\r
+        MagicNumber['run_mode'] = 1\r
+      end\r
+      it '自身に読者用ロールチェックを問い合わせしている' do\r
+        Panel.any_instance.stub(:reader_role_check).and_return(true)\r
+        Panel.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)\r
+        r = @panel.visible?([@author])\r
+      end\r
+      it '読者用ロールチェックが失敗したとき、falseを返す' do\r
+        Panel.any_instance.stub(:reader_role_check).and_return(false)\r
+        r = @panel.visible?([@author])\r
+        r.should be_false\r
+      end\r
+    end\r
+    context '事前チェックする' do\r
+      before do\r
+        MagicNumber['run_mode'] = 1\r
+        Panel.any_instance.stub(:reader_role_check).and_return(true)\r
+      end\r
+      it '自身に所持判定を問い合わせしている' do\r
         Panel.any_instance.stub(:own?).and_return(true)\r
+        Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
+        r = @panel.visible?([@author])\r
+      end\r
+      it '自身に閲覧許可を問い合わせしている' do\r
+        Panel.any_instance.stub(:own?).and_return(false)\r
         Panel.any_instance.stub(:publish?).and_return(true)\r
-        r = @panel.visible?(@author)\r
-        r.should == true\r
+        Panel.any_instance.should_receive(:publish?).with(any_args).exactly(1)\r
+        r = @panel.visible?([@author])\r
+      end\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      before do\r
+        MagicNumber['run_mode'] = 1\r
+        Panel.any_instance.stub(:reader_role_check).and_return(true)\r
+      end\r
+      it '自分のコマなら許可する' do\r
+        Panel.any_instance.stub(:own?).and_return(true)\r
+        Panel.any_instance.stub(:publish?).and_return(false)\r
+        r = @panel.visible?([@author])\r
+        r.should be_true\r
       end\r
       it '他人の非公開コマなら許可しない' do\r
         Panel.any_instance.stub(:own?).and_return(false)\r
         Panel.any_instance.stub(:publish?).and_return(false)\r
-        r = @panel.visible?(@author)\r
-        r.should == false\r
+        r = @panel.visible?([@author])\r
+        r.should be_false\r
       end\r
       it '他人のコマでも公開なら許可する' do\r
         Panel.any_instance.stub(:own?).and_return(false)\r
         Panel.any_instance.stub(:publish?).and_return(true)\r
-        r = @panel.visible?(@author)\r
-        r.should == true\r
-      end\r
-    end\r
-    context '検査対象がそれ以外のとき' do\r
-      it '不許可を返す。' do\r
-        r = @panel.visible?(@admin)\r
-        r.should be_false\r
+        r = @panel.visible?([@author])\r
+        r.should be_true\r
       end\r
     end\r
   end\r
@@ -1136,13 +1192,21 @@ describe Panel do
         }.should change(Panel, :count)\r
       end\r
     end\r
+    context '不正なjsonデータのとき' do\r
+      before do\r
+      end\r
+      it 'エラーメッセージがセットされている' do\r
+        r = @panel.store false, @author\r
+        @panel.errors[:base].should_not be_blank\r
+      end\r
+    end\r
     context '従属データの検証に失敗したとき' do\r
       before do\r
         Panel.any_instance.stub(:validate_child).with(any_args).and_return(false)\r
       end\r
       it 'エラーメッセージがセットされている' do\r
         r = @panel.store @attr, @author\r
-        @panel.errors.should_not be_empty\r
+        @panel.errors[:base].should_not be_blank\r
       end\r
     end\r
     context 'カラム値がFalseしたとき' do\r
@@ -1155,6 +1219,73 @@ describe Panel do
       end\r
     end\r
   end\r
+  \r
+  describe '削除に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @author.id\r
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
+      @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
+      @sb = @panel.speech_balloons.create(\r
+        FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
+      )\r
+      @gc = @panel.ground_colors.create(\r
+        FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :color_id => @color.id)\r
+      )\r
+      @gp = @panel.ground_pictures.create(\r
+        FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
+      )\r
+      @pc = @panel.panel_colors.create(\r
+        FactoryGirl.attributes_for(:panel_color, :panel_id => @panel.id, :code => 0xff0000)\r
+      )\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it '自身を削除する' do\r
+        lambda {\r
+          r = @panel.destroy_with_elements\r
+        }.should change(Panel, :count).by(-1)\r
+        lambda {\r
+          r = Panel.find @panel.id\r
+        }.should raise_error\r
+      end\r
+      it '自身にリンクしているコマ要素をすべて削除する' do\r
+        lambda {\r
+          r = @panel.destroy_with_elements\r
+        }.should change(PanelPicture, :count).by(-1)\r
+        lambda {\r
+          r = PanelPicture.find @pp.id\r
+        }.should raise_error \r
+      end\r
+      it '自身にリンクしているコマ要素をすべて削除する' do\r
+        lambda {\r
+          r = @panel.destroy_with_elements\r
+        }.should change(GroundPicture, :count).by(-1)\r
+        lambda {\r
+          r = GroundPicture.find @gp.id\r
+        }.should raise_error \r
+      end\r
+      it 'Trueを返す' do\r
+        r = @panel.destroy_with_elements\r
+        r.should be_true\r
+      end\r
+    end\r
+    context '削除に失敗したとき' do\r
+      before do\r
+        PanelPicture.any_instance.stub(:destroy).with(any_args).and_return(false)\r
+      end\r
+      it 'Falseを返す' do\r
+        r = @panel.destroy_with_elements\r
+        r.should be_false\r
+      end\r
+      it 'ロールバックしている' do\r
+        lambda {\r
+          r = @panel.destroy_with_elements\r
+        }.should_not change(Panel, :count)\r
+        lambda {\r
+          r = @panel.destroy_with_elements\r
+        }.should_not change(PanelPicture, :count)\r
+      end\r
+    end\r
+  end\r
 =begin\r
   describe 'id挿げ替え防止確認に於いて' do\r
     before do\r