Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / test / integration / users_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'integration_helper'
6
7 class UsersTest < ActionDispatch::IntegrationTest
8
9   test "login as active user but not admin" do
10     need_javascript
11     visit page_with_token('active_trustedclient')
12
13     assert page.has_no_link? 'Users' 'Found Users link for non-admin user'
14   end
15
16   test "login as admin user and verify active user data" do
17     need_javascript
18     visit page_with_token('admin_trustedclient')
19
20     # go to Users list page
21     find('#system-menu').click
22     click_link 'Users'
23
24     # check active user attributes in the list page
25     page.within(:xpath, '//tr[@data-object-uuid="zzzzz-tpzed-xurymjxw79nv3jz"]') do
26       assert (text.include? 'true false'), 'Expected is_active'
27     end
28
29     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
30       find('a', text: 'Show').
31       click
32     assert page.has_text? 'Attributes'
33     assert page.has_text? 'Advanced'
34     assert page.has_text? 'Admin'
35
36     # go to the Attributes tab
37     click_link 'Attributes'
38     assert page.has_text? 'modified_by_user_uuid'
39     page.within(:xpath, '//span[@data-name="is_active"]') do
40       assert_equal "true", text, "Expected user's is_active to be true"
41     end
42     page.within(:xpath, '//span[@data-name="is_admin"]') do
43       assert_equal "false", text, "Expected user's is_admin to be false"
44     end
45
46   end
47
48   test "create a new user" do
49     need_javascript
50
51     visit page_with_token('admin_trustedclient')
52
53     find('#system-menu').click
54     click_link 'Users'
55
56     assert page.has_text? 'zzzzz-tpzed-d9tiejq69daie8f'
57
58     click_link 'Add a new user'
59
60     within '.modal-content' do
61       find 'label', text: 'Virtual Machine'
62       fill_in "email", :with => "foo@example.com"
63       click_button "Submit"
64       wait_for_ajax
65     end
66
67     visit '/users'
68
69     # verify that the new user showed up in the users page and find
70     # the new user's UUID
71     new_user_uuid =
72       find('tr[data-object-uuid]', text: 'foo@example.com')['data-object-uuid']
73     assert new_user_uuid, "Expected new user uuid not found"
74
75     # go to the new user's page
76     find('tr', text: new_user_uuid).
77       find('a', text: 'Show').
78       click
79
80     assert page.has_text? 'modified_by_user_uuid'
81     page.within(:xpath, '//span[@data-name="is_active"]') do
82       assert_equal "false", text, "Expected new user's is_active to be false"
83     end
84
85     click_link 'Advanced'
86     click_link 'Metadata'
87     assert page.has_text? 'can_login' # make sure page is rendered / ready
88     assert page.has_no_text? 'VirtualMachine:'
89   end
90
91   test "setup the active user" do
92     need_javascript
93     visit page_with_token('admin_trustedclient')
94
95     find('#system-menu').click
96     click_link 'Users'
97
98     # click on active user
99     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
100       find('a', text: 'Show').
101       click
102     user_url = page.current_url
103
104     # Setup user
105     click_link 'Admin'
106     assert page.has_text? 'As an admin, you can setup'
107
108     click_link 'Setup shell account for Active User'
109
110     within '.modal-content' do
111       find 'label', text: 'Virtual Machine'
112       click_button "Submit"
113     end
114
115     visit user_url
116     assert page.has_text? 'modified_by_client_uuid'
117
118     click_link 'Advanced'
119     click_link 'Metadata'
120     vm_links = all("a", text: "VirtualMachine:")
121     assert_equal(1, vm_links.size)
122     assert_equal("VirtualMachine: testvm2.shell", vm_links.first.text)
123
124     # Click on Setup button again and this time also choose a VM
125     click_link 'Admin'
126     click_link 'Setup shell account for Active User'
127
128     within '.modal-content' do
129       select("testvm.shell", :from => 'vm_uuid')
130       fill_in "groups", :with => "test group one, test-group-two"
131       click_button "Submit"
132     end
133
134     visit user_url
135     find '#Attributes', text: 'modified_by_client_uuid'
136
137     click_link 'Advanced'
138     click_link 'Metadata'
139     assert page.has_text? 'VirtualMachine: testvm.shell'
140     assert page.has_text? '["test group one", "test-group-two"]'
141   end
142
143   test "unsetup active user" do
144     need_javascript
145
146     visit page_with_token('admin_trustedclient')
147
148     find('#system-menu').click
149     click_link 'Users'
150
151     # click on active user
152     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
153       find('a', text: 'Show').
154       click
155     user_url = page.current_url
156
157     # Verify that is_active is set
158     find('a,button', text: 'Attributes').click
159     assert page.has_text? 'modified_by_user_uuid'
160     page.within(:xpath, '//span[@data-name="is_active"]') do
161       assert_equal "true", text, "Expected user's is_active to be true"
162     end
163
164     # go to Admin tab
165     click_link 'Admin'
166     assert page.has_text? 'As an admin, you can deactivate and reset this user'
167
168     # unsetup user and verify all the above links are deleted
169     click_link 'Admin'
170     click_button 'Deactivate Active User'
171
172     if Capybara.current_driver == :selenium
173       sleep(0.1)
174       page.driver.browser.switch_to.alert.accept
175     else
176       # poltergeist returns true for confirm(), so we don't need to accept.
177     end
178
179     # Should now be back in the Attributes tab for the user
180     assert page.has_text? 'modified_by_user_uuid'
181     page.within(:xpath, '//span[@data-name="is_active"]') do
182       assert_equal "false", text, "Expected user's is_active to be false after unsetup"
183     end
184
185     click_link 'Advanced'
186     click_link 'Metadata'
187     assert page.has_no_text? 'VirtualMachine: testvm.shell'
188
189     # setup user again and verify links present
190     click_link 'Admin'
191     click_link 'Setup shell account for Active User'
192
193     within '.modal-content' do
194       select("testvm.shell", :from => 'vm_uuid')
195       click_button "Submit"
196     end
197
198     visit user_url
199     assert page.has_text? 'modified_by_client_uuid'
200
201     click_link 'Advanced'
202     click_link 'Metadata'
203     assert page.has_text? 'VirtualMachine: testvm.shell'
204   end
205
206   test "test add group button" do
207     need_javascript
208
209     user_url = "/users/#{api_fixture('users')['active']['uuid']}"
210     visit page_with_token('admin_trustedclient', user_url)
211
212     # Setup user
213     click_link 'Admin'
214     assert page.has_text? 'As an admin, you can setup'
215
216     click_link 'Add new group'
217
218     within '.modal-content' do
219       fill_in "group_name_input", :with => "test-group-added-in-modal"
220       click_button "Create"
221     end
222     wait_for_ajax
223
224     # Back in the user "Admin" tab
225     assert page.has_text? 'test-group-added-in-modal'
226   end
227 end