5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RSpec3でBASIC認証がかかったアクションをテストする

Last updated at Posted at 2015-07-07

Basic認証かけたアクションのテストについて書きます。

ModuleのAuthHelper

module AuthHelper
  def basic_login
    @env ||= {} #またはenv
    user = 'arekore'
    pw = 'passarekore'
    @env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
  end
end

ユーザ名とパスワードはご自分のに読み替えてください。

ただし@request.envに入れてあげないと認証は通りません。

module AuthHelper
  def basic_login
    @request.env ||= {}
    user = 'arekore'
    pw = 'passarekore'
    @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
  end
end

これなら通ります。

spec_helperに以下を追記して各specで呼び出すといいです。

RSpec.configure do |config|
  config.include AuthHelper, :type => :controller
end
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?