3620: display gear menu to admin users only. update the layout tests accordingly.
[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   end
11
12   def verify_homepage user, invited, has_profile
13     profile_config = Rails.configuration.user_profile_form_fields
14
15     if !user
16       assert page.has_text?('Please log in'), 'Not found text - Please log in'
17       assert page.has_text?('The "Log in" button below will show you a Google sign-in page'), 'Not found text - google sign in page'
18       assert page.has_no_text?('My projects'), 'Found text - My projects'
19       assert page.has_link?("Log in to #{Rails.configuration.site_name}"), 'Not found text - log in to'
20     elsif user['is_active']
21       if profile_config && !has_profile
22         assert page.has_text?('Save profile'), 'No text - Save profile'
23       else
24         assert page.has_text?('My projects'), 'Not found text - My projects'
25         assert page.has_text?('Projects shared with me'), 'Not found text - Project shared with me'
26       end
27     elsif invited
28       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 . . .'
29     else
30       assert page.has_text?('Your account is inactive'), 'Not found text - Your account is inactive'
31     end
32
33     within('.navbar-fixed-top') do
34       if !user
35         assert page.has_link?('Log in'), 'Not found link - Log in'
36       else
37         # my account menu
38         assert page.has_link?("#{user['email']}"), 'Not found link - email'
39         find('a', text: "#{user['email']}").click
40         within('.dropdown-menu') do
41           if user['is_active']
42             assert page.has_no_link?('Not active'), 'Found link - Not active'
43             assert page.has_no_link?('Sign agreements'), 'Found link - Sign agreements'
44
45             assert page.has_link?('Manage account'), 'No link - Manage account'
46
47             if profile_config
48               assert page.has_link?('Manage profile'), 'No link - Manage profile'
49             else
50               assert page.has_no_link?('Manage profile'), 'Found link - Manage profile'
51             end
52           else
53             assert page.has_no_link?('Manage account'), 'Found link - Manage account'
54             assert page.has_no_link?('Manage profile'), 'Found link - Manage profile'
55           end
56           assert page.has_link?('Log out'), 'No link - Log out'
57         end
58       end
59     end
60   end
61
62   # test the help menu
63   def check_help_menu
64     within('.navbar-fixed-top') do
65       page.find("#arv-help").click
66       within('.dropdown-menu') do
67         assert page.has_link?('Tutorials and User guide'), 'No link - Tutorials and User guide'
68         assert page.has_link?('API Reference'), 'No link - API Reference'
69         assert page.has_link?('SDK Reference'), 'No link - SDK Reference'
70         assert page.has_link?('Show version / debugging info ...'), 'No link - Show version / debugging info'
71         assert page.has_link?('Report a problem ...'), 'No link - Report a problem'
72         # Version info and Report a problem are tested in "report_issue_test.rb"
73       end
74     end
75   end
76
77   def verify_system_menu user
78     if user && user['is_admin']
79       assert page.has_link?('system-menu'), 'No link - system menu'
80       within('.navbar-fixed-top') do
81         page.find("#system-menu").click
82         within('.dropdown-menu') do
83           assert page.has_text?('Groups'), 'No text - Groups'
84           assert page.has_link?('Repositories'), 'No link - Repositories'
85           assert page.has_link?('Virtual machines'), 'No link - Virtual machines'
86           assert page.has_link?('SSH keys'), 'No link - SSH keys'
87           assert page.has_link?('API tokens'), 'No link - API tokens'
88           find('a', text: 'Users').click
89         end
90       end
91       assert page.has_text? 'Add a new user'
92     else
93       assert page.has_no_link?('system-menu'), 'Found link - system menu'
94     end
95   end
96
97   # test manage_account page
98   def verify_manage_account user
99     if user && user['is_active']
100       within('.navbar-fixed-top') do
101         find('a', text: "#{user['email']}").click
102         within('.dropdown-menu') do
103           find('a', text: 'Manage account').click
104         end
105       end
106
107       # now in manage account page
108       assert page.has_text?('Virtual Machines'), 'No text - Virtual Machines'
109       assert page.has_text?('Repositories'), 'No text - Repositories'
110       assert page.has_text?('SSH Keys'), 'No text - SSH Keys'
111       assert page.has_text?('Current Token'), 'No text - Current Token'
112
113       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'
114
115       click_link 'Add new SSH key'
116
117       within '.modal-content' do
118         assert page.has_text?('Public Key'), 'No text - Public Key'
119         assert page.has_button?('Cancel'), 'No button - Cancel'
120         assert page.has_button?('Submit'), 'No button - Submit'
121
122         page.find_field('public_key').set 'first test with an incorrect ssh key value'
123         click_button 'Submit'
124         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'
125
126         public_key_str = api_fixture('authorized_keys')['active']['public_key']
127         page.find_field('public_key').set public_key_str
128         page.find_field('name').set 'added_in_test'
129         click_button 'Submit'
130         assert page.has_text?('Public key already exists in the database, use a different key.'), 'No text - Public key already exists'
131
132         new_key = SSHKey.generate
133         page.find_field('public_key').set new_key.ssh_public_key
134         page.find_field('name').set 'added_in_test'
135         click_button 'Submit'
136       end
137
138       # key must be added. look for it in the refreshed page
139       assert page.has_text?('added_in_test'), 'No text - added_in_test'
140     end
141   end
142
143   [
144     [nil, nil, false, false],
145     ['inactive', api_fixture('users')['inactive'], true, false],
146     ['inactive_uninvited', api_fixture('users')['inactive_uninvited'], false, false],
147     ['active', api_fixture('users')['active'], true, true],
148     ['admin', api_fixture('users')['admin'], true, true],
149     ['active_no_prefs', api_fixture('users')['active_no_prefs'], true, false],
150     ['active_no_prefs_profile', api_fixture('users')['active_no_prefs_profile'], true, false],
151   ].each do |token, user, invited, has_profile|
152
153     test "visit home page for user #{token}" do
154       if !token
155         visit ('/')
156       else
157         visit page_with_token(token)
158       end
159
160       verify_homepage user, invited, has_profile
161     end
162
163     test "check help for user #{token}" do
164       if !token
165         visit ('/')
166       else
167         visit page_with_token(token)
168       end
169
170       check_help_menu
171     end
172   end
173
174   [
175     ['active', api_fixture('users')['active']],
176     ['admin', api_fixture('users')['admin']],
177   ].each do |token, user|
178
179     test "test system menu for user #{token}" do
180       visit page_with_token(token)
181       verify_system_menu user
182     end
183
184     test "test manage account for user #{token}" do
185       visit page_with_token(token)
186       verify_manage_account user
187     end
188   end
189 end