OSDN Git Service

t#29035:fix destroy on panel
authoryasushiito <yas@pen-chan.jp>
Thu, 19 Jul 2012 23:41:46 +0000 (08:41 +0900)
committeryasushiito <yas@pen-chan.jp>
Thu, 19 Jul 2012 23:41:46 +0000 (08:41 +0900)
app/controllers/panels_controller.rb
app/models/panel.rb
spec/controllers/panels_controller_spec.rb
spec/models/panel_spec.rb

index 2dc0e67..85cd8ce 100644 (file)
@@ -121,18 +121,13 @@ class PanelsController < ApplicationController
   # DELETE /panels/1
   # DELETE /panels/1.json
   def destroy
-    @panel = Panel.find(params[:id])
-    if @panel.own? @author
-      respond_to do |format|
-        Panel.transaction do
-          @panel.destroy_and_shorten
-          format.html { redirect_to panels_url }
-          format.json { head :ok }
-        end
+    @panel = Panel.edit(params[:id], @author)
+    respond_to do |format|
+      Panel.transaction do
+        @panel.destroy
+        format.html { redirect_to panels_url }
+        format.json { head :ok }
       end
-    else
-      format.html { render action: "edit" }
-      format.json { render json: @panel.errors, status: :unprocessable_entity }
     end
   end
   
index b84ffc9..56cd5b5 100644 (file)
@@ -164,6 +164,12 @@ class Panel < ActiveRecord::Base
     r
   end
   
+  def self.edit rid, au, opt = {}
+    r = Panel.find(rid, :include => self.show_include_opt(opt))
+    raise ActiveRecord::Forbidden unless r.own?(au)
+    r
+  end
+  
   def self.show_include_opt opt = {}
     res = {
       :panel_pictures => {
index 700b236..6d9298a 100644 (file)
@@ -599,5 +599,75 @@ describe PanelsController do
     end\r
   end\r
 \r
+  describe '削除に於いて' do\r
+    before do\r
+      @panel = Factory :panel, :author_id => @user.author.id\r
+      Panel.stub(:edit).and_return(@panel)\r
+      sign_in @user\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'コマモデルに編集取得を問い合わせている' do\r
+        Panel.should_receive(:edit).exactly(1)\r
+        delete :destroy, :id => @panel.id\r
+      end\r
+      it '@panelにアレを取得している' do\r
+        delete :destroy, :id => @panel.id\r
+        assigns(:panel).id.should eq(@panel.id)\r
+      end\r
+      it 'そのコマを一つのトランザクションで削除する' do\r
+        lambda {\r
+          delete :destroy, :id => @panel.id\r
+        }.should change(Panel, :count)\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          delete :destroy, :id => @panel.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'コマ一覧ページへ遷移する' do\r
+          delete :destroy, :id => @panel.id\r
+          response.should redirect_to(panels_url)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード200 OKを返す' do\r
+          delete :destroy, :id => @panel.id, :format => :json\r
+          response.should be_success\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          delete :destroy, :id => @panel.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          delete :destroy, :id => @panel.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          delete :destroy, :id => @panel.id, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          delete :destroy, :id => @panel.id, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+=begin\r
+    context '対象コマがないとき' do\r
+    end\r
+    context '他人のコマだったとき' do\r
+    end\r
+=end\r
+  end\r
+  \r
 end\r
 \r
index eb49e89..1398367 100644 (file)
@@ -264,7 +264,7 @@ describe Panel do
       @panel = Factory :panel, :author_id => @author.id\r
     end\r
     it '指定のコマを返す' do\r
-      Comic.any_instance.stub(:visible?).and_return(true)\r
+      Panel.any_instance.stub(:visible?).and_return(true)\r
       pl = Panel.show @panel.id, @author\r
       pl.should eq @panel\r
     end\r
@@ -284,6 +284,31 @@ describe Panel do
       end\r
     end\r
   end\r
+  describe '単体取得に於いて' do\r
+    before do\r
+      @panel = Factory :panel, :author_id => @author.id\r
+    end\r
+    it '指定のコマを返す' do\r
+      Panel.any_instance.stub(:own?).and_return(true)\r
+      pl = Panel.edit @panel.id, @author\r
+      pl.should eq @panel\r
+    end\r
+    context '他人のコマを開こうとしたとき' do\r
+      it '403Forbidden例外を返す' do\r
+        Panel.any_instance.stub(:own?).and_return(false)\r
+        lambda{\r
+          Panel.edit @panel.id, @author\r
+        }.should raise_error(ActiveRecord::Forbidden)\r
+      end\r
+    end\r
+    context '存在しないコマを開こうとしたとき' do\r
+      it '404RecordNotFound例外を返す' do\r
+        lambda{\r
+          Panel.edit 110, @author\r
+        }.should raise_error(ActiveRecord::RecordNotFound)\r
+      end\r
+    end\r
+  end\r
   describe '関連テーブルプションに於いて' do\r
     context 'オプションがないとき' do\r
       it '3つの項目を含んでいる' do\r