OSDN Git Service

t#30322:create provider license import func
[pettanr/pettanr.git] / spec / controllers / demanders_controller_spec.rb
index c5f513f..ed7b6f0 100644 (file)
@@ -550,7 +550,7 @@ describe DemandersController do
         end
       end
     end
-    context '管理者権限がないとき' do
+    context '借手権限がないとき' do
       before do
         sign_out @demand_user
       end
@@ -601,5 +601,88 @@ describe DemandersController do
       end
     end
   end
-
+  
+  describe 'ライセンスエクスポートに於いて' do
+    before do
+      @ds = FactoryGirl.create :demander_status
+      @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id, :demand_user_id => @demand_user.id
+      sign_in @demand_user
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Demander.any_instance.stub(:licenses_export).with(any_args).and_return([@license, @license, @license])
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :licenses_export, :format => :json
+        response.should be_success 
+      end
+      it '日付文字列変換を依頼している' do
+        DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+        get :licenses_export, :date => '20111010', :format => :json
+      end
+      it '借手モデルにライセンスエクスポートを問い合わせている' do
+        Demander.any_instance.should_receive(:licenses_export).with(Time.parse('2011/10/10')).exactly(1)
+        get :licenses_export, :date => '20111010', :format => :json
+      end
+      it '@licensesにリストを取得している' do
+        get :licenses_export, :format => :json
+        assigns(:licenses).should have_at_least(3).items
+      end
+      context 'html形式' do
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :licenses_export, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがリスト構造になっている' do
+          get :licenses_export, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do
+          get :licenses_export, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("name").should be_true
+          json.first.has_key?("caption").should be_true
+          json.first.has_key?("url").should be_true
+        end
+      end
+    end
+    context '借手権限がないとき' do
+      before do
+        sign_out @demand_user
+      end
+      it 'ステータスコード302 Foundを返す' do
+        get :licenses_export
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          get :licenses_export
+          response.body.should redirect_to '/demand_users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          get :licenses_export, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '借受権限がないとき' do
+      before do
+        Demander.any_instance.stub(:status).and_return(1)
+      end
+      context 'html形式' do
+      end
+      context 'json形式' do
+        it 'ステータスコード403 forbiddenを返す' do
+          lambda{
+            get :licenses_export, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+  end
 end