OSDN Git Service

update license test
[pettanr/pettanr.git] / spec / controllers / original_licenses_controller_spec.rb
index b6f01eb..4c5df40 100644 (file)
@@ -456,6 +456,133 @@ describe OriginalLicensesController do
   end
 
   describe 'インポートに於いて' do
+    before do
+      sign_in @admin
+      sign_in @user
+      #テストデータを用意してね
+      @f = Rails.root + 'spec/json/original_license.json'
+      @t = File.open(@f, 'r').read
+      @j = JSON.parse @t
+      @fs = Rails.root + 'spec/json/original_licenses.json'
+      @ts = File.open(@fs, 'r').read
+      @js = JSON.parse @ts
+      @fes = Rails.root + 'spec/json/invalid_original_licenses.json'
+      @tes = File.open(@fes, 'r').read
+      @jes = JSON.parse @tes
+    end
+    context '事前チェックしておく' do
+      before do
+        #異常な行を返すから、正常の意味で空を返す
+        OriginalLicense.stub(:import).with(any_args()).and_return([])
+      end
+      it "@dataに渡したデータを保持している" do
+        post :import, :file => @t
+        assigns(:data).should_not be_nil
+      end
+      it 'モデルにインポート依頼する' do
+        OriginalLicense.should_receive(:import).with(any_args()).exactly(1)
+        post :import, :file => @t
+      end
+      it "@errorsに結果を保持している" do
+        post :import, :file => @t
+        assigns(:errors).should eq []
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :import, :file => @t
+          response.status.should eq 302
+        end
+        it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
+          post :import, :file => @t
+          response.should redirect_to('/original_licenses/list')
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :import, :file => @t, :format => :json
+          response.should be_success 
+        end
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :import, :file => @t
+          response.status.should eq 302
+        end
+        it '管理者サインインページへ遷移する' do
+          post :import, :file => @t
+          response.body.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :import, :file => @t, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :import, :file => @t, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      #必要なのは管理者権限であって作家権限ではない。成功を見届ける
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :import, :file => @t
+          response.status.should eq 302
+        end
+        it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
+          post :import, :file => @t
+          response.should redirect_to('/original_licenses/list')
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :import, :file => @t, :format => :json
+          response.should be_success 
+        end
+      end
+    end
+    context '検証、保存に失敗した' do
+      before do
+        #異常な行を返す
+        OriginalLicense.stub(:import).with(any_args()).and_return(
+          [OriginalLicense.new(Factory.attributes_for(:original_license))]
+        )
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :import, :file => @t
+          response.status.should eq 200
+        end
+        it 'resultページを描画する' do
+          post :import, :file => @t
+          response.should render_template("result")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          post :import, :file => @t, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          post :import, :file => @t, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
   end
   
 end