Sha256: ef2fc01579bd3ccd87bc9d772f3fb08da813d11d91adcf4d4500bacc2e5b8ff1

Contents?: true

Size: 1.51 KB

Versions: 15

Compression:

Stored size: 1.51 KB

Contents

require 'abstract_unit'

class HttpBasicAuthenticationTest < Test::Unit::TestCase
  include ActionController::HttpAuthentication::Basic
  
  class DummyController
    attr_accessor :headers, :renders, :request
    
    def initialize
      @headers, @renders = {}, []
      @request = ActionController::TestRequest.new
    end
    
    def render(options)
      self.renders << options
    end
  end

  def setup
    @controller  = DummyController.new
    @credentials = ActionController::HttpAuthentication::Basic.encode_credentials("dhh", "secret")
  end

  def test_successful_authentication
    login = Proc.new { |user_name, password| user_name == "dhh" && password == "secret" }
    set_headers
    assert authenticate(@controller, &login)

    set_headers ''
    assert_nothing_raised do
      assert !authenticate(@controller, &login)
    end

    set_headers nil
    set_headers @credentials, 'REDIRECT_X_HTTP_AUTHORIZATION'
    assert authenticate(@controller, &login)
  end

  def test_failing_authentication
    set_headers
    assert !authenticate(@controller) { |user_name, password| user_name == "dhh" && password == "incorrect" }
  end
  
  def test_authentication_request
    authentication_request(@controller, "Megaglobalapp")
    assert_equal 'Basic realm="Megaglobalapp"', @controller.headers["WWW-Authenticate"]
    assert_equal :unauthorized, @controller.renders.first[:status]
  end

  private
    def set_headers(value = @credentials, name = 'HTTP_AUTHORIZATION')
      @controller.request.env[name] = value
    end
end

Version data entries

15 entries across 14 versions & 6 rubygems

Version Path
radiant-0.7.2 vendor/rails/actionpack/test/controller/http_authentication_test.rb
vibes-bj-1.2.2 spec/rails_root/vendor/rails/actionpack/test/controller/http_authentication_test.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/actionpack/test/controller/http_authentication_test.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/actionpack/pkg/actionpack-2.2.2/test/controller/http_authentication_test.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/actionpack/test/controller/http_authentication_test.rb
actionpack-2.2.3 test/controller/http_authentication_test.rb
actionpack-2.2.2 test/controller/http_authentication_test.rb
actionpack-2.1.0 test/controller/http_authentication_test.rb
actionpack-2.1.1 test/controller/http_authentication_test.rb
actionpack-2.1.2 test/controller/http_authentication_test.rb
antfarm-0.3.0 rails/vendor/rails/actionpack/test/controller/http_authentication_test.rb
antfarm-0.4.0 rails/vendor/rails/actionpack/test/controller/http_authentication_test.rb
eactionpack-2.1.2 test/controller/http_authentication_test.rb
radiant-0.7.0 vendor/rails/actionpack/test/controller/http_authentication_test.rb
radiant-0.7.1 vendor/rails/actionpack/test/controller/http_authentication_test.rb