module Rails::Auth::RSpec::HelperMethods

RSpec helper methods

Public Instance Methods

test_credentials() click to toggle source

Credentials to be injected into the request during tests

# File lib/rails/auth/rspec/helper_methods.rb, line 9
def test_credentials
  Rails.configuration.x.rails_auth.test_credentials
end
with_credentials(credentials = {}) click to toggle source

Perform a test with the given credentials NOTE: Credentials will be cleared after the block. Nesting is not allowed.

# File lib/rails/auth/rspec/helper_methods.rb, line 15
def with_credentials(credentials = {})
  raise TypeError, "expected Hash of credentials, got #{credentials.class}" unless credentials.is_a?(Hash)

  test_credentials.clear

  credentials.each do |type, value|
    test_credentials[type.to_s] = value
  end
ensure
  test_credentials.clear
end
x509_certificate(cn: nil, ou: nil) click to toggle source

Creates an Rails::Auth::X509::Certificate instance double

# File lib/rails/auth/rspec/helper_methods.rb, line 28
def x509_certificate(cn: nil, ou: nil)
  subject = ""
  subject += "CN=#{cn}" if cn
  subject += "OU=#{ou}" if ou

  instance_double(Rails::Auth::X509::Certificate, subject, cn: cn, ou: ou).tap do |certificate|
    allow(certificate).to receive(:[]) do |key|
      {
        "CN" => cn,
        "OU" => ou
      }[key.to_s.upcase]
    end
  end
end
x509_certificate_hash(**args) click to toggle source

Creates a certificates hash containing a single X.509 certificate instance double

# File lib/rails/auth/rspec/helper_methods.rb, line 44
def x509_certificate_hash(**args)
  { "x509" => x509_certificate(**args) }
end