20259: Add documentation for banner and tooltip features
[arvados.git] / services / api / test / functional / user_sessions_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class UserSessionsControllerTest < ActionController::TestCase
8
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
13     assert_response 404
14   end
15
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)
25   end
26
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
35   end
36
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,
48                     1.second)
49   end
50
51   [[0, 1.hour, 1.hour],
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'}
63       else
64         get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com", expires_at: Time.now() + request_lifetime}, return_to: ',https://app.example'}
65       end
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,
72                       1.second)
73     end
74   end
75
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))
85   end
86
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}
92     assert_response 400
93   end
94
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)
103   end
104
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.
110     assert_response 404
111   end
112
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'}
115     assert_response 401
116   end
117
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'}
121     assert_response 401
122   end
123
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)
131   end
132 end