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