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