OSDN Git Service

t#30326:create provider_artist
[pettanr/pettanr.git] / spec / controllers / provider_statuses_controller_spec.rb
index ed08f0e..db2c7b3 100644 (file)
@@ -9,7 +9,8 @@ describe ProviderStatusesController do
     @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    #ユーザ作成時に連動して作成される
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @artist = FactoryGirl.create :artist, :author_id => @author.id
   end
   
   describe '一覧表示に於いて' do
@@ -369,7 +370,7 @@ describe ProviderStatusesController do
       before do
         ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
-        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json')
         ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
         ProviderLicense.stub(:import).with(any_args).and_return([])
       end
@@ -383,7 +384,7 @@ describe ProviderStatusesController do
       end
       it 'エクスポートurl取得を依頼している' do
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
-        ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, @ps.token, Time.parse('2011/10/10')).exactly(1)
+        ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'licenses_export', @ps.token, Time.parse('2011/10/10')).exactly(1)
         get :licenses_import, :id => @ps.id, :date => '20111010'
       end
       it '貸手からのエクスポートを依頼している' do
@@ -400,7 +401,7 @@ describe ProviderStatusesController do
         ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
-        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json')
         ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
         ProviderLicense.stub(:import).with(any_args).and_return([])
       end
@@ -475,4 +476,121 @@ describe ProviderStatusesController do
     end
   end
   
+  describe '絵師インポートに於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+      ProviderStatus.stub(:show).and_return(@ps)
+      sign_in @admin
+    end
+    context '事前チェックしておく' do
+      before do
+        ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json')
+        ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes])
+        ProviderArtist.stub(:import).with(any_args).and_return([])
+      end
+      it '借受状況モデルに単体取得を問い合わせている' do
+        ProviderStatus.should_receive(:show).exactly(1)
+        get :artists_import, :id => @ps.id
+      end
+      it '日付文字列変換を依頼している' do
+        ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+        get :licenses_import, :id => @ps.id, :date => '20111010'
+      end
+      it 'エクスポートurl取得を依頼している' do
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
+        ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'artists_export', @ps.token, Time.parse('2011/10/10')).exactly(1)
+        get :artists_import, :id => @ps.id, :date => '20111010'
+      end
+      it '貸手からのエクスポートを依頼している' do
+        ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1)
+        get :artists_import, :id => @ps.id
+      end
+      it '絵師対照表モデルにインポートを依頼している' do
+        ProviderArtist.should_receive(:import).exactly(1)
+        get :artists_import, :id => @ps.id
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json')
+        ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes])
+        ProviderLicense.stub(:import).with(any_args).and_return([])
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :artists_import, :id => @ps.id
+        response.should be_success
+      end
+      it '@provider_statusに借受状況を取得している' do
+        get :artists_import, :id => @ps.id
+        assigns(:provider_status).should eq(@ps)
+      end
+      it '@failuresにインポート失敗リストを取得している' do
+        get :artists_import, :id => @ps.id
+        assigns(:failures).should eq([])
+      end
+      context 'html形式' do
+        it 'artists_importテンプレートを描画する' do
+          get :artists_import, :id => @ps.id
+          response.should render_template("artists_import")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :artists_import, :id => @ps.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :artists_import, :id => @ps.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :artists_import, :id => @ps.id
+          response.body.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :artists_import, :id => @ps.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :artists_import, :id => @ps.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '借受権限がないとき' do
+      before do
+        ProviderStatus.any_instance.stub(:status).and_return(0)
+      end
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :artists_import, :id => @ps.id
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :artists_import, :id => @ps.id, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+  end
+  
 end