WebMock :: NetConnectNotAllowedError身份验证存根与Webmock

时间:2019-02-07 19:18:06

标签: ruby box-api webmock

我目前正在使用纯Ruby应用程序,正在使用Ruby 2.5.3。我具有以下依赖性。

     group :test do                                                  |     pdf_producer.rb
--       gem 'rspec', '~> 3.5.0'                                     | util.rb
         gem 'simplecov', '~> 0.16.1'                                   Gemfile
         gem 'simplecov-console', '~> 0.4.1'                            Gemfile.lock
- -      gem 'pry', '~> 0.12.2'                                         README.md
         gem 'webmock', '~> 2.1'                                     
     end

我还使用Boxr宝石与Box Api进行交互。我创建了一个使用此gem的代理库。我目前正在尝试编写一种测试方法,以从Box中检索Box文件夹。我收到以下错误:

WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: POST https://api.box.com/oauth2/token with body 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&client_id=XXXXclient_secret=XXXXXXX&assertion=XXX' with headers {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded', 'Date'=>'Thu, 07 Feb 2019 17:46:43 GMT', 'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.5.3 (2018-10-18))'}

       You can stub this request with the following snippet:

       stub_request(:post, "https://api.box.com/oauth2/token").
         with(:body => {"assertion"=>"XXXXXX", "client_id"=>"XXXXXXXX", "client_secret"=>"XXXXXXXXXX", "grant_type"=>"urn:ietf:params:oauth:grant-type:jwt-bearer"},
              :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded', 'Date'=>'Thu, 07 Feb 2019 17:46:43 GMT', 'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.5.3 (2018-10-18))'}).
         to_return(:status => 200, :body => "", :headers => {})

这是我要编写的以下测试。

 describe '#folder' do
    context 'success' do
      it 'Retrieves a folder from Box' do
        id = double('123')
        folder_resp = double(type: 'folder', id: '123')
        box_api = BoxApi.new

        allow(box_api).to receive(:folder).with(id).and_return(folder_resp)

        expect(box_api.folder(id)).to eq(folder_resp)
      end
    end

    context 'fail' do
      it 'Raise error for folder not found' do
        stub_request(:get, "http://api.box.com/folders/123").to_return(body: {id: "123"} )
        box_api = BoxApi.new
        id = double('456')

        # allow(box_api).to receive(:folder).with(id).and_return(fail_message)

        expect(box_api.folder(id)).to eq("123")
      end
    end
  end
end

对于代理类中的以下方法。

class BoxApi
  attr_reader :user_id, :private_key, :private_key_password, :public_key_id,
              :client_id, :client_secret, :box_folder_id, :box_domain

  def initialize
    @user_id              = ENV.fetch('BOX_USER_ID')
    @private_key          = ENV.fetch('JWT_PRIVATE_KEY')
    @private_key_password = ENV.fetch('JWT_PRIVATE_KEY_PASSWORD')
    @public_key_id        = ENV.fetch('JWT_PUBLIC_KEY_ID')
    @client_id            = ENV.fetch('BOX_CLIENT_ID')
    @client_secret        = ENV.fetch('BOX_CLIENT_SECRET')
  end

  def client
    @client ||= Boxr::Client.new(token.access_token)
  end

  def token
    Boxr.get_user_token(@user_id,
                        private_key: @private_key,
                        private_key_password: @private_key_password,
                        public_key_id: @public_key_id,
                        client_id: @client_id,
                        client_secret: @client_secret)
  end

  def prepare_folder(folder_id)
    client.folder_from_id(folder_id)
  end

  def folder(box_folder_id)
    prepare_folder(box_folder_id)
  end

上面的类使用Boxr gem。我正在尝试通过ID请求获取文件夹。但是我得到了有关发帖请求的上述方法。

spec_helper.rb中,我还配置了Boxr

RSpec.configure do |config|


  # Configuration for Boxr mocking libraries for testin
  config.before(:suite) do
    Boxr::BOX_CLIENT = HTTPClient.new
  end

  config.before(:each) do
    stub_request(:post, "https://api.box.com/oauth2/token").
         with(:body => {"assertion"=>"XXXXXXXX", "grant_type"=>"urn:ietf:params:oauth:grant-type:jwt-bearer"},
              :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded', 'Date'=>'Thu, 07 Feb 2019 16:31:30 GMT', 'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.5.3 (2018-10-18))'}).
         to_return(:status => 200, :body => "", :headers => {})
  end

"assertion"=>"XXXXXXXX"中的哈希是真实哈希吗?我可以将其添加到github还是必须将其隐藏?如何解决认证错误?这是问题的根源所在吗?

0 个答案:

没有答案