Merge branch 'master' into 3618-column-ordering
[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     within '.modal-content' do
62       find 'label', 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')['data-object-uuid']
75     assert new_user_uuid, "Expected new user uuid not found"
76
77     # go to the new user's page
78     find('tr', text: new_user_uuid).
79       find('a', text: 'Show').
80       click
81
82     assert page.has_text? 'modified_by_user_uuid'
83     page.within(:xpath, '//span[@data-name="is_active"]') do
84       assert_equal "false", text, "Expected new user's is_active to be false"
85     end
86
87     click_link 'Advanced'
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     find('#system-menu').click
103     click_link 'Users'
104
105     # click on active user
106     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
107       find('a', text: 'Show').
108       click
109     user_url = page.current_url
110
111     # Setup user
112     click_link 'Admin'
113     assert page.has_text? 'As an admin, you can setup'
114
115     click_link 'Setup Active User'
116
117     within '.modal-content' do
118       find 'label', text: 'Virtual Machine'
119       fill_in "repo_name", :with => "test_repo"
120       click_button "Submit"
121     end
122
123     visit user_url
124     assert page.has_text? 'modified_by_client_uuid'
125
126     click_link 'Advanced'
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     within '.modal-content' do
136       fill_in "repo_name", :with => "second_test_repo"
137       select("testvm.shell", :from => 'vm_uuid')
138       click_button "Submit"
139     end
140
141     visit user_url
142     find '#Attributes', text: 'modified_by_client_uuid'
143
144     click_link 'Advanced'
145     click_link 'Metadata'
146     assert page.has_text? 'Repository: second_test_repo'
147     assert page.has_text? 'VirtualMachine: testvm.shell'
148
149     headless.stop
150   end
151
152   test "unsetup active user" do
153     headless = Headless.new
154     headless.start
155
156     Capybara.current_driver = :selenium
157
158     visit page_with_token('admin_trustedclient')
159
160     find('#system-menu').click
161     click_link 'Users'
162
163     # click on active user
164     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
165       find('a', text: 'Show').
166       click
167     user_url = page.current_url
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
188     assert page.has_text? 'modified_by_user_uuid'
189     page.within(:xpath, '//span[@data-name="is_active"]') do
190       assert_equal "false", text, "Expected user's is_active to be false after unsetup"
191     end
192
193     click_link 'Advanced'
194     click_link 'Metadata'
195     assert !(page.has_text? 'Repository: test_repo')
196     assert !(page.has_text? 'Repository: second_test_repo')
197     assert !(page.has_text? 'VirtualMachine: testvm.shell')
198
199     # setup user again and verify links present
200     click_link 'Admin'
201     click_link 'Setup Active User'
202
203     within '.modal-content' do
204       fill_in "repo_name", :with => "second_test_repo"
205       select("testvm.shell", :from => 'vm_uuid')
206       click_button "Submit"
207     end
208
209     visit user_url
210     assert page.has_text? 'modified_by_client_uuid'
211
212     click_link 'Advanced'
213     click_link 'Metadata'
214     assert page.has_text? 'Repository: second_test_repo'
215     assert page.has_text? 'VirtualMachine: testvm.shell'
216
217     headless.stop
218   end
219
220 end