【问题标题】:Problems for request rspec with basic http auth using Devise使用 Devise 的基本 http auth 请求 rspec 的问题
【发布时间】:2017-06-03 01:44:13
【问题描述】:
Devise: 4.20
Rails: 5.0.1
Rspec: 3.5

我曾使用此链接https://github.com/plataformatec/devise/wiki/How-To:-Use-HTTP-Basic-Authentication,但我无法使用 rspec 为我的 api 测试 http 基本身份验证以进行请求测试。以下是错误示例:

app/controllers/api/base_controller.rb

module Api
  class BaseController < ApplicationController
    before_action :authenticate_user!

    protected

    def authenticate_user!
      authenticate_or_request_with_http_basic do |username, password|
        resource = User.find_by_username(username)
        if resource
          sign_in :user, resource if resource.valid_password?(password)
        else
          request_http_basic_authentication
        end
      end
    end
  end
end

app/controllers/api/v1/car_controller.rb

module Api
  module V1
    class CarController < Api::BaseController
      respond_to :json

      def index
        @cars = Car.all
        render :json => {:content => @cars}, :status => 200
      end
    end
  end
end

spec/requests/api/v1/car_controller_spec.rb

require 'rails_helper'

RSpec.describe "Servers API", :type => :request do
  it 'sends a list of servers' do
    admin = FactoryGirl.create(:admin)
    @env = {}
    @env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(admin.username, admin.password)
    get "/api/cars", :params => {}, :headers => @env

    # test for the 200 status-code
    expect(response.status).to eq(200)

  end
end

当我运行规范时,出现以下错误:

# --- Caused by: ---
     # NoMethodError:
     #   undefined method `sign_in' for #<Api::V1::CarController:0x0000000609ef12>
     # ./app/controllers/api/base_controller.rb:10 in block in authenticate_user!

有人可以帮助我吗?谢谢。

【问题讨论】:

    标签: ruby-on-rails rspec devise


    【解决方案1】:

    我的规格通过了类似的设置,您是否也显示您的spec_helper 内容,看起来您不包括Devise::TestHelpers

    spec_helper

    RSpec.configure do |config|
      config.include Devise::TestHelpers
      config.include Warden::Test::Helpers
      config.before { Warden.test_mode! }
      config.after { Warden.test_reset! }
    
      config.before(:each) do
        @headers = { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
      end
    end
    

    我的测试看起来像这样:

    RSpec.describe 'Users' do
      context 'when client is authorized' do
        let(:user) { FactoryGirl.create(:user) }
    
        it 'gets user' do
          @headers['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.
            encode_credentials(
              user.authentication_token,
              email: user.email
            )
          get api_v1_user_url(id: user.id), {}, @headers
          expect(response.status).to eq(200)
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      相关资源
      最近更新 更多