1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class UserSessionsControllerTest < ActionController::TestCase
9 test "redirect to joshid" do
10 api_client_page = 'http://client.example.com/home'
11 get :login, params: {return_to: api_client_page}
12 # Not supported any more
16 test "send token when user is already logged in" do
17 authorize_with :inactive
18 api_client_page = 'http://client.example.com/home'
19 get :login, params: {return_to: api_client_page}
20 assert_response :redirect
21 assert_equal(0, @response.redirect_url.index(api_client_page + '?'),
22 'Redirect url ' + @response.redirect_url +
23 ' should start with ' + api_client_page + '?')
24 assert_not_nil assigns(:api_client)
27 test "login creates token without expiration by default" do
28 assert_equal Rails.configuration.Login.TokenLifetime, 0
29 authorize_with :inactive
30 api_client_page = 'http://client.example.com/home'
31 get :login, params: {return_to: api_client_page}
32 assert_response :redirect
33 assert_not_nil assigns(:api_client)
34 assert_nil assigns(:api_client_auth).expires_at
37 test "login creates token with configured lifetime" do
38 token_lifetime = 1.hour
39 Rails.configuration.Login.TokenLifetime = token_lifetime
40 authorize_with :inactive
41 api_client_page = 'http://client.example.com/home'
42 get :login, params: {return_to: api_client_page}
43 assert_response :redirect
44 assert_not_nil assigns(:api_client)
45 api_client_auth = assigns(:api_client_auth)
46 assert_in_delta(api_client_auth.expires_at,
47 api_client_auth.updated_at + token_lifetime,
52 [1.hour, 2.hour, 1.hour],
53 [2.hour, 1.hour, 1.hour],
54 [2.hour, nil, 2.hour],
55 ].each do |config_lifetime, request_lifetime, expect_lifetime|
56 test "login with TokenLifetime=#{config_lifetime} and request has expires_at=#{ request_lifetime.nil? ? "nil" : request_lifetime }" do
57 Rails.configuration.Login.TokenLifetime = config_lifetime
58 expected_expiration_time = Time.now() + expect_lifetime
59 authorize_with :inactive
60 @request.headers['Authorization'] = 'Bearer '+Rails.configuration.SystemRootToken
61 if request_lifetime.nil?
62 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
64 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com", expires_at: Time.now() + request_lifetime}, return_to: ',https://app.example'}
66 assert_response :redirect
67 api_client_auth = assigns(:api_client_auth)
68 assert_not_nil api_client_auth
69 assert_not_nil assigns(:api_client)
70 assert_in_delta(api_client_auth.expires_at,
71 expected_expiration_time,
76 test "login with remote param returns a salted token" do
77 authorize_with :inactive
78 api_client_page = 'http://client.example.com/home'
79 remote_prefix = 'zbbbb'
80 get :login, params: {return_to: api_client_page, remote: remote_prefix}
81 assert_response :redirect
82 api_client_auth = assigns(:api_client_auth)
83 assert_not_nil api_client_auth
84 assert_includes(@response.redirect_url, 'api_token='+api_client_auth.salted_token(remote: remote_prefix))
87 test "login with malformed remote param returns an error" do
88 authorize_with :inactive
89 api_client_page = 'http://client.example.com/home'
90 remote_prefix = 'invalid_cluster_id'
91 get :login, params: {return_to: api_client_page, remote: remote_prefix}
95 test "login to LoginCluster" do
96 Rails.configuration.Login.LoginCluster = 'zbbbb'
97 Rails.configuration.RemoteClusters['zbbbb'] = ConfigLoader.to_OrderedOptions({'Host' => 'zbbbb.example.com'})
98 api_client_page = 'http://client.example.com/home'
99 get :login, params: {return_to: api_client_page}
100 assert_response :redirect
101 assert_equal("https://zbbbb.example.com/login?return_to=http%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
102 assert_nil assigns(:api_client)
105 test "don't go into redirect loop if LoginCluster is self" do
106 Rails.configuration.Login.LoginCluster = 'zzzzz'
107 api_client_page = 'http://client.example.com/home'
108 get :login, params: {return_to: api_client_page}
109 # Doesn't redirect, just fail.
113 test "controller cannot create session without SystemRootToken" do
114 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
118 test "controller cannot create session with wrong SystemRootToken" do
119 @request.headers['Authorization'] = 'Bearer blah'
120 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
124 test "controller can create session using SystemRootToken" do
125 @request.headers['Authorization'] = 'Bearer '+Rails.configuration.SystemRootToken
126 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
127 assert_response :redirect
128 api_client_auth = assigns(:api_client_auth)
129 assert_not_nil api_client_auth
130 assert_includes(@response.redirect_url, 'api_token='+api_client_auth.token)