Merge branch 'master' into 3112-report-bug
[arvados.git] / apps / workbench / test / integration / application_layout_test.rb
1 require 'integration_helper'
2 require 'selenium-webdriver'
3 require 'headless'
4
5 class ApplicationLayoutTest < ActionDispatch::IntegrationTest
6   setup do
7     headless = Headless.new
8     headless.start
9     Capybara.current_driver = :selenium
10
11     @user_profile_form_fields = Rails.configuration.user_profile_form_fields
12   end
13
14   teardown do
15     Rails.configuration.user_profile_form_fields = @user_profile_form_fields
16   end
17
18   def verify_homepage_with_profile user, invited, has_profile
19     profile_config = Rails.configuration.user_profile_form_fields
20
21     if !user
22       assert page.has_text? 'Please log in'
23       assert page.has_text? 'The "Log in" button below will show you a Google sign-in page'
24       assert page.has_no_text? 'My projects'
25       assert page.has_link? "Log in to #{Rails.configuration.site_name}"
26     elsif profile_config && !has_profile && user['is_active']
27       add_profile user
28     elsif user['is_active']
29       assert page.has_text? 'My projects'
30       assert page.has_text? 'Projects shared with me'
31       assert page.has_no_text? 'Save profile'
32     elsif invited
33       assert page.has_text? 'Please check the box below to indicate that you have read and accepted the user agreement'
34       assert page.has_no_text? 'Save profile'
35     else
36       assert page.has_text? 'Your account is inactive'
37       assert page.has_no_text? 'Save profile'
38     end
39
40     within('.navbar-fixed-top') do
41       if !user
42         assert page.has_link? 'Log in'
43       else
44         # my account menu
45         assert page.has_link? "#{user['email']}"
46         find('a', text: "#{user['email']}").click
47         within('.dropdown-menu') do
48           if user['is_active']
49             assert page.has_no_link? ('Not active')
50             assert page.has_no_link? ('Sign agreements')
51
52             assert page.has_link? ('Manage account')
53
54             if profile_config
55               assert page.has_link? ('Manage profile')
56             else
57               assert page.has_no_link? ('Manage profile')
58             end
59           end
60           assert page.has_link? ('Log out')
61         end
62       end
63     end
64   end
65
66   # test the help menu
67   def check_help_menu
68     within('.navbar-fixed-top') do
69       page.find("#arv-help").click
70       within('.dropdown-menu') do
71         assert page.has_link? 'Tutorials and User guide'
72         assert page.has_link? 'API Reference'
73         assert page.has_link? 'SDK Reference'
74         assert page.has_link? 'Show version / debugging info'
75         assert page.has_link? 'Report a problem'
76
77         # check show version info link
78         click_link 'Show version / debugging info'
79       end
80     end
81
82     within '.modal-content' do
83       assert page.has_text? 'Version / debugging info'
84       assert page.has_no_text? 'Report a problem'
85       assert page.has_text? 'Server version'
86       assert page.has_text? 'Server restarted at'
87       assert page.has_text? 'Workbench version'
88       assert page.has_text? 'Arvados base'
89       assert page.has_text? 'Additional info'
90       assert page.has_no_text? 'Found a problem?'
91       assert page.has_button? 'Close'
92       assert page.has_no_button? 'Report issue'
93       click_button 'Close'
94     end
95
96     # check report issue link
97     within('.navbar-fixed-top') do
98       page.find("#arv-help").click
99       within('.dropdown-menu') do
100         click_link 'Report a problem'
101       end
102     end
103
104     within '.modal-content' do
105       assert page.has_text? 'Report a problem'
106       assert page.has_no_text? 'Version / debugging info'
107       assert page.has_text? 'Server version'
108       assert page.has_text? 'Server restarted at'
109       assert page.has_text? 'Workbench version'
110       assert page.has_text? 'Arvados base'
111       assert page.has_text? 'Additional info'
112       assert page.has_text? 'Found a problem?'
113       assert page.has_no_button? 'Close'
114       assert page.has_button? 'Report issue'
115       assert page.has_button? 'Cancel'
116
117       # enter a report text and click on report
118       page.find_field('report_issue_text').set 'my test report text'
119       click_button 'Report issue'
120     end
121
122     assert page.has_no_text? 'Version / debugging info'
123   end
124
125   def verify_system_menu user
126     if user && user['is_active']
127       look_for_add_new = nil
128       within('.navbar-fixed-top') do
129         page.find("#system-menu").click
130         if user['is_admin']
131           within('.dropdown-menu') do
132             assert page.has_text? 'Groups'
133             assert page.has_link? 'Repositories'
134             assert page.has_link? 'Virtual machines'
135             assert page.has_link? 'SSH keys'
136             assert page.has_link? 'API tokens'
137             find('a', text: 'Users').click
138             look_for_add_new = 'Add a new user'
139           end
140         else
141           within('.dropdown-menu') do
142             assert page.has_no_text? 'Users'
143             assert page.has_no_link? 'Repositories'
144             assert page.has_no_link? 'Virtual machines'
145             assert page.has_no_link? 'SSH keys'
146             assert page.has_no_link? 'API tokens'
147
148             find('a', text: 'Groups').click
149             look_for_add_new = 'Add a new group'
150           end
151         end
152       end
153       if look_for_add_new
154         assert page.has_text? look_for_add_new
155       end
156     else
157       assert page.has_no_link? '#system-menu'
158     end
159   end
160
161   # test manage_account page
162   def verify_manage_account user
163     if user && user['is_active']
164       within('.navbar-fixed-top') do
165         find('a', text: "#{user['email']}").click
166         within('.dropdown-menu') do
167           find('a', text: 'Manage account').click
168         end
169       end
170
171       # now in manage account page
172       assert page.has_text? 'Virtual Machines'
173       assert page.has_text? 'Repositories'
174       assert page.has_text? 'SSH Keys'
175       assert page.has_text? 'Current Token'
176
177       assert page.has_text? 'The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados'
178
179       click_link 'Add new SSH key'
180
181       within '.modal-content' do
182         assert page.has_text? 'Public Key'
183         assert page.has_button? 'Cancel'
184         assert page.has_button? 'Submit'
185
186         page.find_field('public_key').set 'first test with an incorrect ssh key value'
187         click_button 'Submit'
188         assert page.has_text? 'Public key does not appear to be a valid ssh-rsa or dsa public key'
189
190         public_key_str = api_fixture('authorized_keys')['active']['public_key']
191         page.find_field('public_key').set public_key_str
192         page.find_field('name').set 'added_in_test'
193         click_button 'Submit'
194         assert page.has_text? 'Public key already exists in the database, use a different key.'
195
196         new_key = SSHKey.generate
197         page.find_field('public_key').set new_key.ssh_public_key
198         page.find_field('name').set 'added_in_test'
199         click_button 'Submit'
200       end
201
202       # key must be added. look for it in the refreshed page
203       assert page.has_text? 'added_in_test'
204     end
205   end
206
207   # Check manage profile page and add missing profile to the user
208   def add_profile user
209     assert page.has_no_text? 'My projects'
210     assert page.has_no_text? 'Projects shared with me'
211
212     assert page.has_text? 'Profile'
213     assert page.has_text? 'First name'
214     assert page.has_text? 'Last name'
215     assert page.has_text? 'Identity URL'
216     assert page.has_text? 'Email'
217     assert page.has_text? user['email']
218
219     # Using the default profile which has message and one required field
220
221     # Save profile without filling in the required field. Expect to be back in this profile page again
222     click_button "Save profile"
223     assert page.has_text? 'Profile'
224     assert page.has_text? 'First name'
225     assert page.has_text? 'Last name'
226     assert page.has_text? 'Save profile'
227
228     # This time fill in required field and then save. Expect to go to requested page after that.
229     profile_message = Rails.configuration.user_profile_form_message
230     required_field_title = ''
231     required_field_key = ''
232     profile_config = Rails.configuration.user_profile_form_fields
233     profile_config.andand.each do |entry|
234       if entry['required']
235         required_field_key = entry['key']
236         required_field_title = entry['form_field_title']
237       end
238     end
239
240     assert page.has_text? profile_message[0,25]
241     assert page.has_text? required_field_title
242     page.find_field('user[prefs][:profile][:'+required_field_key+']').set 'value to fill required field'
243
244     click_button "Save profile"
245     # profile saved and in profile page now with success
246     assert page.has_text? 'Thank you for filling in your profile'
247     click_button 'Access Arvados Workbench'
248
249     # profile saved and in home page now
250     assert page.has_text? 'My projects'
251     assert page.has_text? 'Projects shared with me'
252   end
253
254   # test the search box
255   def verify_search_box user
256     if user && user['is_active']
257       # let's search for a valid uuid
258       within('.navbar-fixed-top') do
259         page.find_field('search').set user['uuid']
260         page.find('.glyphicon-search').click
261       end
262
263       # we should now be in the user's page as a result of search
264       assert page.has_text? user['first_name']
265
266       # let's search again for an invalid valid uuid
267       within('.navbar-fixed-top') do
268         search_for = String.new user['uuid']
269         search_for[0]='1'
270         page.find_field('search').set search_for
271         page.find('.glyphicon-search').click
272       end
273
274       # we should see 'not found' error page
275       assert page.has_text? 'Not Found'
276       assert page.has_link? 'Report problem'
277       click_link 'Report problem'
278       within '.modal-content' do
279         assert page.has_text? 'Report a problem'
280         assert page.has_no_text? 'Version / debugging info'
281         assert page.has_text? 'Server version'
282         assert page.has_text? 'Server restarted at'
283         assert page.has_text? 'Found a problem?'
284         assert page.has_button? 'Report issue'
285         assert page.has_button? 'Cancel'
286
287         # enter a report text and click on report
288         page.find_field('report_issue_text').set 'my test report text'
289         click_button 'Report issue'
290       end
291
292       # let's search for the anonymously accessible project
293       publicly_accessible_project = api_fixture('groups')['anonymously_accessible_project']
294
295       within('.navbar-fixed-top') do
296         # search again for the anonymously accessible project
297         page.find_field('search').set publicly_accessible_project['name'][0,10]
298         page.find('.glyphicon-search').click
299       end
300
301       within '.modal-content' do
302         assert page.has_text? 'All projects'
303         assert page.has_text? 'Search'
304         assert page.has_text? 'Cancel'
305         assert_selector('div', text: publicly_accessible_project['name'])
306         find(:xpath, '//div[./span[contains(.,publicly_accessible_project["uuid"])]]').click
307
308         click_button 'Show'
309       end
310
311       # seeing "Unrestricted public data" now
312       assert page.has_text? publicly_accessible_project['name']
313       assert page.has_text? publicly_accessible_project['description']
314     end
315   end
316
317   [
318     [nil, nil, false, false],
319     ['inactive', api_fixture('users')['inactive'], true, false],
320     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
321     ['active', api_fixture('users')['active'], true, true],
322     ['admin', api_fixture('users')['admin'], true, true],
323     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
324     ['active_no_prefs_profile', api_fixture('users')['active_no_prefs_profile'], true, false],
325   ].each do |token, user, invited, has_profile|
326
327     test "visit home page when profile is configured for user #{token}" do
328       # Our test config enabled profile by default. So, no need to update config
329       if !token
330         visit ('/')
331       else
332         visit page_with_token(token)
333       end
334
335       verify_homepage_with_profile user, invited, has_profile
336     end
337
338     test "visit home page when profile not configured for user #{token}" do
339       Rails.configuration.user_profile_form_fields = false
340
341       if !token
342         visit ('/')
343       else
344         visit page_with_token(token)
345       end
346
347       verify_homepage_with_profile user, invited, has_profile
348     end
349
350     test "check help for user #{token}" do
351       if !token
352         visit ('/')
353       else
354         visit page_with_token(token)
355       end
356
357       check_help_menu
358     end
359
360   end
361
362   [
363     ['active', api_fixture('users')['active'], true, true],
364     ['admin', api_fixture('users')['admin'], true, true],
365   ].each do |token, user|
366
367     test "test system menu for user #{token}" do
368       visit page_with_token(token)
369       verify_system_menu user
370     end
371
372     test "test manage account for user #{token}" do
373       visit page_with_token(token)
374       verify_manage_account user
375     end
376
377     test "test search for user #{token}" do
378       visit page_with_token(token)
379       verify_search_box user
380     end
381
382   end
383
384 end