Sha256: 8f8640f2418b4813a3327ce45f97c84fad85c7b54bef5fb709ce3b0f1a63cd3a

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

module Rails
  module Auth
    module RSpec
      # RSpec helper methods
      module HelperMethods
        # Creates an Rails::Auth::X509::Certificate instance double
        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

        # Creates a certificates hash containing a single X.509 certificate instance double
        def x509_certificate_hash(**args)
          { "x509" => x509_certificate(**args) }
        end

        Rails::Auth::ACL::Resource::HTTP_METHODS.each do |method|
          define_method("#{method.downcase}_request") do |certificates: {}|
            path = self.class.description

            # Warn if methods are improperly used
            unless path.chars[0] == "/"
              raise ArgumentError, "expected #{path} to start with '/'"
            end

            env = {
              "REQUEST_METHOD" => method,
              "PATH_INFO"      => self.class.description
            }

            certificates.each do |type, value|
              Rails::Auth.add_credential(env, type.to_s, value)
            end

            env
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails-auth-1.3.0 lib/rails/auth/rspec/helper_methods.rb
rails-auth-1.2.0 lib/rails/auth/rspec/helper_methods.rb
rails-auth-1.1.0 lib/rails/auth/rspec/helper_methods.rb
rails-auth-1.0.0 lib/rails/auth/rspec/helper_methods.rb
rails-auth-0.5.3 lib/rails/auth/rspec/helper_methods.rb
rails-auth-0.5.2 lib/rails/auth/rspec/helper_methods.rb
rails-auth-0.5.1 lib/rails/auth/rspec/helper_methods.rb
rails-auth-0.5.0 lib/rails/auth/rspec/helper_methods.rb
rails-auth-0.4.1 lib/rails/auth/rspec/helper_methods.rb