Merge branch 'master' into 16811-public-favs
[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   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))
59   end
60
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}
66     assert_response 400
67   end
68
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)
77   end
78
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)
86   end
87
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'}
90     assert_response 401
91   end
92
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'}
96     assert_response 401
97   end
98
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)
106   end
107 end