【问题标题】:RSpec test for webhook controllers with basic auth具有基本身份验证的 Webhook 控制器的 RSpec 测试
【发布时间】:2020-06-17 22:23:00
【问题描述】:

在我的 Rails/Grape 应用程序中,我创建了一个从 CMS webhook 接收 JSON 的 webhook 控制器。我只是想知道如果我没有任何参数,如何在 RSpec 中测试它(我想我不需要它,因为我只从 webhook 接收 JSON)。

我的 webhook 控制器(效果很好):

module Cms
  class Webhook < Base
    desc 'Take the CMS webhook'

    http_basic do |user, password|
      user == ENV['USER'] && password == ENV['PASSWORD']
    end

    post :receive do
      params
    end
  end
end

我想点赞:

describe Cms::Webhooks, type: :request do
  subject(:call) { post endpoint, params: params, as: :json }

  let(:endpoint) { '/api/cms/webhooks/receive' }

  let(:params) do
    {
      some: 'some pass'
    }
  end

  it 'returns a successful response' do
    call
    expect(response).to be_successful
  end
end

我收到一个错误:

 Failure/Error: expect(response).to be_successful
   expected `#<ActionDispatch::TestResponse:0x00007f9058e43e60 @mon_data=#<Monitor:0x00007f9058e43de8>, @mon_data_..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>>.successful?` to return true, got false

【问题讨论】:

    标签: ruby-on-rails ruby rspec grape


    【解决方案1】:

    你能试试这个代码吗?

    describe Cms::Webhooks, type: :request do
      subject(:call) { post endpoint, params: params, as: :son, headers: headers }
    
      let(:endpoint) { '/api/cms/webhooks/receive' }
    
      let(:params) do
        {
          some: 'some pass'
        }
      end
    
      let(:headers) do
        {
          'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials('your_username', 'your_password')
        }
      end  
    
      it 'returns a successful response' do
        call
        expect(response).to be_successful
      end
    end
    

    参考https://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多