3782: code review feedback
[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
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     within '.modal-content' do
117       find 'label', text: 'Virtual Machine'
118       fill_in "repo_name", :with => "test_repo"
119       click_button "Submit"
120     end
121
122     assert page.has_text? 'modified_by_client_uuid'
123
124     click_link 'Advanced'
125     click_link 'Metadata'
126     assert page.has_text? 'Repository: test_repo'
127     assert !(page.has_text? 'VirtualMachine:')
128
129     # Click on Setup button again and this time also choose a VM
130     click_link 'Admin'
131     click_link 'Setup Active User'
132
133     within '.modal-content' do
134       fill_in "repo_name", :with => "second_test_repo"
135       select("testvm.shell", :from => 'vm_uuid')
136       click_button "Submit"
137     end
138
139     find '#Attributes', text: 'modified_by_client_uuid'
140
141     click_link 'Advanced'
142     click_link 'Metadata'
143     assert page.has_text? 'Repository: second_test_repo'
144     assert page.has_text? 'VirtualMachine: testvm.shell'
145
146     headless.stop
147   end
148
149   test "unsetup active user" do
150     headless = Headless.new
151     headless.start
152
153     Capybara.current_driver = :selenium
154
155     visit page_with_token('admin_trustedclient')
156
157     find('#system-menu').click
158     click_link 'Users'
159
160     # click on active user
161     find('tr', text: 'zzzzz-tpzed-xurymjxw79nv3jz').
162       find('a', text: 'Show').
163       click
164
165     # Verify that is_active is set
166     find('a,button', text: 'Attributes').click
167     assert page.has_text? 'modified_by_user_uuid'
168     page.within(:xpath, '//span[@data-name="is_active"]') do
169       assert_equal "true", text, "Expected user's is_active to be true"
170     end
171
172     # go to Admin tab
173     click_link 'Admin'
174     assert page.has_text? 'As an admin, you can deactivate and reset this user'
175
176     # unsetup user and verify all the above links are deleted
177     click_link 'Admin'
178     click_button 'Deactivate Active User'
179     sleep(0.1)
180
181     # Should now be back in the Attributes tab for the user
182     page.driver.browser.switch_to.alert.accept
183
184     assert page.has_text? 'modified_by_user_uuid'
185     page.within(:xpath, '//span[@data-name="is_active"]') do
186       assert_equal "false", text, "Expected user's is_active to be false after unsetup"
187     end
188
189     click_link 'Advanced'
190     click_link 'Metadata'
191     assert !(page.has_text? 'Repository: test_repo')
192     assert !(page.has_text? 'Repository: second_test_repo')
193     assert !(page.has_text? 'VirtualMachine: testvm.shell')
194
195     # setup user again and verify links present
196     click_link 'Admin'
197     click_link 'Setup Active User'
198
199     within '.modal-content' do
200       fill_in "repo_name", :with => "second_test_repo"
201       select("testvm.shell", :from => 'vm_uuid')
202       click_button "Submit"
203     end
204
205     assert page.has_text? 'modified_by_client_uuid'
206
207     click_link 'Advanced'
208     click_link 'Metadata'
209     assert page.has_text? 'Repository: second_test_repo'
210     assert page.has_text? 'VirtualMachine: testvm.shell'
211
212     headless.stop
213   end
214
215 end