20850: Fix test selector
[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, params: {}, session: 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, params: {}, session: session_for(:valid_token_deleted_user)
16     assert_response :redirect
17     assert_match /^#{Rails.configuration.Services.Workbench1.ExternalURL}users\/welcome/, @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     assert Rails.configuration.Login.Test.Enable
24     get :show, params: {
25       id: api_fixture('users')['active']['uuid']
26     }, session: session_for(:expired_trustedclient)
27     assert_response :redirect
28     assert_match /^#{Rails.configuration.Services.Workbench1.ExternalURL}users\/welcome/, @response.redirect_url
29     assert_nil assigns(:my_jobs)
30     assert_nil assigns(:my_ssh_keys)
31   end
32
33   test "show welcome page if no token provided" do
34     get :index, params: {}
35     assert_response :redirect
36     assert_match /\/users\/welcome/, @response.redirect_url
37   end
38
39   test "'log in as user' feature uses a v2 token" do
40     post :sudo, params: {
41       id: api_fixture('users')['active']['uuid']
42     }, session: session_for('admin_trustedclient')
43     assert_response :redirect
44     assert_match /api_token=v2%2F/, @response.redirect_url
45   end
46
47   test "request shell access" do
48     user = api_fixture('users')['spectator']
49
50     ActionMailer::Base.deliveries = []
51
52     post :request_shell_access, params: {
53       id: user['uuid'],
54       format: 'js'
55     }, session: session_for(:spectator)
56     assert_response :success
57
58     full_name = "#{user['first_name']} #{user['last_name']}"
59     expected = "Shell account request from #{full_name} (#{user['email']}, #{user['uuid']})"
60     found_email = 0
61     ActionMailer::Base.deliveries.each do |email|
62       if email.subject.include?(expected)
63         found_email += 1
64         break
65       end
66     end
67     assert_equal 1, found_email, "Expected 1 email after requesting shell access"
68   end
69
70   [
71     'admin',
72     'active',
73   ].each do |username|
74     test "access users page as #{username} and verify show button is available" do
75       admin_user = api_fixture('users','admin')
76       active_user = api_fixture('users','active')
77       get :index, params: {}, session: session_for(username)
78       if username == 'admin'
79         assert_match /<a href="\/projects\/#{admin_user['uuid']}">Home<\/a>/, @response.body
80         assert_match /<a href="\/projects\/#{active_user['uuid']}">Home<\/a>/, @response.body
81         assert_match /href="\/users\/#{admin_user['uuid']}"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
82         assert_match /href="\/users\/#{active_user['uuid']}"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
83         assert_includes @response.body, admin_user['email']
84         assert_includes @response.body, active_user['email']
85       else
86         refute_match  /Home<\/a>/, @response.body
87         refute_match /href="\/users\/#{admin_user['uuid']}"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
88         assert_match /href="\/users\/#{active_user['uuid']}"><i class="fa fa-fw fa-user"><\/i> Show<\/a/, @response.body
89         assert_includes @response.body, active_user['email']
90       end
91     end
92   end
93
94   [
95     'admin',
96     'active',
97   ].each do |username|
98     test "access settings drop down menu as #{username}" do
99       admin_user = api_fixture('users','admin')
100       active_user = api_fixture('users','active')
101       get :show, params: {
102         id: api_fixture('users')[username]['uuid']
103       }, session: session_for(username)
104       if username == 'admin'
105         assert_includes @response.body, admin_user['email']
106         refute_empty css_select('[id="system-menu"]')
107       else
108         assert_includes @response.body, active_user['email']
109         assert_empty css_select('[id="system-menu"]')
110       end
111     end
112   end
113 end