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