Merge branch '15877-accept-json-in-json'
[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     click_link 'Attributes'
81
82     assert page.has_text? 'modified_by_user_uuid'
83     page.within(:xpath, '//span[@data-name="is_active"]') do
84       assert_equal "false", text, "Expected new user's is_active to be false"
85     end
86
87     click_link 'Advanced'
88     click_link 'Metadata'
89     assert page.has_text? 'can_read' # make sure page is rendered / ready
90     assert page.has_no_text? 'VirtualMachine:'
91   end
92
93   test "setup the active user" do
94     need_javascript
95     visit page_with_token('admin_trustedclient')
96
97     find('#system-menu').click
98     click_link 'Users'
99
100     # click on active user
101     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
102       find('a', text: 'Show').
103       click
104     user_url = page.current_url
105
106     # Setup user
107     click_link 'Admin'
108     assert page.has_text? 'This button sets up a user'
109
110     click_link 'Setup account for Active User'
111
112     within '.modal-content' do
113       find 'label', text: 'Virtual Machine'
114       click_button "Submit"
115     end
116
117     visit user_url
118     click_link 'Attributes'
119     assert page.has_text? 'modified_by_client_uuid'
120
121     click_link 'Advanced'
122     click_link 'Metadata'
123     vm_links = all("a", text: "VirtualMachine:")
124     assert_equal(1, vm_links.size)
125     assert_equal("VirtualMachine: testvm2.shell", vm_links.first.text)
126
127     # Click on Setup button again and this time also choose a VM
128     click_link 'Admin'
129     click_link 'Setup account for Active User'
130
131     within '.modal-content' do
132       select("testvm.shell", :from => 'vm_uuid')
133       fill_in "groups", :with => "test group one, test-group-two"
134       click_button "Submit"
135     end
136
137     visit user_url
138     click_link 'Attributes'
139     find '#Attributes', text: 'modified_by_client_uuid'
140
141     click_link 'Advanced'
142     click_link 'Metadata'
143     assert page.has_text? 'VirtualMachine: testvm.shell'
144     assert page.has_text? '["test group one", "test-group-two"]'
145     vm_links = all("a", text: "VirtualMachine:")
146     assert_equal(2, vm_links.size)
147   end
148
149   test "unsetup active user" do
150     need_javascript
151
152     visit page_with_token('admin_trustedclient')
153
154     find('#system-menu').click
155     click_link 'Users'
156
157     # click on active user
158     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
159       find('a', text: 'Show').
160       click
161     user_url = page.current_url
162
163     # Verify that is_active is set
164     click_link 'Attributes'
165     assert page.has_text? 'modified_by_user_uuid'
166     page.within(:xpath, '//span[@data-name="is_active"]') do
167       assert_equal "true", text, "Expected user's is_active to be true"
168     end
169
170     # go to Admin tab
171     click_link 'Admin'
172     assert page.has_text? 'As an admin, you can deactivate and reset this user'
173
174     # unsetup user and verify all the above links are deleted
175     click_link 'Admin'
176     click_button 'Deactivate Active User'
177
178     if Capybara.current_driver == :selenium
179       sleep(0.1)
180       page.driver.browser.switch_to.alert.accept
181     else
182       # poltergeist returns true for confirm(), so we don't need to accept.
183     end
184
185     click_link 'Attributes'
186
187     # Should now be back in the Attributes tab for the user
188     assert page.has_text? 'modified_by_user_uuid'
189     page.within(:xpath, '//span[@data-name="is_active"]') do
190       assert_equal "false", text, "Expected user's is_active to be false after unsetup"
191     end
192
193     click_link 'Advanced'
194     click_link 'Metadata'
195     assert page.has_no_text? 'VirtualMachine: testvm.shell'
196
197     # setup user again and verify links present
198     click_link 'Admin'
199     click_link 'Setup account for Active User'
200
201     within '.modal-content' do
202       select("testvm.shell", :from => 'vm_uuid')
203       click_button "Submit"
204     end
205
206     visit user_url
207     click_link 'Attributes'
208     assert page.has_text? 'modified_by_client_uuid'
209
210     click_link 'Advanced'
211     click_link 'Metadata'
212     assert page.has_text? 'VirtualMachine: testvm.shell'
213   end
214
215   test "test add group button" do
216     need_javascript
217
218     user_url = "/users/#{api_fixture('users')['active']['uuid']}"
219     visit page_with_token('admin_trustedclient', user_url)
220
221     # Setup user
222     click_link 'Admin'
223     assert page.has_text? 'This button sets up a user'
224
225     click_link 'Add new group'
226
227     within '.modal-content' do
228       fill_in "group_name_input", :with => "test-group-added-in-modal"
229       click_button "Create"
230     end
231     wait_for_ajax
232
233     # Back in the user "Admin" tab
234     assert page.has_text? 'test-group-added-in-modal'
235   end
236 end