3296: split profile config into two parameters: one for fields and one for message.
[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 !invited
49             page.has_no_link? ('Not active')
50           else
51             page.has_no_link? ('Sign agreements')
52             page.has_link? ('Manage account')
53
54             if profile_config
55               page.has_link? ('Manage profile')
56             else
57               page.has_no_link? ('Manage profile')
58             end
59           end
60           page.has_link? ('Log out')
61         end
62       end
63     end
64
65     # check help menu
66     check_help_menu
67
68     if user && user['is_active']
69       # check system menu
70       check_system_menu user
71
72       # test manage account page
73       check_manage_account_page user
74
75       # check search box
76       check_search_box user
77     end
78   end
79
80   # test the help menu
81   def check_help_menu
82     within('.navbar-fixed-top') do
83       page.find("#arv-help").click
84       within('.dropdown-menu') do
85         assert page.has_link? 'Tutorials and User guide'
86         assert page.has_link? 'API Reference'
87         assert page.has_link? 'SDK Reference'
88       end
89     end
90   end
91
92   # test the system menu
93   def check_system_menu user
94     if user && user['is_active']
95       look_for_add_new = nil
96       within('.navbar-fixed-top') do
97         page.find("#system-menu").click
98         if user['is_admin']
99           within('.dropdown-menu') do
100             assert page.has_text? 'Groups'
101             assert page.has_link? 'Repositories'
102             assert page.has_link? 'Virtual machines'
103             assert page.has_link? 'SSH keys'
104             assert page.has_link? 'API tokens'
105             find('a', text: 'Users').click
106             look_for_add_new = 'Add a new user'
107           end
108         else
109           within('.dropdown-menu') do
110             assert page.has_no_text? 'Users'
111             assert page.has_no_link? 'Repositories'
112             assert page.has_no_link? 'Virtual machines'
113             assert page.has_no_link? 'SSH keys'
114             assert page.has_no_link? 'API tokens'
115
116             find('a', text: 'Groups').click
117             look_for_add_new = 'Add a new group'
118           end
119         end
120       end
121       if look_for_add_new
122         assert page.has_text? look_for_add_new
123       end
124     else
125       assert page.has_no_link? '#system-menu'
126     end
127   end
128
129   # test manage_account page
130   def check_manage_account_page user
131     within('.navbar-fixed-top') do
132       find('a', text: "#{user['email']}").click
133       within('.dropdown-menu') do
134         find('a', text: 'Manage account').click
135       end
136     end
137
138     # now in manage account page
139     assert page.has_text? 'Virtual Machines'
140     assert page.has_text? 'Repositories'
141     assert page.has_text? 'SSH Keys'
142     assert page.has_text? 'Current Token'
143
144     assert page.has_text? 'The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados'
145
146     click_link 'Add new SSH key'
147
148     within '.modal-content' do
149       assert page.has_text? 'Public Key'
150       assert page.has_button? 'Cancel'
151       assert page.has_button? 'Submit'
152
153       page.find_field('public_key').set 'first test with an incorrect ssh key value'
154       click_button 'Submit'
155       assert page.has_text? 'Public key does not appear to be a valid ssh-rsa or dsa public key'
156
157       public_key_str = api_fixture('authorized_keys')['active']['public_key']
158       page.find_field('public_key').set public_key_str
159       page.find_field('name').set 'added_in_test'
160       click_button 'Submit'
161       assert page.has_text? 'Public key already exists in the database, use a different key.'
162
163       new_key = SSHKey.generate
164       page.find_field('public_key').set new_key.ssh_public_key
165       page.find_field('name').set 'added_in_test'
166       click_button 'Submit'
167     end
168
169     # key must be added. look for it in the refreshed page
170     assert page.has_text? 'added_in_test'
171   end
172
173   # Check manage profile page and add missing profile to the user
174   def add_profile user
175     assert page.has_no_text? 'My projects'
176     assert page.has_no_text? 'Projects shared with me'
177
178     assert page.has_text? 'Profile'
179     assert page.has_text? 'First name'
180     assert page.has_text? 'Last name'
181     assert page.has_text? 'Identity URL'
182     assert page.has_text? 'Email'
183     assert page.has_text? user['email']
184
185     # Using the default profile which has message and one required field
186
187     # Save profile without filling in the required field. Expect to be back in this profile page again
188     click_button "Save profile"
189     assert page.has_text? 'Profile'
190     assert page.has_text? 'First name'
191     assert page.has_text? 'Last name'
192     assert page.has_text? 'Save profile'
193
194     # This time fill in required field and then save. Expect to go to requested page after that.
195     profile_message = Rails.configuration.user_profile_form_message
196     required_field_title = ''
197     required_field_key = ''
198     profile_config = Rails.configuration.user_profile_form_fields
199     profile_config.andand.each do |entry|
200       if entry['required']
201         required_field_key = entry['key']
202         required_field_title = entry['form_field_title']
203       end
204     end
205
206     assert page.has_text? profile_message
207     assert page.has_text? required_field_title
208     page.find_field('user[prefs][:profile][:'+required_field_key+']').set 'value to fill required field'
209
210     click_button "Save profile"
211     # profile saved and in profile page now with success
212     assert page.has_text? 'Thank you for filling in your profile'
213     click_button 'Take me to my page'
214
215     # profile saved and in home page now
216     assert page.has_text? 'My projects'
217     assert page.has_text? 'Projects shared with me'
218   end
219
220   # test the search box
221   def check_search_box user
222     if user
223       # let's search for a valid uuid
224       within('.navbar-fixed-top') do
225         page.find_field('search').set user['uuid']
226         page.find('.glyphicon-search').click
227       end
228
229       # we should now be in the user's page as a result of search
230       assert page.has_text? user['first_name']
231
232       # let's search again for an invalid valid uuid
233       within('.navbar-fixed-top') do
234         search_for = String.new user['uuid']
235         search_for[0]='1'
236         page.find_field('search').set search_for
237         page.find('.glyphicon-search').click
238       end
239
240       # we should see 'not found' error page
241       assert page.has_text? 'Not Found'
242
243       # let's search for the anonymously accessible project
244       publicly_accessible_project = api_fixture('groups')['anonymously_accessible_project']
245
246       within('.navbar-fixed-top') do
247         # search again for the anonymously accessible project
248         page.find_field('search').set publicly_accessible_project['name'][0,10]
249         page.find('.glyphicon-search').click
250       end
251
252       within '.modal-content' do
253         assert page.has_text? 'All projects'
254         assert page.has_text? 'Search'
255         assert page.has_text? 'Cancel'
256         assert_selector('div', text: publicly_accessible_project['name'])
257         find(:xpath, '//div[./span[contains(.,publicly_accessible_project["uuid"])]]').click
258
259         click_button 'Show'
260       end
261
262       # seeing "Unrestricted public data" now
263       assert page.has_text? publicly_accessible_project['name']
264       assert page.has_text? publicly_accessible_project['description']
265     end
266   end
267
268   [
269     [nil, nil, false, false],
270     ['inactive', api_fixture('users')['inactive'], true, false],
271     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
272     ['active', api_fixture('users')['active'], true, true],
273     ['admin', api_fixture('users')['admin'], true, true],
274     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
275   ].each do |token, user, invited, has_profile|
276       test "visit home page when profile is configured for user #{token}" do
277       # Our test config enabled profile by default. So, no need to update config
278       if !token
279         visit ('/')
280       else
281         visit page_with_token(token)
282       end
283
284       verify_homepage_with_profile user, invited, has_profile
285     end
286   end
287
288   [
289     [nil, nil, false, false],
290     ['inactive', api_fixture('users')['inactive'], true, false],
291     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
292     ['active', api_fixture('users')['active'], true, true],
293     ['admin', api_fixture('users')['admin'], true, true],
294     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
295   ].each do |token, user, invited, has_profile|
296     test "visit home page when profile not configured for user #{token}" do
297       Rails.configuration.user_profile_form_fields = false
298
299       if !token
300         visit ('/')
301       else
302         visit page_with_token(token)
303       end
304
305       verify_homepage_with_profile user, invited, has_profile
306     end
307   end
308
309 end