Merge branch '3198-writable-fuse' closes #3198
[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       fill_in "repo_name", :with => "newtestrepo"
60       click_button "Submit"
61       wait_for_ajax
62     end
63
64     visit '/users'
65
66     # verify that the new user showed up in the users page and find
67     # the new user's UUID
68     new_user_uuid =
69       find('tr[data-object-uuid]', text: 'foo@example.com')['data-object-uuid']
70     assert new_user_uuid, "Expected new user uuid not found"
71
72     # go to the new user's page
73     find('tr', text: new_user_uuid).
74       find('a', text: 'Show').
75       click
76
77     assert page.has_text? 'modified_by_user_uuid'
78     page.within(:xpath, '//span[@data-name="is_active"]') do
79       assert_equal "false", text, "Expected new user's is_active to be false"
80     end
81
82     click_link 'Advanced'
83     click_link 'Metadata'
84     assert page.has_text? 'Repository: foo/newtestrepo'
85     assert !(page.has_text? 'VirtualMachine:')
86   end
87
88   test "setup the active user" do
89     need_javascript
90     visit page_with_token('admin_trustedclient')
91
92     find('#system-menu').click
93     click_link 'Users'
94
95     # click on active user
96     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
97       find('a', text: 'Show').
98       click
99     user_url = page.current_url
100
101     # Setup user
102     click_link 'Admin'
103     assert page.has_text? 'As an admin, you can setup'
104
105     click_link 'Setup Active User'
106
107     within '.modal-content' do
108       find 'label', text: 'Virtual Machine'
109       fill_in "repo_name", :with => "activetestrepo"
110       click_button "Submit"
111     end
112
113     visit user_url
114     assert page.has_text? 'modified_by_client_uuid'
115
116     click_link 'Advanced'
117     click_link 'Metadata'
118     assert page.has_text? 'Repository: active/activetestrepo'
119     vm_links = all("a", text: "VirtualMachine:")
120     assert_equal(1, vm_links.size)
121     assert_equal("VirtualMachine: testvm2.shell", vm_links.first.text)
122
123     # Click on Setup button again and this time also choose a VM
124     click_link 'Admin'
125     click_link 'Setup Active User'
126
127     within '.modal-content' do
128       fill_in "repo_name", :with => "activetestrepo2"
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? 'Repository: active/activetestrepo2'
140     assert page.has_text? 'VirtualMachine: testvm.shell'
141     assert page.has_text? '["test group one", "test-group-two"]'
142   end
143
144   test "unsetup active user" do
145     need_javascript
146
147     visit page_with_token('admin_trustedclient')
148
149     find('#system-menu').click
150     click_link 'Users'
151
152     # click on active user
153     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
154       find('a', text: 'Show').
155       click
156     user_url = page.current_url
157
158     # Verify that is_active is set
159     find('a,button', text: 'Attributes').click
160     assert page.has_text? 'modified_by_user_uuid'
161     page.within(:xpath, '//span[@data-name="is_active"]') do
162       assert_equal "true", text, "Expected user's is_active to be true"
163     end
164
165     # go to Admin tab
166     click_link 'Admin'
167     assert page.has_text? 'As an admin, you can deactivate and reset this user'
168
169     # unsetup user and verify all the above links are deleted
170     click_link 'Admin'
171     click_button 'Deactivate Active User'
172
173     if Capybara.current_driver == :selenium
174       sleep(0.1)
175       page.driver.browser.switch_to.alert.accept
176     else
177       # poltergeist returns true for confirm(), so we don't need to accept.
178     end
179
180     # Should now be back in the Attributes tab for the user
181     assert page.has_text? 'modified_by_user_uuid'
182     page.within(:xpath, '//span[@data-name="is_active"]') do
183       assert_equal "false", text, "Expected user's is_active to be false after unsetup"
184     end
185
186     click_link 'Advanced'
187     click_link 'Metadata'
188     assert page.has_no_text? 'Repository: active/'
189     assert page.has_no_text? 'VirtualMachine: testvm.shell'
190
191     # setup user again and verify links present
192     click_link 'Admin'
193     click_link 'Setup Active User'
194
195     within '.modal-content' do
196       fill_in "repo_name", :with => "activetestrepo"
197       select("testvm.shell", :from => 'vm_uuid')
198       click_button "Submit"
199     end
200
201     visit user_url
202     assert page.has_text? 'modified_by_client_uuid'
203
204     click_link 'Advanced'
205     click_link 'Metadata'
206     assert page.has_text? 'Repository: active/activetestrepo'
207     assert page.has_text? 'VirtualMachine: testvm.shell'
208   end
209
210   [
211     ['admin', false],
212     ['active', true],
213   ].each do |username, expect_show_button|
214     test "login as #{username} and access show button #{expect_show_button}" do
215       need_javascript
216
217       user = api_fixture('users', username)
218
219       visit page_with_token(username, '/users')
220
221       if expect_show_button
222         within('tr', text: user['uuid']) do
223           assert_text user['email']
224           assert_selector 'a', text: 'Show'
225           find('a', text: 'Show').click
226         end
227         assert_selector 'a', 'Data collections'
228       else
229         # no 'Show' button in the admin user's own row
230         within('tr', text: user['uuid']) do
231           assert_text user['email']
232           assert_no_selector 'a', text: 'Show'
233         end
234
235         # but the admin user can access 'Show' button for other users
236         active_user = api_fixture('users', 'active')
237         within('tr', text: active_user['uuid']) do
238           assert_text active_user['email']
239           assert_selector 'a', text: 'Show'
240           find('a', text: 'Show').click
241           assert_selector 'a', 'Attributes'
242         end
243       end
244     end
245   end
246 end