OSDN Git Service

comic test ok
[pettanr/pettanr.git] / spec / controllers / comics_controller_spec.rb
index 7a11c89..95dde5a 100644 (file)
+# -*- encoding: utf-8 -*-\r
 require 'spec_helper'
 
-# This spec was generated by rspec-rails when you ran the scaffold generator.
-# It demonstrates how one might use RSpec to specify the controller code that
-# was generated by Rails when you ran the scaffold generator.
-#
-# It assumes that the implementation code is generated by the rails scaffold
-# generator.  If you are using any extension libraries to generate different
-# controller code, this generated spec may or may not pass.
-#
-# It only uses APIs available in rails and/or rspec-rails.  There are a number
-# of tools you can use to make these specs even more expressive, but we're
-# sticking to rails and rspec-rails APIs to keep things simple and stable.
-#
-# Compared to earlier versions of this generator, there is very limited use of
-# stubs and message expectations in this spec.  Stubs are only used when there
-# is no simpler way to get a handle on the object needed for the example.
-# Message expectations are only used when there is no simpler way to specify
-# that an instance is receiving a specific message.
-
 describe ComicsController do
-
   # This should return the minimal set of attributes required to create a valid
   # Comic. As you add validations to Comic, be sure to
   # update the return value of this method accordingly.
+  before do
+    Factory :admin
+      @user = Factory :user_yas\r
+      @author = @user.author    #ユーザ作成時に連動して作成される
+  end
+  
   def valid_attributes
-    {}
+    {:title => 'test comic', :width => 100, :height => 50, :visible => 0, :editable => 0}
   end
 
-  describe "GET index" do
-    it "assigns all comics as @comics" do
-      comic = Comic.create! valid_attributes
-      get :index
-      assigns(:comics).should eq([comic])
+  describe '一覧表示に於いて' do
+    before do
+      Factory :normal_comic, :author_id => @user.author.id
+      Factory :visible_comic, :author_id => @user.author.id
+      Factory:editable_comic, :author_id => @user.author.id
+      Factory :hidden_comic, :author_id => @user.author.id
+      sign_in @user
+    end
+    context '事前チェックする' do
+      it '与えられたpageがセットされている' do
+        get :index, :page => 5
+        assigns(:page).should eq 5
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      it '@comicsにリストを取得している' do
+        get :index
+        assigns(:comics).should have_at_least(3).items
+      end
+      context 'html形式' do
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがリスト構造になっている' do
+          get :index, :format => :json
+          json = JSON.parse response.body\r
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいはコミックっぽいものであって欲しい' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("title").should be_true
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          sign_out @user
+          get :index
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          sign_out @user
+          get :index
+          response.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          sign_out @user
+            get :index, :format => :json
+          response.status.should eq 401
+        end
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          sign_out @user
+          get :index, :format => :json\r
+          response.message.should match(/Unauthorized/)
+        end
+      end
     end
   end
-
-  describe "GET show" do
-    it "assigns the requested comic as @comic" do
-      comic = Comic.create! valid_attributes
-      get :show, :id => comic.id
-      assigns(:comic).should eq(comic)
+  
+  describe '単体表示に於いて' do
+    before do
+      @comic = Factory :normal_comic, :author_id => @user.author.id
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @comic.id
+        response.should be_success
+      end
+      it '@comicにアレを取得している' do
+        get :show, :id => @comic.id
+        assigns(:comic).id.should eq(@comic.id)
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @comic.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @comic.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @comic.id, :format => :json
+          json = JSON.parse response.body
+          json["title"].should match(/normal/)
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      it 'ステータスコード302 Foundを返す' do
+        sign_out @user
+        get :show, :id => @comic.id
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          sign_out @user
+          get :show, :id => @comic.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          sign_out @user
+          get :show, :id => @comic.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '対象コミックがないとき' do
+      context 'html形式' do
+        it '例外404 not_foundを返す' do
+          lambda{\r
+            get :show, :id => 0
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+      context 'json形式' do
+        it '例外404 not_foundを返す' do
+          lambda{ \r
+            get :show, :id => 0, :format => :json
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+    end
+    context '非公開コミックを見ようとしたとき' do
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          Comic.any_instance.stub(:vis).with(any_args()).and_return(false)
+          hidden = Factory :hidden_comic, :author_id => @author.id
+          lambda{\r
+            get :show, :id => hidden
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          Comic.any_instance.stub(:vis).with(any_args()).and_return(false)
+          hidden = Factory :hidden_comic, :author_id => @author.id
+          lambda{\r
+            get :show, :id => hidden, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
     end
   end
 
-  describe "GET new" do
-    it "assigns a new comic as @comic" do
-      get :new
-      assigns(:comic).should be_a_new(Comic)
+  describe 'コミック数取得に於いて' do
+    before do
+      Comic.should_receive(:visible_count).and_return(3)
+#      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :count, :format => :json
+        response.should be_success 
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :count, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがHash構造になっていてコミック数が1である' do
+          get :count, :format => :json
+          json = JSON.parse response.body
+          json["count"].should == 3
+        end
+      end
     end
   end
 
-  describe "GET edit" do
-    it "assigns the requested comic as @comic" do
-      comic = Comic.create! valid_attributes
-      get :edit, :id => comic.id
-      assigns(:comic).should eq(comic)
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :new
+        response.should be_success 
+      end
+      it '@comicに新規データを用意している' do
+        get :new
+        assigns(:comic).should be_a_new(Comic)
+      end
+      it '@comicにデフォルト値セットが施されている' do
+        Comic.any_instance.should_receive(:supply_default).exactly(1)
+        get :new
+      end
+      context 'html形式' do
+        it 'newテンプレートを描画する' do
+          get :new
+          response.should render_template("new")
+        end
+      end
+      context 'js形式' do
+        it 'new.jsテンプレートを描画する' do
+          get :new, :format => :js
+          response.should render_template("new")
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          sign_out @user
+          get :new
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          sign_out @user
+          get :new
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          sign_out @user
+          get :new, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          sign_out @user
+          get :new, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
     end
   end
 
-  describe "POST create" do
-    describe "with valid params" do
-      it "creates a new Comic" do
-        expect {
-          post :create, :comic => valid_attributes
-        }.to change(Comic, :count).by(1)
+  describe '新規作成に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'モデルに保存依頼する' do
+        Comic.any_instance.should_receive(:save).exactly(1)
+        post :create, :comic => Factory.attributes_for(:normal_comic)
       end
-
-      it "assigns a newly created comic as @comic" do
-        post :create, :comic => valid_attributes
+      it 'ステータスコード302 Foundを返す' do
+        Comic.any_instance.stub(:save).and_return(true)
+        post :create, :comic => Factory.attributes_for(:normal_comic)
+        response.status.should eq 302
+      end
+      it "@comicに作成されたコミックを保持していて、それがDBにある" do
+        post :create, :comic => Factory.attributes_for(:normal_comic)
         assigns(:comic).should be_a(Comic)
         assigns(:comic).should be_persisted
       end
-
-      it "redirects to the created comic" do
-        post :create, :comic => valid_attributes
-        response.should redirect_to(Comic.last)
+      context 'html形式' do
+        it '作成されたコミックの表示ページへ遷移する' do
+          post :create, :comic => Factory.attributes_for(:normal_comic)
+          response.should redirect_to(Comic.last)
+        end
+      end
+      context 'json形式' do
+        it '作成されたコミックをjsonデータで返す' do
+          post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
+          json = JSON.parse response.body
+          json["title"].should match(/normal/)
+        end
       end
     end
-
-    describe "with invalid params" do
-      it "assigns a newly created but unsaved comic as @comic" do
-        # Trigger the behavior that occurs when invalid params are submitted
+    context '作家権限がないとき' do
+      it 'ステータスコード302 Foundを返す' do
+        sign_out @user
+        post :create, :comic => Factory.attributes_for(:normal_comic)
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          sign_out @user
+          post :create, :comic => Factory.attributes_for(:normal_comic)
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          sign_out @user
+          post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '検証、保存に失敗した' do
+      it "未保存のコミックを保持している" do
         Comic.any_instance.stub(:save).and_return(false)
         post :create, :comic => {}
         assigns(:comic).should be_a_new(Comic)
       end
-
-      it "re-renders the 'new' template" do
-        # Trigger the behavior that occurs when invalid params are submitted
-        Comic.any_instance.stub(:save).and_return(false)
-        post :create, :comic => {}
-        response.should render_template("new")
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          Comic.any_instance.stub(:save).and_return(false)
+          post :create, :comic => {}
+          response.status.should eq 200
+        end
+        it '新規ページを描画する' do
+          Comic.any_instance.stub(:save).and_return(false)
+          post :create, :comic => {}
+          response.should render_template("new")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          Comic.any_instance.stub(:save).and_return(false)
+          post :create, :comic => {}, :format => :json
+          response.status.should eq 422
+        end
+        it 'jsonデータを返す' do
+          Comic.any_instance.stub(:save).and_return(false)
+          post :create, :comic => {}, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          Comic.any_instance.stub(:save).and_return(false)
+          post :create, :comic => {}, :format => :json
+          json = JSON.parse response.body
+          json.should eq( {})
+        end
       end
     end
   end
 
-  describe "PUT update" do
-    describe "with valid params" do
-      it "updates the requested comic" do
-        comic = Comic.create! valid_attributes
-        # Assuming there are no other comics in the database, this
-        # specifies that the Comic created on the previous line
-        # receives the :update_attributes message with whatever params are
-        # submitted in the request.
-        Comic.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
-        put :update, :id => comic.id, :comic => {'these' => 'params'}
+  describe '編集フォーム表示に於いて' do
+    before do
+      @comic = Factory :normal_comic, :author_id => @user.author.id
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :edit, :id => @comic.id
+        response.should be_success 
       end
-
-      it "assigns the requested comic as @comic" do
-        comic = Comic.create! valid_attributes
-        put :update, :id => comic.id, :comic => valid_attributes
-        assigns(:comic).should eq(comic)
+      it '@comicにデータを用意している' do
+        get :edit, :id => @comic.id
+        assigns(:comic).should eq @comic
       end
-
-      it "redirects to the comic" do
-        comic = Comic.create! valid_attributes
-        put :update, :id => comic.id, :comic => valid_attributes
-        response.should redirect_to(comic)
+      it '@comicにデフォルト値セットが施されている' do
+        Comic.any_instance.should_receive(:supply_default).exactly(1)
+        get :edit, :id => @comic.id\r
+      end
+      context 'html形式' do
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @comic.id
+          response.should render_template("edit")
+        end
+      end
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @comic.id, :format => :js
+          response.should render_template("edit")
+        end
       end
     end
-
-    describe "with invalid params" do
-      it "assigns the comic as @comic" do
-        comic = Comic.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        Comic.any_instance.stub(:save).and_return(false)
-        put :update, :id => comic.id, :comic => {}
-        assigns(:comic).should eq(comic)
+    context '作家権限がないとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          sign_out @user
+          get :edit, :id => @comic.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          sign_out @user
+          get :edit, :id => @comic.id
+          response.body.should redirect_to '/users/sign_in'
+        end
       end
-
-      it "re-renders the 'edit' template" do
-        comic = Comic.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        Comic.any_instance.stub(:save).and_return(false)
-        put :update, :id => comic.id, :comic => {}
-        response.should render_template("edit")
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          sign_out @user
+          get :edit, :id => @comic.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          sign_out @user
+          get :edit, :id => @comic.id, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '対象コミックがないとき' do
+      context 'html形式' do
+        it '例外404 not_foundを返す' do
+          lambda{\r
+            get :edit, :id => 0\r
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+      context 'js形式' do
+        it '例外404 not_foundを返す' do
+          lambda{ \r
+            get :edit, :id => 0, :format => :js\r
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+    end
+    context '自分のコミックでないとき' do
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          other = Factory :visible_comic, :author_id => 0
+          lambda{\r
+            get :edit, :id => other.id
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          other = Factory :visible_comic, :author_id => 0
+          lambda{\r
+            get :edit, :id => other.id
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
       end
     end
   end
 
-  describe "DELETE destroy" do
-    it "destroys the requested comic" do
-      comic = Comic.create! valid_attributes
-      expect {
-        delete :destroy, :id => comic.id
-      }.to change(Comic, :count).by(-1)
+  describe '更新に於いて' do
+    before do\r
+      @comic = Factory :normal_comic, :author => @author
+      sign_in @user
     end
-
-    it "redirects to the comics list" do
-      comic = Comic.create! valid_attributes
-      delete :destroy, :id => comic.id
-      response.should redirect_to(comics_url)
+    context '事前チェックしておく' do
+      it 'モデルに更新を依頼する' do
+        Comic.any_instance.should_receive(:update_attributes).with(any_args)
+        put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
+      end
+      it '@comicにアレを取得している' do
+        put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
+        assigns(:comic).id.should eq(@comic.id)
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード302 Foundを返す' do
+        Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
+        put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
+        response.status.should eq 302
+      end
+      it '更新される' do\r
+        put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)\r
+        Comic.find(@comic.id).visible.should eq 0
+      end
+      context 'html形式' do
+        it '更新されたコミックの表示ページへ遷移する' do
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
+          response.should redirect_to(@comic)
+        end
+      end
+      context 'json形式' do
+        it 'ページ本体は特に返さない' do
+          Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      it 'ステータスコード302 Foundを返す' do
+        sign_out @user
+        put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          sign_out @user
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          sign_out @user
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '対象コミックがないとき' do
+      context 'html形式' do
+        it '例外404 not_foundを返す' do
+          lambda{\r
+            put :update, :id => 0, :comic => Factory.attributes_for(:hidden_comic)
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+      context 'json形式' do
+        it '例外404 not_foundを返す' do
+          lambda{ \r
+            put :update, :id => 0, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+    end
+    context '他人のコミックを編集しようとしたとき' do
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          other = Factory :visible_comic, :author_id => 0
+          lambda{\r
+            put :update, :id => other.id, :comic => Factory.attributes_for(:hidden_comic)
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          other = Factory :visible_comic, :author_id => 0
+          lambda{\r
+            put :update, :id => other.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+    context '検証、保存に失敗したとき' do
+      context 'html形式' do
+        it '編集ページを描画する' do
+          Comic.any_instance.stub(:update_attributes).and_return(false)
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
+          response.should render_template("edit")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          Comic.any_instance.stub(:update_attributes).and_return(false)
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          response.status.should eq 422
+        end
+        it 'jsonデータを返す' do
+          Comic.any_instance.stub(:update_attributes).and_return(false)
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          lambda{\r
+            JSON.parse(response.body)\r
+          }.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          Comic.any_instance.stub(:update_attributes).and_return(false)
+          put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
+          json = JSON.parse response.body
+          json.should eq( {})\r
+        end
+      end
     end
   end
 
+
 end