OSDN Git Service

fix: any
[pettanr/pettanr.git] / spec / controllers / demanders_controller_spec.rb
index 8b1ae8f..a7326ca 100644 (file)
@@ -653,17 +653,21 @@ describe DemandersController 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 'ステータスコード302 Foundを返す' do
+          get :licenses_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :licenses_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :licenses_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :licenses_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -737,17 +741,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :artists_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :artists_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :artists_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :artists_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :artists_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -827,17 +835,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :original_pictures_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :original_pictures_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :original_pictures_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :original_pictures_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :original_pictures_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -920,17 +932,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :pictures_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :pictures_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :pictures_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :pictures_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :pictures_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -953,4 +969,109 @@ describe DemandersController do
     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
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+      sign_in @demand_user
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Demander.any_instance.stub(:export).with(any_args).and_return({:licenses => [@license], :artists => [@artist], :original_pictures => [@op], :pictures => [@p]})
+        OriginalPicture.stub(:list_as_json_text).with(any_args).and_return('[]')
+        Picture.stub(:list_as_json_text).with(any_args).and_return('[]')
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :export, :format => :json
+        response.should be_success 
+      end
+      it '日付文字列変換を依頼している' do
+        DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+        get :export, :date => '20111010', :format => :json
+      end
+      it '借手モデルにエクスポートを問い合わせている' do
+        Demander.any_instance.should_receive(:export).with(Time.parse('2011/10/10')).exactly(1)
+        get :export, :date => '20111010', :format => :json
+      end
+      it '@exportsにリストを取得している' do
+        get :export, :format => :json
+        assigns(:exports).is_a?(Hash).should be_true
+      end
+      context 'html形式' do
+      end
+      context 'json形式' do
+        it '原画モデルにリストのjson化を依頼している' do
+          OriginalPicture.should_receive(:list_as_json_text).with(any_args).exactly(1)
+          OriginalPicture.stub(:list_as_json_text).with(any_args).and_return('[]')
+          get :export, :format => :json
+        end
+        it '実素材モデルにリストのjson化を依頼している' do
+          Picture.should_receive(:list_as_json_text).with(any_args).exactly(1)
+          Picture.stub(:list_as_json_text).with(any_args).and_return('[]')
+          get :export, :format => :json
+        end
+        it 'jsonデータを返す' do
+          get :export, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがHash構造になっている' do
+          get :export, :format => :json
+          json = JSON.parse response.body
+          json.is_a?(Hash).should be_true
+        end
+        it 'Hashっぽいものであって欲しい' do
+          get :export, :format => :json
+          json = JSON.parse response.body
+          json.has_key?("licenses").should be_true
+          json.has_key?("artists").should be_true
+          json.has_key?("original_pictures").should be_true
+          json.has_key?("pictures").should be_true
+        end
+      end
+    end
+    context '借手権限がないとき' do
+      before do
+        sign_out @demand_user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :export
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :export
+          response.body.should redirect_to '/demand_users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :export, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :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 :export, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+  end
+  
 end