Rails rspec如何在index_spec中测试基本的HTTP身份验证

时间:2019-05-30 10:28:08

标签: ruby-on-rails rspec

我想将在其他文件中定义的基本http auth添加到我的规格中。我想使用来自secret.yml的ENV凭据。我想我必须在端点中添加一行,但这是我的问题。

index_spec.rb

describe API::V1::Inquiries::ExportsData::Index, type: :request do
  subject(:call) { get endpoint, params: params }

  let(:endpoint) { "/api/v1/inquiries/export_data?shop_code=#{shop.code}" }
  let(:params) { { shop_code: '98918081' } }


# several `let` with expected_data let either

 it 'returns expected value' do
       call
       expect(data).to match_array(expected_data)
 end
end

base.rb

class Base < Grape::API
          mount Inquiries::ExportsData::Index
          before do
            authenticate!
          end

          helpers do
            def authenticate!
              authenticate_or_request_with_http_basic do |username, password|
                username == Rails.applications.secrets.ENV['EXPORT_API_USERNAME'] &&
                  password == Rails.applications.secrets.ENV['EXPORT_API_PASSWORD']
              end
            end
          end
        end

0 个答案:

没有答案