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'), 'Not found text - Please log in'
23       assert page.has_text?('The "Log in" button below will show you a Google sign-in page'), 'Not found text - google sign in page'
24       assert page.has_no_text?('My projects'), 'Found text - My projects'
25       assert page.has_link?("Log in to #{Rails.configuration.site_name}"), 'Not found text - log in to'
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'), 'Not found text - My projects'
30       assert page.has_text?('Projects shared with me'), 'Not found text - Project shared with me'
31       assert page.has_no_text?('Save profile'), 'Found 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'), 'Not found text - Please check the box below . . .'
34       assert page.has_no_text?('Save profile'), 'Found text - Save profile'
35     else
36       assert page.has_text?('Your account is inactive'), 'Not found text - Your account is inactive'
37       assert page.has_no_text?('Save profile'), 'Found text - Save profile'
38     end
39
40     within('.navbar-fixed-top') do
41       if !user
42         assert page.has_link?('Log in'), 'Not found link - Log in'
43       else
44         # my account menu
45         assert page.has_link?("#{user['email']}"), 'Not found link - 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'), 'Found link - Not active'
50             assert page.has_no_link?('Sign agreements'), 'Found link - Sign agreements'
51
52             assert page.has_link?('Manage account'), 'No link - Manage account'
53
54             if profile_config
55               assert page.has_link?('Manage profile'), 'No link - Manage profile'
56             else
57               assert page.has_no_link?('Manage profile'), 'Found link - Manage profile'
58             end
59           end
60           assert page.has_link?('Log out'), 'No 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'), 'No link - Tutorials and User guide'
72         assert page.has_link?('API Reference'), 'No link - API Reference'
73         assert page.has_link?('SDK Reference'), 'No link - SDK Reference'
74         assert page.has_link?('Show version / debugging info'), 'No link - Show version / debugging info'
75         assert page.has_link?('Report a problem'), 'No link - Report a problem'
76         # Version info and Report a problem are tested in "report_issue_test.rb"
77       end
78     end
79   end
80
81   def verify_system_menu user
82     if user && user['is_active']
83       look_for_add_new = nil
84       within('.navbar-fixed-top') do
85         page.find("#system-menu").click
86         if user['is_admin']
87           within('.dropdown-menu') do
88             assert page.has_text?('Groups'), 'No text - Groups'
89             assert page.has_link?('Repositories'), 'No link - Repositories'
90             assert page.has_link?('Virtual machines'), 'No link - Virtual machines'
91             assert page.has_link?('SSH keys'), 'No link - SSH keys'
92             assert page.has_link?('API tokens'), 'No link - API tokens'
93             find('a', text: 'Users').click
94             look_for_add_new = 'Add a new user'
95           end
96         else
97           within('.dropdown-menu') do
98             assert page.has_no_text?('Users'), 'Found text - Users'
99             assert page.has_no_link?('Repositories'), 'Found link - Repositories'
100             assert page.has_no_link?('Virtual machines'), 'Found link - Virtual machines'
101             assert page.has_no_link?('SSH keys'), 'Found link - SSH keys'
102             assert page.has_no_link?('API tokens'), 'Found link - API tokens'
103
104             find('a', text: 'Groups').click
105             look_for_add_new = 'Add a new group'
106           end
107         end
108       end
109       if look_for_add_new
110         assert page.has_text? look_for_add_new
111       end
112     else
113       assert page.has_no_link?('#system-menu'), 'Found link - system menu'
114     end
115   end
116
117   # test manage_account page
118   def verify_manage_account user
119     if user && user['is_active']
120       within('.navbar-fixed-top') do
121         find('a', text: "#{user['email']}").click
122         within('.dropdown-menu') do
123           find('a', text: 'Manage account').click
124         end
125       end
126
127       # now in manage account page
128       assert page.has_text?('Virtual Machines'), 'No text - Virtual Machines'
129       assert page.has_text?('Repositories'), 'No text - Repositories'
130       assert page.has_text?('SSH Keys'), 'No text - SSH Keys'
131       assert page.has_text?('Current Token'), 'No text - Current Token'
132
133       assert page.has_text?('The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados'), 'No text - Arvados API token'
134
135       click_link 'Add new SSH key'
136
137       within '.modal-content' do
138         assert page.has_text?('Public Key'), 'No text - Public Key'
139         assert page.has_button?('Cancel'), 'No button - Cancel'
140         assert page.has_button?('Submit'), 'No button - Submit'
141
142         page.find_field('public_key').set 'first test with an incorrect ssh key value'
143         click_button 'Submit'
144         assert page.has_text?('Public key does not appear to be a valid ssh-rsa or dsa public key'), 'No text - Public key does not appear to be a valid'
145
146         public_key_str = api_fixture('authorized_keys')['active']['public_key']
147         page.find_field('public_key').set public_key_str
148         page.find_field('name').set 'added_in_test'
149         click_button 'Submit'
150         assert page.has_text?('Public key already exists in the database, use a different key.'), 'No text - Public key already exists'
151
152         new_key = SSHKey.generate
153         page.find_field('public_key').set new_key.ssh_public_key
154         page.find_field('name').set 'added_in_test'
155         click_button 'Submit'
156       end
157
158       # key must be added. look for it in the refreshed page
159       assert page.has_text?('added_in_test'), 'No text - added_in_test'
160     end
161   end
162
163   # Check manage profile page and add missing profile to the user
164   def add_profile user
165     assert page.has_no_text?('My projects'), 'Found text - My projects'
166     assert page.has_no_text?('Projects shared with me'), 'Found text - Projects shared with me'
167
168     assert page.has_text?('Profile'), 'No text - Profile'
169     assert page.has_text?('First name'), 'No text - First name'
170     assert page.has_text?('Last name'), 'No text - Last name'
171     assert page.has_text?('Identity URL'), 'No text - Identity URL'
172     assert page.has_text?('Email'), 'No text - Email'
173     assert page.has_text?(user['email']), 'No text - user email'
174
175     # Using the default profile which has message and one required field
176
177     # Save profile without filling in the required field. Expect to be back in this profile page again
178     click_button "Save profile"
179     assert page.has_text?('Profile'), 'No text - Profile'
180     assert page.has_text?('First name'), 'No text - First name'
181     assert page.has_text?('Last name'), 'No text - Last name'
182     assert page.has_text?('Save profile'), 'No text - Save profile'
183
184     # This time fill in required field and then save. Expect to go to requested page after that.
185     profile_message = Rails.configuration.user_profile_form_message
186     required_field_title = ''
187     required_field_key = ''
188     profile_config = Rails.configuration.user_profile_form_fields
189     profile_config.andand.each do |entry|
190       if entry['required']
191         required_field_key = entry['key']
192         required_field_title = entry['form_field_title']
193       end
194     end
195
196     assert page.has_text? profile_message.gsub(/<.*?>/,'')
197     assert page.has_text?(required_field_title), 'No text - configured required field title'
198
199     page.find_field('user[prefs][:profile][:'+required_field_key+']').set 'value to fill required field'
200
201     click_button "Save profile"
202     # profile saved and in profile page now with success
203     assert page.has_text?('Thank you for filling in your profile'), 'No text - Thank you for filling'
204     click_link 'Back to work!'
205
206     # profile saved and in home page now
207     assert page.has_text?('My projects'), 'No text - My projects'
208     assert page.has_text?('Projects shared with me'), 'No text - Projects shared with me'
209   end
210
211   [
212     [nil, nil, false, false],
213     ['inactive', api_fixture('users')['inactive'], true, false],
214     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
215     ['active', api_fixture('users')['active'], true, true],
216     ['admin', api_fixture('users')['admin'], true, true],
217     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
218     ['active_no_prefs_profile', api_fixture('users')['active_no_prefs_profile'], true, false],
219   ].each do |token, user, invited, has_profile|
220
221     test "visit home page when profile is configured for user #{token}" do
222       # Our test config enabled profile by default. So, no need to update config
223       if !token
224         visit ('/')
225       else
226         visit page_with_token(token)
227       end
228
229       verify_homepage_with_profile user, invited, has_profile
230     end
231
232     test "visit home page when profile not configured for user #{token}" do
233       Rails.configuration.user_profile_form_fields = false
234
235       if !token
236         visit ('/')
237       else
238         visit page_with_token(token)
239       end
240
241       verify_homepage_with_profile user, invited, has_profile
242     end
243
244     test "check help for user #{token}" do
245       if !token
246         visit ('/')
247       else
248         visit page_with_token(token)
249       end
250
251       check_help_menu
252     end
253   end
254
255   [
256     ['active', api_fixture('users')['active']],
257     ['admin', api_fixture('users')['admin']],
258   ].each do |token, user|
259
260     test "test system menu for user #{token}" do
261       visit page_with_token(token)
262       verify_system_menu user
263     end
264
265     test "test manage account for user #{token}" do
266       visit page_with_token(token)
267       verify_manage_account user
268     end
269   end
270 end