f4b9880825aea2f6df46b3db8c396ddbabdf885e
[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_link? 'Virtual Machines'
140     assert page.has_link? 'Repositories'
141     assert page.has_link? 'SSH Keys'
142     assert page.has_link? '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     profile_config = Rails.configuration.user_profile_form_fields
187     profile_message = ''
188     required_field_title = ''
189     required_field_key = ''
190     profile_config.andand.each do |entry| 
191       if entry['message']
192         profile_message = entry['message']
193       else
194         if entry['required']
195           required_field_key = entry['key']
196           required_field_title = entry['form_field_title']
197         end
198       end
199     end
200
201     assert page.has_text? profile_message
202     assert page.has_text? required_field_title
203     page.find_field('user[prefs][:profile][:'+required_field_key+']').set 'value to fill required field'
204
205     click_button "Save profile"
206
207     # profile saved and in home page now
208     assert page.has_text? 'My projects'
209     assert page.has_text? 'Projects shared with me'
210   end
211
212   # test the search box
213   def check_search_box user
214     if user
215       # let's search for the anonymously accessible project
216       publicly_accessible_project = api_fixture('groups')['anonymously_accessible_project']
217
218       within('.navbar-fixed-top') do
219         page.find_field('search').set user['uuid']
220         page.find('.glyphicon-search').click
221         
222         # we should now be in the user's page as a result of search
223         assert page.has_text? user['email']
224
225         # search again for the anonymously accessible project
226         page.find_field('search').set publicly_accessible_project['name'][0,10]
227         page.find('.glyphicon-search').click
228       end
229
230       within '.modal-content' do
231         assert page.has_text? 'All projects'
232         assert page.has_text? 'Search'
233         assert page.has_text? 'Cancel'
234         assert_selector('div', text: publicly_accessible_project['name'])
235         find(:xpath, '//div[./span[contains(.,publicly_accessible_project["uuid"])]]').click
236
237         click_button 'Show'
238       end
239
240       # seeing "Unrestricted public data" now
241       assert page.has_text? publicly_accessible_project['name']
242       assert page.has_text? publicly_accessible_project['description']
243     end
244   end
245
246   [
247     [nil, nil, false, false],
248     ['inactive', api_fixture('users')['inactive'], true, false],
249     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
250     ['active', api_fixture('users')['active'], true, true],
251     ['admin', api_fixture('users')['admin'], true, true],
252     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
253   ].each do |token, user, invited, has_profile|
254       test "visit home page when profile is configured for user #{token}" do
255       # Our test config enabled profile by default. So, no need to update config
256       if !token
257         visit ('/')
258       else
259         visit page_with_token(token)
260       end
261
262       verify_homepage_with_profile user, invited, has_profile
263     end
264   end
265
266   [
267     [nil, nil, false, false],
268     ['inactive', api_fixture('users')['inactive'], true, false],
269     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
270     ['active', api_fixture('users')['active'], true, true],
271     ['admin', api_fixture('users')['admin'], true, true],
272     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
273   ].each do |token, user, invited, has_profile|
274     test "visit home page when profile not configured for user #{token}" do
275       Rails.configuration.user_profile_form_fields = false
276
277       if !token
278         visit ('/')
279       else
280         visit page_with_token(token)
281       end
282
283       verify_homepage_with_profile user, invited, has_profile
284     end
285   end
286
287 end