2010-03-26 10:27:19 +00:00
|
|
|
require 'test_helper'
|
2010-02-06 00:33:32 +00:00
|
|
|
|
|
|
|
class HttpAuthenticationTest < ActionController::IntegrationTest
|
|
|
|
|
|
|
|
test 'sign in should authenticate with http' do
|
|
|
|
sign_in_as_new_user_with_http
|
|
|
|
assert_response :success
|
2010-05-16 17:13:20 +00:00
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
2010-02-06 00:33:32 +00:00
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'returns a custom response with www-authenticate header on failures' do
|
|
|
|
sign_in_as_new_user_with_http("unknown")
|
|
|
|
assert_equal 401, status
|
|
|
|
assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
|
|
|
|
end
|
|
|
|
|
2010-02-17 20:40:01 +00:00
|
|
|
test 'uses the request format as response content type' do
|
2010-05-16 17:13:20 +00:00
|
|
|
sign_in_as_new_user_with_http("unknown")
|
2010-02-17 20:40:01 +00:00
|
|
|
assert_equal 401, status
|
2010-03-29 13:16:14 +00:00
|
|
|
assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
|
2010-05-16 17:13:20 +00:00
|
|
|
assert_match "<error>Invalid email or password.</error>", response.body
|
2010-02-17 20:40:01 +00:00
|
|
|
end
|
|
|
|
|
2010-02-06 00:33:32 +00:00
|
|
|
test 'returns a custom response with www-authenticate and chosen realm' do
|
|
|
|
swap Devise, :http_authentication_realm => "MyApp" do
|
|
|
|
sign_in_as_new_user_with_http("unknown")
|
|
|
|
assert_equal 401, status
|
|
|
|
assert_equal 'Basic realm="MyApp"', headers["WWW-Authenticate"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'sign in should authenticate with http even with specific authentication keys' do
|
|
|
|
swap Devise, :authentication_keys => [:username] do
|
2010-05-16 17:13:20 +00:00
|
|
|
sign_in_as_new_user_with_http("usertest")
|
2010-02-06 00:33:32 +00:00
|
|
|
assert_response :success
|
2010-05-16 17:13:20 +00:00
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
2010-02-06 00:33:32 +00:00
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-12 22:53:48 +00:00
|
|
|
test 'test request with oauth2 header doesnt get mistaken for basic authentication' do
|
|
|
|
swap Devise, :http_authenticatable => true do
|
|
|
|
add_oauth2_header
|
|
|
|
assert_equal 401, status
|
|
|
|
assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-14 23:45:56 +00:00
|
|
|
test 'sign in should authenticate with really long token' do
|
|
|
|
token = "token_containing_so_many_characters_that_the_base64_encoding_will_wrap"
|
|
|
|
user = create_user
|
|
|
|
user.update_attribute :authentication_token, token
|
|
|
|
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{token}:x")}"
|
|
|
|
assert_response :success
|
|
|
|
assert_match "<email>user@test.com</email>", response.body
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
|
2010-02-06 00:33:32 +00:00
|
|
|
private
|
|
|
|
|
2010-05-16 17:13:20 +00:00
|
|
|
def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
|
2010-02-06 00:33:32 +00:00
|
|
|
user = create_user
|
2010-05-16 17:13:20 +00:00
|
|
|
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
|
2010-02-06 00:33:32 +00:00
|
|
|
user
|
|
|
|
end
|
2010-09-12 22:53:48 +00:00
|
|
|
|
|
|
|
# Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
|
|
|
|
def add_oauth2_header
|
2010-09-13 10:44:38 +00:00
|
|
|
user = create_user
|
|
|
|
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{ActiveSupport::Base64.encode64("#{user.email}:123456")}"
|
2010-09-12 22:53:48 +00:00
|
|
|
end
|
|
|
|
|
2010-02-06 00:33:32 +00:00
|
|
|
end
|