3296: after profile is filled in by user, bring the user back to profile page with...
[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_config = Rails.configuration.user_profile_form_fields
196     profile_message = ''
197     required_field_title = ''
198     required_field_key = ''
199     profile_config.andand.each do |entry| 
200       if entry['message']
201         profile_message = entry['message']
202       else
203         if entry['required']
204           required_field_key = entry['key']
205           required_field_title = entry['form_field_title']
206         end
207       end
208     end
209
210     assert page.has_text? profile_message
211     assert page.has_text? required_field_title
212     page.find_field('user[prefs][:profile][:'+required_field_key+']').set 'value to fill required field'
213
214     click_button "Save profile"
215     # profile saved and in profile page now with success
216     assert page.has_text? 'Thank you for filling in your profile'
217     click_button 'Take me to my page'
218
219     # profile saved and in home page now
220     assert page.has_text? 'My projects'
221     assert page.has_text? 'Projects shared with me'
222   end
223
224   # test the search box
225   def check_search_box user
226     if user
227       # let's search for a valid uuid
228       within('.navbar-fixed-top') do
229         page.find_field('search').set user['uuid']
230         page.find('.glyphicon-search').click
231       end
232
233       # we should now be in the user's page as a result of search
234       assert page.has_text? user['first_name']
235
236       # let's search again for an invalid valid uuid
237       within('.navbar-fixed-top') do
238         search_for = String.new user['uuid']
239         search_for[0]='1'
240         page.find_field('search').set search_for
241         page.find('.glyphicon-search').click
242       end
243
244       # we should see 'not found' error page
245       assert page.has_text? 'Not Found'
246
247       # let's search for the anonymously accessible project
248       publicly_accessible_project = api_fixture('groups')['anonymously_accessible_project']
249
250       within('.navbar-fixed-top') do
251         # search again for the anonymously accessible project
252         page.find_field('search').set publicly_accessible_project['name'][0,10]
253         page.find('.glyphicon-search').click
254       end
255
256       within '.modal-content' do
257         assert page.has_text? 'All projects'
258         assert page.has_text? 'Search'
259         assert page.has_text? 'Cancel'
260         assert_selector('div', text: publicly_accessible_project['name'])
261         find(:xpath, '//div[./span[contains(.,publicly_accessible_project["uuid"])]]').click
262
263         click_button 'Show'
264       end
265
266       # seeing "Unrestricted public data" now
267       assert page.has_text? publicly_accessible_project['name']
268       assert page.has_text? publicly_accessible_project['description']
269     end
270   end
271
272   [
273     [nil, nil, false, false],
274     ['inactive', api_fixture('users')['inactive'], true, false],
275     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
276     ['active', api_fixture('users')['active'], true, true],
277     ['admin', api_fixture('users')['admin'], true, true],
278     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
279   ].each do |token, user, invited, has_profile|
280       test "visit home page when profile is configured for user #{token}" do
281       # Our test config enabled profile by default. So, no need to update config
282       if !token
283         visit ('/')
284       else
285         visit page_with_token(token)
286       end
287
288       verify_homepage_with_profile user, invited, has_profile
289     end
290   end
291
292   [
293     [nil, nil, false, false],
294     ['inactive', api_fixture('users')['inactive'], true, false],
295     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
296     ['active', api_fixture('users')['active'], true, true],
297     ['admin', api_fixture('users')['admin'], true, true],
298     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
299   ].each do |token, user, invited, has_profile|
300     test "visit home page when profile not configured for user #{token}" do
301       Rails.configuration.user_profile_form_fields = false
302
303       if !token
304         visit ('/')
305       else
306         visit page_with_token(token)
307       end
308
309       verify_homepage_with_profile user, invited, has_profile
310     end
311   end
312
313 end