OSDN Git Service

t#29050:fix edit permissin
authoryasushiito <yas@pen-chan.jp>
Fri, 20 Jul 2012 09:57:45 +0000 (18:57 +0900)
committeryasushiito <yas@pen-chan.jp>
Fri, 20 Jul 2012 09:57:45 +0000 (18:57 +0900)
app/controllers/original_pictures_controller.rb
app/controllers/stories_controller.rb
app/models/original_picture.rb
app/models/story.rb
spec/controllers/original_pictures_controller_spec.rb
spec/controllers/stories_controller_spec.rb
spec/models/original_picture_spec.rb
spec/models/story_spec.rb

index 31b3ca1..b9e941c 100644 (file)
@@ -129,7 +129,7 @@ class OriginalPicturesController < ApplicationController
   # PUT /original_pictures/1.json
   def update
     @picture_data = set_image params[:original_picture][:file]
-    @original_picture = OriginalPicture.show(params[:id], @author)
+    @original_picture = OriginalPicture.edit(params[:id], @author)
     @original_picture.supply_default @artist
 
     respond_to do |format|
@@ -146,7 +146,7 @@ class OriginalPicturesController < ApplicationController
   # DELETE /original_pictures/1
   # DELETE /original_pictures/1.json
   def destroy
-    @original_picture = OriginalPicture.find(params[:id], @author)
+    @original_picture = OriginalPicture.edit(params[:id], @author)
     OriginalPicture.transaction do
       @original_picture.destroy
     end
index 15d48ca..f2ee082 100644 (file)
@@ -87,7 +87,7 @@ class StoriesController < ApplicationController
   # PUT /stories/1
   # PUT /stories/1.json
   def update
-    @story = Story.show(params[:id], @author)
+    @story = Story.edit(params[:id], @author)
     ot = @story.t
     @story.attributes = params[:story]
     @story.overwrite @author
@@ -106,8 +106,10 @@ class StoriesController < ApplicationController
   # DELETE /stories/1.js
   # DELETE /stories/1.json
   def destroy
-    @story = Story.show(params[:id], @author)
-    @story.destroy_and_shorten
+    @story = Story.edit(params[:id], @author)
+    Story.transaction do
+      @story.destroy_and_shorten
+    end
     respond_to do |format|
       format.html { redirect_to story_path(@story.comic) }
       format.json { head :ok }
index 722e110..96740f9 100644 (file)
@@ -59,6 +59,12 @@ class OriginalPicture < ActiveRecord::Base
     pic
   end
   
+  def self.edit cid, artist, opt = {}
+    pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
+    raise ActiveRecord::Forbidden unless pic.own?(artist)
+    pic
+  end
+  
   def self.show_include_opt opt = {}
     res = [:license, :resource_picture]
     res.push(opt[:include]) if opt[:include]
index 7839480..26575e6 100644 (file)
@@ -23,11 +23,9 @@ class Story < ActiveRecord::Base
     self.author_id == author.id
   end
   
-  def self.show sid, au = nil
+  def self.edit sid, au
     res = Story.find sid
-    if au
-      raise ActiveRecord::Forbidden unless res.own?(au)
-    end
+    raise ActiveRecord::Forbidden unless res.own?(au)
     res
   end
   
index 9359603..4eb1e8d 100644 (file)
@@ -592,7 +592,7 @@ describe OriginalPicturesController do
   describe '更新に於いて' do
     before do
       @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
-      OriginalPicture.stub(:show).with(any_args()).and_return(@pic)
+      OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
       sign_in @user
     end
     context '事前チェックしておく' do
@@ -600,7 +600,7 @@ describe OriginalPicturesController do
         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
       end
       it '原画モデルに単体取得を問い合わせている' do
-        OriginalPicture.should_receive(:show).exactly(1)\r
+        OriginalPicture.should_receive(:edit).exactly(1)\r
         put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
       end
       it 'モデルに更新を依頼する' do
index 13985d9..5124482 100644 (file)
@@ -262,9 +262,9 @@ describe StoriesController do
       sign_in @user
     end
     context 'つつがなく終わるとき' do
-      it 'モデルに取得依頼する' do
-        Story.stub(:show).with(any_args).and_return(@story)
-        Story.should_receive(:show).exactly(1)
+      it 'モデルに編集取得依頼する' do
+        Story.stub(:edit).with(any_args).and_return(@story)
+        Story.should_receive(:edit).exactly(1)
         put :update, :id => @story.id, :story => @attr
       end
       it 'POSTデータから、カラム値を復元している' do
@@ -361,4 +361,74 @@ describe StoriesController do
     end
   end
 
+  describe '削除に於いて' do\r
+    before do\r
+      @story = Factory :story, :author_id => @author.id
+      sign_in @user
+      Story.stub(:edit).and_return(@story)\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ストーリーモデルに編集取得を問い合わせている' do\r
+        Story.should_receive(:edit).exactly(1)\r
+        delete :destroy, :id => @story.id\r
+      end\r
+      it '@storyにアレを取得している' do\r
+        delete :destroy, :id => @story.id\r
+        assigns(:story).id.should eq(@story.id)\r
+      end\r
+      it 'そのストーリーを一つのトランザクションで削除する' do\r
+        lambda {\r
+          delete :destroy, :id => @story.id\r
+        }.should change(Story, :count)\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          delete :destroy, :id => @story.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'ストーリー一覧ページへ遷移する' do\r
+          delete :destroy, :id => @story.id\r
+          response.should redirect_to(story_path(@story.comic_id))\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード200 OKを返す' do\r
+          delete :destroy, :id => @story.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 => @story.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          delete :destroy, :id => @story.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 => @story.id, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          delete :destroy, :id => @story.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
index f8c8f7c..dd685ea 100644 (file)
@@ -224,6 +224,41 @@ describe OriginalPicture do
       end
     end
   end
+  describe '編集取得に於いて' do
+    before do
+      @op = Factory :original_picture, :artist_id => @artist.id
+    end
+    it '指定の原画を返す' do
+      pic = OriginalPicture.edit @op.id, @artist
+      pic.should eq @op
+    end
+    context '関連テーブルオプションがないとき' do
+      it 'ライセンスと素材を含んでいる' do
+        r = OriginalPicture.show_include_opt
+        r.should eq [:license, :resource_picture]
+      end
+    end
+    context '関連テーブルオプションで絵師を含ませたとき' do
+      it 'ライセンスと素材と作者データを含んでいる' do
+        r = OriginalPicture.show_include_opt(:include => :artist)
+        r.should eq [:license, :resource_picture, :artist]
+      end
+    end
+    context '他人の原画を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        lambda{
+          pic = OriginalPicture.edit @op.id, @other_artist
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しない原画を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          pic = OriginalPicture.edit 0, @artist
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
   describe 'json単体出力オプションに於いて' do
     it 'includeキーがライセンスと素材を含んでいる' do
       r = OriginalPicture.show_json_include_opt
index 57cb93b..1f9a23e 100644 (file)
@@ -164,30 +164,22 @@ describe Story do
       @panel = Factory :panel, :author_id => @author.id
       @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
     end
-    context 'オーナー指定がないとき' do
-      it '指定のストーリーを返す' do
-        l = Story.show @story.id, @author
-        l.should eq @story
-      end
+    it '指定のストーリーを返す' do
+      l = Story.edit @story.id, @author
+      l.should eq @story
     end
-    context 'オーナー指定のとき' do
-      it '指定のストーリーが自分のものならそれを返す' do
-        l = Story.show @story.id, @author
-        l.should eq @story
-      end
-      context '他人のストーリーを開こうとしたとき' do
-        it '403Forbidden例外を返す' do
-          Story.any_instance.stub(:own?).and_return(false)
-          lambda{
-            Story.show @story.id, @author
-          }.should raise_error(ActiveRecord::Forbidden)
-        end
+    context '他人のストーリーを開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        Story.any_instance.stub(:own?).and_return(false)
+        lambda{
+          Story.edit @story.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
       end
     end
     context '存在しないストーリーを開こうとしたとき' do
       it '404RecordNotFound例外を返す' do
         lambda{
-          Story.show 110, @author
+          Story.edit 110, @author
         }.should raise_error(ActiveRecord::RecordNotFound)
       end
     end