X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=blobdiff_plain;f=spec%2Fcontrollers%2Foriginal_licenses_controller_spec.rb;h=4c5df40ce176b110909d5b719d6a358cad828570;hp=b6f01ebb4e69c734cfaf0794e00a1efd6ed95d3c;hb=9ffd93ec6449ed003acbe8022902d9fe9f41c2c9;hpb=261d481751fff922dcdfc391aa7932d344fb447c diff --git a/spec/controllers/original_licenses_controller_spec.rb b/spec/controllers/original_licenses_controller_spec.rb index b6f01ebb..4c5df40c 100644 --- a/spec/controllers/original_licenses_controller_spec.rb +++ b/spec/controllers/original_licenses_controller_spec.rb @@ -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