RubyLearning / ProjectTrak / vendor / rails / actionpack / test / controller / http_authentication_test.rb

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.