2010-03-26 11:27:19 +01:00
|
|
|
require 'test_helper'
|
2010-02-06 01:33:32 +01:00
|
|
|
|
2015-08-18 16:42:56 -07:00
|
|
|
class HttpAuthenticationTest < Devise::IntegrationTest
|
2011-06-28 22:13:35 -03:00
|
|
|
test 'handles unverified requests gets rid of caches but continues signed in' do
|
2014-02-25 22:12:55 +05:30
|
|
|
swap ApplicationController, allow_forgery_protection: true do
|
2011-06-28 22:13:35 -03:00
|
|
|
create_user
|
2015-08-18 16:42:56 -07:00
|
|
|
post exhibit_user_url(1), headers: { "HTTP_AUTHORIZATION" => "Basic #{Base64.encode64("user@test.com:12345678")}" }
|
2011-06-28 22:13:35 -03:00
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
assert_equal "User is authenticated", response.body
|
|
|
|
end
|
|
|
|
end
|
2010-02-06 01:33:32 +01:00
|
|
|
|
|
|
|
test 'sign in should authenticate with http' do
|
|
|
|
sign_in_as_new_user_with_http
|
2011-12-11 20:39:41 +01:00
|
|
|
assert_response 200
|
2010-05-16 19:13:20 +02:00
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
2010-02-06 01:33:32 +01:00
|
|
|
assert warden.authenticated?(:user)
|
2011-12-11 20:39:41 +01:00
|
|
|
|
2014-02-25 22:12:55 +05:30
|
|
|
get users_path(format: :xml)
|
2011-12-11 20:39:41 +01:00
|
|
|
assert_response 200
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'sign in should authenticate with http but not emit a cookie if skipping session storage' do
|
2014-02-25 22:12:55 +05:30
|
|
|
swap Devise, skip_session_storage: [:http_auth] do
|
2011-12-11 20:39:41 +01:00
|
|
|
sign_in_as_new_user_with_http
|
|
|
|
assert_response 200
|
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
|
2014-02-25 22:12:55 +05:30
|
|
|
get users_path(format: :xml)
|
2011-12-11 20:39:41 +01:00
|
|
|
assert_response 401
|
|
|
|
end
|
2010-02-06 01:33:32 +01:00
|
|
|
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 21:40:01 +01:00
|
|
|
test 'uses the request format as response content type' do
|
2010-05-16 19:13:20 +02:00
|
|
|
sign_in_as_new_user_with_http("unknown")
|
2010-02-17 21:40:01 +01:00
|
|
|
assert_equal 401, status
|
2010-03-29 15:16:14 +02:00
|
|
|
assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
|
2014-09-11 08:38:14 -07:00
|
|
|
assert_match "<error>Invalid email or password.</error>", response.body
|
2010-02-17 21:40:01 +01:00
|
|
|
end
|
|
|
|
|
2010-02-06 01:33:32 +01:00
|
|
|
test 'returns a custom response with www-authenticate and chosen realm' do
|
2014-02-25 22:12:55 +05:30
|
|
|
swap Devise, http_authentication_realm: "MyApp" do
|
2010-02-06 01:33:32 +01:00
|
|
|
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
|
2014-02-25 22:12:55 +05:30
|
|
|
swap Devise, authentication_keys: [:username] do
|
2010-05-16 19:13:20 +02:00
|
|
|
sign_in_as_new_user_with_http("usertest")
|
2010-02-06 01:33:32 +01:00
|
|
|
assert_response :success
|
2010-05-16 19:13:20 +02:00
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
2010-02-06 01:33:32 +01:00
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-04 12:18:20 -05:00
|
|
|
test 'it uses appropriate authentication_keys when configured with hash' do
|
2014-02-25 22:12:55 +05:30
|
|
|
swap Devise, authentication_keys: ActiveSupport::OrderedHash[:username, false, :email, false] do
|
2013-03-04 12:18:20 -05:00
|
|
|
sign_in_as_new_user_with_http("usertest")
|
|
|
|
assert_response :success
|
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'it uses the appropriate key when configured explicitly' do
|
2014-02-25 22:12:55 +05:30
|
|
|
swap Devise, authentication_keys: ActiveSupport::OrderedHash[:email, false, :username, false], http_authentication_key: :username do
|
2013-03-04 12:18:20 -05:00
|
|
|
sign_in_as_new_user_with_http("usertest")
|
|
|
|
assert_response :success
|
|
|
|
assert_match '<email>user@test.com</email>', response.body
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-13 06:53:48 +08:00
|
|
|
test 'test request with oauth2 header doesnt get mistaken for basic authentication' do
|
2014-02-25 22:12:55 +05:30
|
|
|
swap Devise, http_authenticatable: true do
|
2010-09-13 06:53:48 +08:00
|
|
|
add_oauth2_header
|
|
|
|
assert_equal 401, status
|
|
|
|
assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-06 01:33:32 +01:00
|
|
|
private
|
2012-07-06 11:46:46 -03:00
|
|
|
def sign_in_as_new_user_with_http(username="user@test.com", password="12345678")
|
2010-02-06 01:33:32 +01:00
|
|
|
user = create_user
|
2015-08-18 16:42:56 -07:00
|
|
|
get users_path(format: :xml), headers: { "HTTP_AUTHORIZATION" => "Basic #{Base64.encode64("#{username}:#{password}")}" }
|
2010-02-06 01:33:32 +01:00
|
|
|
user
|
|
|
|
end
|
2010-11-26 01:14:26 -02:00
|
|
|
|
2010-09-13 06:53:48 +08: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 18:44:38 +08:00
|
|
|
user = create_user
|
2015-08-18 16:42:56 -07:00
|
|
|
get users_path(format: :xml), headers: { "HTTP_AUTHORIZATION" => "OAuth #{Base64.encode64("#{user.email}:12345678")}" }
|
2010-09-13 06:53:48 +08:00
|
|
|
end
|
|
|
|
|
2010-11-26 01:14:26 -02:00
|
|
|
end
|