Merge branch '2640-folder-api' into 1970-folder-view
[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     end
67
68     sleep 0.1
69
70     # verify that the new user showed up in the users page and find
71     # the new user's UUID
72     new_user_uuid =
73       find('tr[data-object-uuid]', text: 'foo@example.com').
74       find('td', text: '-tpzed-').
75       text
76     assert new_user_uuid, "Expected new user uuid not found"
77
78     # go to the new user's page
79     find('tr', text: new_user_uuid).
80       find('a,button', text: 'Show').
81       click
82
83     assert page.has_text? 'modified_by_user_uuid'
84     page.within(:xpath, '//span[@data-name="is_active"]') do
85       assert_equal "false", text, "Expected new user's is_active to be false"
86     end
87
88     click_link 'Metadata'
89     assert page.has_text? '(Repository: test_repo)'
90     assert !(page.has_text? '(VirtualMachine:)')
91
92     headless.stop
93   end
94
95   test "setup the active user" do
96     headless = Headless.new
97     headless.start
98
99     Capybara.current_driver = :selenium
100     visit page_with_token('admin_trustedclient')
101
102     click_link 'Users'
103
104     # click on active user
105     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
106       find('a,button', text: 'Show').
107       click
108
109     # Setup user
110     click_link 'Admin'
111     assert page.has_text? 'As an admin, you can setup'
112
113     click_link 'Setup Active User'
114
115     sleep(0.1)
116
117     popup = page.driver.browser.window_handles.last
118     page.within_window popup do
119       assert has_text? 'Virtual Machine'
120       fill_in "repo_name", :with => "test_repo"
121       click_button "Submit"
122     end
123
124     sleep(1)
125     assert page.has_text? 'modified_by_client_uuid'
126
127     click_link 'Metadata'
128     assert page.has_text? '(Repository: test_repo)'
129     assert !(page.has_text? '(VirtualMachine:)')
130
131     # Click on Setup button again and this time also choose a VM
132     click_link 'Admin'
133     click_link 'Setup Active User'
134
135     sleep(0.1)
136     popup = page.driver.browser.window_handles.last
137     page.within_window popup do
138       fill_in "repo_name", :with => "second_test_repo"
139       select("testvm.shell", :from => 'vm_uuid')
140       click_button "Submit"
141     end
142
143     sleep(0.1)
144     assert page.has_text? 'modified_by_client_uuid'
145
146     click_link 'Metadata'
147     assert page.has_text? '(Repository: second_test_repo)'
148     assert page.has_text? '(VirtualMachine: testvm.shell)'
149
150     headless.stop
151   end
152
153   test "unsetup active user" do
154     headless = Headless.new
155     headless.start
156
157     Capybara.current_driver = :selenium
158
159     visit page_with_token('admin_trustedclient')
160
161     click_link 'Users'
162
163     # click on active user
164     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
165       find('a,button', text: 'Show').
166       click
167
168     # Verify that is_active is set
169     click_link 'Attributes'
170     assert page.has_text? 'modified_by_user_uuid'
171     page.within(:xpath, '//span[@data-name="is_active"]') do
172       assert_equal "true", text, "Expected user's is_active to be true"
173     end
174
175     # go to Admin tab
176     click_link 'Admin'
177     assert page.has_text? 'As an admin, you can deactivate and reset this user'
178
179     # unsetup user and verify all the above links are deleted
180     click_link 'Admin'
181     click_button 'Deactivate Active User'
182     sleep(0.1)
183
184     # Should now be back in the Attributes tab for the user
185     page.driver.browser.switch_to.alert.accept
186     assert page.has_text? 'modified_by_user_uuid'
187     page.within(:xpath, '//span[@data-name="is_active"]') do
188       assert_equal "false", text, "Expected user's is_active to be false after unsetup"
189     end
190
191     click_link 'Metadata'
192     assert !(page.has_text? '(Repository: test_repo)')
193     assert !(page.has_text? '(Repository: second_test_repo)')
194     assert !(page.has_text? '(VirtualMachine: testvm.shell)')
195
196     # setup user again and verify links present
197     click_link 'Admin'
198     click_link 'Setup Active User'
199
200     sleep(0.1)
201     popup = page.driver.browser.window_handles.last
202     page.within_window popup do
203       fill_in "repo_name", :with => "second_test_repo"
204       select("testvm.shell", :from => 'vm_uuid')
205       click_button "Submit"
206     end
207
208     sleep(0.1)
209     assert page.has_text? 'modified_by_client_uuid'
210
211     click_link 'Metadata'
212     assert page.has_text? '(Repository: second_test_repo)'
213     assert page.has_text? '(VirtualMachine: testvm.shell)'
214
215     headless.stop
216   end
217
218 end