15964: Remove qr1hi from a few more places. Delete unused includes.
[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
18   test "send token when user is already logged in" do
19     authorize_with :inactive
20     api_client_page = 'http://client.example.com/home'
21     get :login, params: {return_to: api_client_page}
22     assert_response :redirect
23     assert_equal(0, @response.redirect_url.index(api_client_page + '?'),
24                  'Redirect url ' + @response.redirect_url +
25                  ' should start with ' + api_client_page + '?')
26     assert_not_nil assigns(:api_client)
27   end
28
29   test "login with remote param returns a salted token" do
30     authorize_with :inactive
31     api_client_page = 'http://client.example.com/home'
32     remote_prefix = 'zbbbb'
33     get :login, params: {return_to: api_client_page, remote: remote_prefix}
34     assert_response :redirect
35     api_client_auth = assigns(:api_client_auth)
36     assert_not_nil api_client_auth
37     assert_includes(@response.redirect_url, 'api_token='+api_client_auth.salted_token(remote: remote_prefix))
38   end
39
40   test "login with malformed remote param returns an error" do
41     authorize_with :inactive
42     api_client_page = 'http://client.example.com/home'
43     remote_prefix = 'invalid_cluster_id'
44     get :login, params: {return_to: api_client_page, remote: remote_prefix}
45     assert_response 400
46   end
47
48   test "login to LoginCluster" do
49     Rails.configuration.Login.LoginCluster = 'zbbbb'
50     Rails.configuration.RemoteClusters['zbbbb'] = {'Host' => 'zbbbb.example.com'}
51     api_client_page = 'http://client.example.com/home'
52     get :login, params: {return_to: api_client_page}
53     assert_response :redirect
54     assert_equal("https://zbbbb.example.com/login?return_to=http%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
55     assert_nil assigns(:api_client)
56   end
57
58   test "don't go into redirect loop if LoginCluster is self" do
59     Rails.configuration.Login.LoginCluster = 'zzzzz'
60     api_client_page = 'http://client.example.com/home'
61     get :login, params: {return_to: api_client_page}
62     assert_response :redirect
63     assert_equal("http://test.host/auth/joshid?return_to=%2Chttp%3A%2F%2Fclient.example.com%2Fhome", @response.redirect_url)
64     assert_nil assigns(:api_client)
65   end
66
67   test "controller cannot create session without SystemRootToken" do
68     get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
69     assert_response 401
70   end
71
72   test "controller cannot create session with wrong SystemRootToken" do
73     @request.headers['Authorization'] = 'Bearer blah'
74     get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
75     assert_response 401
76   end
77
78   test "controller can create session using SystemRootToken" do
79     @request.headers['Authorization'] = 'Bearer '+Rails.configuration.SystemRootToken
80     get :create, params: {provider: 'controller', auth_info: {email: "foo@bar.com"}, return_to: ',https://app.example'}
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.token)
85   end
86 end