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