| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
class HttpBasicAuthenticationTest < Test::Unit::TestCase |
|---|
| 4 |
include ActionController::HttpAuthentication::Basic |
|---|
| 5 |
|
|---|
| 6 |
class DummyController |
|---|
| 7 |
attr_accessor :headers, :renders, :request |
|---|
| 8 |
|
|---|
| 9 |
def initialize |
|---|
| 10 |
@headers, @renders = {}, [] |
|---|
| 11 |
@request = ActionController::TestRequest.new |
|---|
| 12 |
end |
|---|
| 13 |
|
|---|
| 14 |
def render(options) |
|---|
| 15 |
self.renders << options |
|---|
| 16 |
end |
|---|
| 17 |
end |
|---|
| 18 |
|
|---|
| 19 |
def setup |
|---|
| 20 |
@controller = DummyController.new |
|---|
| 21 |
@credentials = ActionController::HttpAuthentication::Basic.encode_credentials("dhh", "secret") |
|---|
| 22 |
end |
|---|
| 23 |
|
|---|
| 24 |
def test_successful_authentication |
|---|
| 25 |
login = Proc.new { |user_name, password| user_name == "dhh" && password == "secret" } |
|---|
| 26 |
set_headers |
|---|
| 27 |
assert authenticate(@controller, &login) |
|---|
| 28 |
|
|---|
| 29 |
set_headers '' |
|---|
| 30 |
assert_nothing_raised do |
|---|
| 31 |
assert !authenticate(@controller, &login) |
|---|
| 32 |
end |
|---|
| 33 |
|
|---|
| 34 |
set_headers nil |
|---|
| 35 |
set_headers @credentials, 'REDIRECT_X_HTTP_AUTHORIZATION' |
|---|
| 36 |
assert authenticate(@controller, &login) |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
def test_failing_authentication |
|---|
| 40 |
set_headers |
|---|
| 41 |
assert !authenticate(@controller) { |user_name, password| user_name == "dhh" && password == "incorrect" } |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
def test_authentication_request |
|---|
| 45 |
authentication_request(@controller, "Megaglobalapp") |
|---|
| 46 |
assert_equal 'Basic realm="Megaglobalapp"', @controller.headers["WWW-Authenticate"] |
|---|
| 47 |
assert_equal :unauthorized, @controller.renders.first[:status] |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
private |
|---|
| 51 |
def set_headers(value = @credentials, name = 'HTTP_AUTHORIZATION') |
|---|
| 52 |
@controller.request.env[name] = value |
|---|
| 53 |
end |
|---|
| 54 |
end |
|---|