Merge branch '11158-fuse-projects' refs #11158
[arvados.git] / apps / workbench / test / controllers / users_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 UsersControllerTest < ActionController::TestCase
8
9   test "valid token works in controller test" do
10     get :index, {}, session_for(:active)
11     assert_response :success
12   end
13
14   test "ignore previously valid token (for deleted user), don't crash" do
15     get :activity, {}, session_for(:valid_token_deleted_user)
16     assert_response :redirect
17     assert_match /^#{Rails.configuration.arvados_login_base}/, @response.redirect_url
18     assert_nil assigns(:my_jobs)
19     assert_nil assigns(:my_ssh_keys)
20   end
21
22   test "expired token redirects to api server login" do
23     get :show, {
24       id: api_fixture('users')['active']['uuid']
25     }, session_for(:expired_trustedclient)
26     assert_response :redirect
27     assert_match /^#{Rails.configuration.arvados_login_base}/, @response.redirect_url
28     assert_nil assigns(:my_jobs)
29     assert_nil assigns(:my_ssh_keys)
30   end
31
32   test "show welcome page if no token provided" do
33     get :index, {}
34     assert_response :redirect
35     assert_match /\/users\/welcome/, @response.redirect_url
36   end
37
38   test "request shell access" do
39     user = api_fixture('users')['spectator']
40
41     ActionMailer::Base.deliveries = []
42
43     post :request_shell_access, {
44       id: user['uuid'],
45       format: 'js'
46     }, session_for(:spectator)
47     assert_response :success
48
49     full_name = "#{user['first_name']} #{user['last_name']}"
50     expected = "Shell account request from #{full_name} (#{user['email']}, #{user['uuid']})"
51     found_email = 0
52     ActionMailer::Base.deliveries.each do |email|
53       if email.subject.include?(expected)
54         found_email += 1
55         break
56       end
57     end
58     assert_equal 1, found_email, "Expected 1 email after requesting shell access"
59   end
60
61   [
62     'admin',
63     'active',
64   ].each do |username|
65     test "access users page as #{username} and verify show button is available" do
66       admin_user = api_fixture('users','admin')
67       active_user = api_fixture('users','active')
68       get :index, {}, session_for(username)
69       if username == 'admin'
70         assert_match /<a href="\/projects\/#{admin_user['uuid']}">Home<\/a>/, @response.body
71         assert_match /<a href="\/projects\/#{active_user['uuid']}">Home<\/a>/, @response.body
72         assert_match /href="\/users\/#{admin_user['uuid']}" title="show user"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
73         assert_match /href="\/users\/#{active_user['uuid']}" title="show user"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
74         assert_includes @response.body, admin_user['email']
75         assert_includes @response.body, active_user['email']
76       else
77         refute_match  /Home<\/a>/, @response.body
78         refute_match /href="\/users\/#{admin_user['uuid']}" title="show user"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
79         assert_match /href="\/users\/#{active_user['uuid']}" title="show user"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
80         assert_includes @response.body, active_user['email']
81       end
82     end
83   end
84
85   [
86     'admin',
87     'active',
88   ].each do |username|
89     test "access settings drop down menu as #{username}" do
90       admin_user = api_fixture('users','admin')
91       active_user = api_fixture('users','active')
92       get :show, {
93         id: api_fixture('users')[username]['uuid']
94       }, session_for(username)
95       if username == 'admin'
96         assert_includes @response.body, admin_user['email']
97         refute_empty css_select('[id="system-menu"]')
98       else
99         assert_includes @response.body, active_user['email']
100         assert_empty css_select('[id="system-menu"]')
101       end
102     end
103   end
104 end