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