OSDN Git Service

create provider
[pettanr/pettanr.git] / spec / controllers / provider_statuses_controller_spec.rb
diff --git a/spec/controllers/provider_statuses_controller_spec.rb b/spec/controllers/provider_statuses_controller_spec.rb
new file mode 100644 (file)
index 0000000..e13ec8e
--- /dev/null
@@ -0,0 +1,377 @@
+# -*- encoding: utf-8 -*-
+#借受状況
+require 'spec_helper'
+
+describe ProviderStatusesController do
+  before do
+    @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
+    @user = FactoryGirl.create :user_yas
+    @author = @user.author    #ユーザ作成時に連動して作成される
+  end
+  
+  describe '一覧表示に於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+      ProviderStatus.stub(:list).and_return([@ps, @ps, @ps])
+      ProviderStatus.stub(:available_list).and_return([@ps, @ps])
+      sign_in @admin
+    end
+    context '事前チェックする' do
+      it '与えられたpageがセットされている' do
+        get :index, :page => 5
+        assigns(:page).should eq 5
+      end
+      it '省略されると@pageに1値が入る' do
+        get :index
+        assigns(:page).should eq 1
+      end
+      it '与えられたpage_sizeがセットされている' do
+        get :index, :page_size => 15
+        assigns(:page_size).should eq 15
+      end
+      it '省略されると@page_sizeにデフォルト値が入る' do
+        get :index
+        assigns(:page_size).should eq ProviderStatus.default_page_size
+      end
+      it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 1500
+        assigns(:page_size).should eq ProviderStatus.max_page_size
+      end
+      it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 0
+        assigns(:page_size).should eq ProviderStatus.default_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      it '@hideが空になっている' do
+        get :index
+        assigns(:hide).should be_blank
+      end
+      it '借受状況モデルに全一覧取得を問い合わせている' do
+        ProviderStatus.should_receive(:list).exactly(1)
+        get :index
+      end
+      it '@provider_statusesにリストを取得している' do
+        get :index
+        assigns(:provider_statuses).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 '借受状況モデルにjson一覧出力オプションを問い合わせている' do
+          ProviderStatus.should_receive(:list_json_opt).exactly(1)
+          get :index, :format => :json
+        end
+        it 'データがリスト構造になっている' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいは借受状況っぽいものであって欲しい' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("token").should be_true
+          json.first.has_key?("receive_hour1").should be_true
+          json.first.has_key?("receive_hour2").should be_true
+          json.first.has_key?("received_at").should be_true
+        end
+      end
+    end
+    context '除外フラグが除外のとき' do
+      it '@hideが設定されている' do
+        get :index, :hide => 1
+        assigns(:hide).should_not be_blank
+      end
+      it '借受状況モデルに待機中一覧取得を問い合わせている' do
+        ProviderStatus.should_receive(:available_list).exactly(1)
+        get :index, :hide => 1
+      end
+      it '@provider_statusesにリストを取得している' do
+        get :index, :hide => 1
+        assigns(:provider_statuses).should have_at_least(2).items
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :index
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :index
+          response.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :index, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :index, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+  
+  describe '単体表示に於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+      ProviderStatus.stub(:show).and_return(@ps)
+      sign_in @admin
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @ps.id
+        response.should be_success
+      end
+      it '借受状況モデルに単体取得を問い合わせている' do
+        ProviderStatus.should_receive(:show).exactly(1)
+        get :show
+      end
+      it '@provider_statusにアレを取得している' do
+        get :show, :id => @ps.id
+        assigns(:provider_status).should eq(@ps)
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @ps.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @ps.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '借受状況モデルにjson単体出力オプションを問い合わせている' do
+          ProviderStatus.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @ps.id, :format => :json
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @ps.id, :format => :json
+          json = JSON.parse response.body
+          json.has_key?("token").should be_true
+          json.has_key?("receive_hour1").should be_true
+          json.has_key?("receive_hour2").should be_true
+          json.has_key?("received_at").should be_true
+        end
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :show, :id => @ps.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :show, :id => @ps.id
+          response.body.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :show, :id => @ps.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :show, :id => @ps.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+  
+  describe '編集フォーム表示に於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+      sign_in @admin
+      ProviderStatus.stub(:edit).with(@ps.id.to_s, @admin).and_return(@ps)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :edit, :id => @ps.id
+        response.should be_success 
+      end
+      it '借受状況モデルに編集取得を問い合わせている' do
+        ProviderStatus.should_receive(:edit).exactly(1)
+        get :edit, :id => @ps.id
+      end
+      it '@provider_statusにデータを用意している' do
+        get :edit, :id => @ps.id
+        assigns(:provider_status).should eq @ps
+      end
+      context 'html形式' do
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @ps.id
+          response.should render_template("edit")
+        end
+      end
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @ps.id, :format => :js
+          response.should render_template("edit")
+        end
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @ps.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @ps.id
+          response.body.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :edit, :id => @ps.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :edit, :id => @ps.id, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
+
+  describe '更新に於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status
+      @attr = FactoryGirl.attributes_for(:provider_status, :receive_hour1 => 22)
+      sign_in @admin
+    end
+    context '事前チェックしておく' do
+      it '借受状況モデルに編集取得を問い合わせている' do
+        ProviderStatus.stub(:edit).with(any_args()).and_return @ps
+        ProviderStatus.should_receive(:edit).exactly(1)
+        put :update, :id => @ps.id, :provider_status => @attr
+      end
+      it '借受状況モデルにカラム値復元を依頼している' do
+        ProviderStatus.any_instance.should_receive(:attributes=).exactly(1)
+        put :update, :id => @ps.id, :provider_status => @attr
+      end
+      it '借受状況モデルに上書き補充を依頼している' do
+        ProviderStatus.any_instance.should_receive(:overwrite).exactly(1)
+        put :update, :id => @ps.id, :provider_status => @attr
+      end
+      it 'モデルに更新を依頼する' do
+        ProviderStatus.any_instance.stub(:save).with(any_args).and_return true
+        ProviderStatus.any_instance.should_receive(:save).exactly(1)
+        put :update, :id => @ps.id, :provider_status => @attr
+      end
+      it '@provider_statusにアレを取得している' do
+        put :update, :id => @ps.id, :provider_status => @attr
+        assigns(:provider_status).id.should eq(@ps.id)
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '更新される' do
+        put :update, :id => @ps.id, :provider_status => @attr
+        ProviderStatus.find(@ps.id).receive_hour1.should eq 22
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          ProviderStatus.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @ps.id, :provider_status => @attr
+          response.status.should eq 302
+        end
+        it '更新された借受状況の表示ページへ遷移する' do
+          put :update, :id => @ps.id, :provider_status => @attr
+          response.should redirect_to(@ps)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          ProviderStatus.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @ps.id, :provider_status => @attr, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          ProviderStatus.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @ps.id, :provider_status => @attr, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      it 'ステータスコード302 Foundを返す' do
+        put :update, :id => @ps.id, :provider_status => @attr
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          put :update, :id => @ps.id, :provider_status => @attr
+          response.body.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          put :update, :id => @ps.id, :provider_status => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '検証、保存に失敗したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:save).and_return(false)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 Okを返す' do
+          put :update, :id => @ps.id, :provider_status => @attr
+          response.status.should eq 200
+        end
+        it '編集ページを描画する' do
+          put :update, :id => @ps.id, :provider_status => @attr
+          response.should render_template("edit")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          ProviderStatus.any_instance.stub(:save).and_return(false)
+          put :update, :id => @ps.id, :provider_status => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          put :update, :id => @ps.id, :provider_status => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
+  end
+
+end