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