129464cf1c5dd5c9e5e3730aa4f9abf12565c2e2
[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     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)
15   end
16
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)
26   end
27
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
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_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,
47                     1.second)
48   end
49
50   [[0, 1.hour, 1.hour],
51   [1.hour, 2.hour, 1.hour],
52   [2.hour, 1.hour, 1.hour],
53   [2.hour, nil, 2.hour],
54   ].each do |config_lifetime, request_lifetime, expect_lifetime|
55     test "login with TokenLifetime=#{config_lifetime} and request has expires_at=#{ request_lifetime.nil? ? "nil" : request_lifetime }" do
56       Rails.configuration.Login.TokenLifetime = config_lifetime
57       expected_expiration_time =  Time.now() + expect_lifetime
58       authorize_with :inactive
59       @request.headers['Authorization'] = 'Bearer '+Rails.configuration.SystemRootToken
60       if request_lifetime.nil?
61         get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
62       else
63         get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com", expires_at: Time.now() + request_lifetime}, return_to: ',https://app.example'}
64       end
65       assert_response :redirect
66       api_client_auth = assigns(:api_client_auth)
67       assert_not_nil api_client_auth
68       assert_not_nil assigns(:api_client)
69       assert_in_delta(api_client_auth.expires_at,
70                       expected_expiration_time,
71                       1.second)
72     end
73   end
74
75   test "login with remote param returns a salted token" do
76     authorize_with :inactive
77     api_client_page = 'http://client.example.com/home'
78     remote_prefix = 'zbbbb'
79     get :login, params: {return_to: api_client_page, remote: remote_prefix}
80     assert_response :redirect
81     api_client_auth = assigns(:api_client_auth)
82     assert_not_nil api_client_auth
83     assert_includes(@response.redirect_url, 'api_token='+api_client_auth.salted_token(remote: remote_prefix))
84   end
85
86   test "login with malformed remote param returns an error" do
87     authorize_with :inactive
88     api_client_page = 'http://client.example.com/home'
89     remote_prefix = 'invalid_cluster_id'
90     get :login, params: {return_to: api_client_page, remote: remote_prefix}
91     assert_response 400
92   end
93
94   test "login to LoginCluster" do
95     Rails.configuration.Login.LoginCluster = 'zbbbb'
96     Rails.configuration.RemoteClusters['zbbbb'] = ConfigLoader.to_OrderedOptions({'Host' => 'zbbbb.example.com'})
97     api_client_page = 'http://client.example.com/home'
98     get :login, params: {return_to: api_client_page}
99     assert_response :redirect
100     assert_equal("https://zbbbb.example.com/login?return_to=http%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
101     assert_nil assigns(:api_client)
102   end
103
104   test "don't go into redirect loop if LoginCluster is self" do
105     Rails.configuration.Login.LoginCluster = 'zzzzz'
106     api_client_page = 'http://client.example.com/home'
107     get :login, params: {return_to: api_client_page}
108     assert_response :redirect
109     assert_equal("http://test.host/auth/joshid?return_to=%2Chttp%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
110     assert_nil assigns(:api_client)
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