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 assert_response :redirect
13 assert_equal("http://test.host/auth/joshid?return_to=%2Chttp%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
14 assert_nil assigns(:api_client)
17 test "send token when user is already logged in" do
18 authorize_with :inactive
19 api_client_page = 'http://client.example.com/home'
20 get :login, params: {return_to: api_client_page}
21 assert_response :redirect
22 assert_equal(0, @response.redirect_url.index(api_client_page + '?'),
23 'Redirect url ' + @response.redirect_url +
24 ' should start with ' + api_client_page + '?')
25 assert_not_nil assigns(:api_client)
28 test "login creates token without expiration by default" do
29 assert_equal Rails.configuration.Login.TokenLifetime, 0
30 authorize_with :inactive
31 api_client_page = 'http://client.example.com/home'
32 get :login, params: {return_to: api_client_page}
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_not_nil assigns(:api_client)
44 api_client_auth = assigns(:api_client_auth)
45 assert_in_delta(api_client_auth.expires_at,
46 api_client_auth.updated_at + token_lifetime,
50 test "login with remote param returns a salted token" do
51 authorize_with :inactive
52 api_client_page = 'http://client.example.com/home'
53 remote_prefix = 'zbbbb'
54 get :login, params: {return_to: api_client_page, remote: remote_prefix}
55 assert_response :redirect
56 api_client_auth = assigns(:api_client_auth)
57 assert_not_nil api_client_auth
58 assert_includes(@response.redirect_url, 'api_token='+api_client_auth.salted_token(remote: remote_prefix))
61 test "login with malformed remote param returns an error" do
62 authorize_with :inactive
63 api_client_page = 'http://client.example.com/home'
64 remote_prefix = 'invalid_cluster_id'
65 get :login, params: {return_to: api_client_page, remote: remote_prefix}
69 test "login to LoginCluster" do
70 Rails.configuration.Login.LoginCluster = 'zbbbb'
71 Rails.configuration.RemoteClusters['zbbbb'] = ConfigLoader.to_OrderedOptions({'Host' => 'zbbbb.example.com'})
72 api_client_page = 'http://client.example.com/home'
73 get :login, params: {return_to: api_client_page}
74 assert_response :redirect
75 assert_equal("https://zbbbb.example.com/login?return_to=http%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
76 assert_nil assigns(:api_client)
79 test "don't go into redirect loop if LoginCluster is self" do
80 Rails.configuration.Login.LoginCluster = 'zzzzz'
81 api_client_page = 'http://client.example.com/home'
82 get :login, params: {return_to: api_client_page}
83 assert_response :redirect
84 assert_equal("http://test.host/auth/joshid?return_to=%2Chttp%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
85 assert_nil assigns(:api_client)
88 test "controller cannot create session without SystemRootToken" do
89 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
93 test "controller cannot create session with wrong SystemRootToken" do
94 @request.headers['Authorization'] = 'Bearer blah'
95 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
99 test "controller can create session using SystemRootToken" do
100 @request.headers['Authorization'] = 'Bearer '+Rails.configuration.SystemRootToken
101 get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
102 assert_response :redirect
103 api_client_auth = assigns(:api_client_auth)
104 assert_not_nil api_client_auth
105 assert_includes(@response.redirect_url, 'api_token='+api_client_auth.token)