6588: Changed settings menu to user settings menu and changed text if account is...
[arvados.git] / apps / workbench / test / integration / user_settings_menu_test.rb
1 require 'integration_helper'
2
3 class UserSettingsMenuTest < ActionDispatch::IntegrationTest
4   setup do
5     need_javascript
6   end
7
8   # test user settings menu
9   def verify_user_settings_menu user
10     if user['is_active']
11       within('.navbar-fixed-top') do
12         page.find("#notifications-menu").click
13         within('.dropdown-menu') do
14           assert_selector 'a', text: 'Virtual machines'
15           assert_selector 'a', text: 'Repositories'
16           assert_selector 'a', text: 'Current token'
17           assert_selector 'a', text: 'SSH keys'
18           find('a', text: 'SSH keys').click
19         end
20       end
21
22       # now in SSH Keys page
23       assert page.has_text?('Add new SSH key'), 'No text - Add SSH key'
24       add_and_verify_ssh_key
25     else  # inactive user
26       within('.navbar-fixed-top') do
27         page.find("#notifications-menu").click
28         within('.dropdown-menu') do
29           assert page.has_no_link?('Manage profile'), 'Found link - Manage profile'
30         end
31       end
32     end
33   end
34
35   def add_and_verify_ssh_key
36       click_link 'Add new SSH key'
37
38       within '.modal-content' do
39         assert page.has_text?('Public Key'), 'No text - Public Key'
40         assert page.has_button?('Cancel'), 'No button - Cancel'
41         assert page.has_button?('Submit'), 'No button - Submit'
42
43         page.find_field('public_key').set 'first test with an incorrect ssh key value'
44         click_button 'Submit'
45         assert_text 'Public key does not appear to be a valid ssh-rsa or dsa public key'
46
47         public_key_str = api_fixture('authorized_keys')['active']['public_key']
48         page.find_field('public_key').set public_key_str
49         page.find_field('name').set 'added_in_test'
50         click_button 'Submit'
51         assert_text 'Public key already exists in the database, use a different key.'
52
53         new_key = SSHKey.generate
54         page.find_field('public_key').set new_key.ssh_public_key
55         page.find_field('name').set 'added_in_test'
56         click_button 'Submit'
57       end
58
59       # key must be added. look for it in the refreshed page
60       assert_text 'added_in_test'
61   end
62
63   [
64     ['inactive', api_fixture('users')['inactive']],
65     ['inactive_uninvited', api_fixture('users')['inactive_uninvited']],
66     ['active', api_fixture('users')['active']],
67     ['admin', api_fixture('users')['admin']],
68   ].each do |token, user|
69     test "test user settings menu for user #{token}" do
70       visit page_with_token(token)
71       verify_user_settings_menu user
72     end
73   end
74
75   test "pipeline notification shown even though public pipelines exist" do
76     skip "created_by doesn't work that way"
77     Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
78     visit page_with_token 'job_reader'
79     click_link 'notifications-menu'
80     assert_selector 'a', text: 'Click here to learn how to run an Arvados Crunch pipeline'
81   end
82
83   [
84     ['job_reader', :ssh, :pipeline],
85     ['active'],
86   ].each do |user, *expect|
87     test "user settings menu for #{user} with notifications #{expect.inspect}" do
88       Rails.configuration.anonymous_user_token = false
89       visit page_with_token(user)
90       click_link 'notifications-menu'
91       if expect.include? :ssh
92         assert_selector('a', text: 'Click here to set up an SSH public key for use with Arvados')
93         click_link('Click here to set up an SSH public key for use with Arvados')
94         assert_selector('a', text: 'Add new SSH key')
95
96         add_and_verify_ssh_key
97
98         # No more SSH notification
99         click_link 'notifications-menu'
100         assert_no_selector('a', text: 'Click here to set up an SSH public key for use with Arvados')
101       else
102         assert_no_selector('a', text: 'Click here to set up an SSH public key for use with Arvados')
103         assert_no_selector('a', text: 'Click here to learn how to run an Arvados Crunch pipeline')
104       end
105
106       if expect.include? :pipeline
107         assert_selector('a', text: 'Click here to learn how to run an Arvados Crunch pipeline')
108       end
109     end
110   end
111
112   test "verify repositories for active user" do
113     user = api_fixture('users')['active']
114     visit page_with_token('active',"/users/#{api_fixture('users')['active']['uuid']}/repositories")
115     repos = [[api_fixture('repositories')['foo'], true, true],
116              [api_fixture('repositories')['repository3'], false, false],
117              [api_fixture('repositories')['repository4'], true, false]]
118
119     repos.each do |(repo, writable, sharable)|
120       within('tr', text: repo['name']+'.git') do
121         if sharable
122           assert_selector 'a', text:'Share'
123           assert_text 'writable'
124         else
125           assert_text repo['name']
126           assert_no_selector 'a', text:'Share'
127           if writable
128             assert_text 'writable'
129           else
130             assert_text 'read-only'
131           end
132         end
133       end
134     end
135   end
136
137   test "request shell access" do
138     user = api_fixture('users')['spectator']
139     ActionMailer::Base.deliveries = []
140     visit page_with_token('spectator', "/users/#{api_fixture('users')['spectator']['uuid']}/virtual_machines")
141     assert_text 'You do not have access to any virtual machines'
142     click_link 'Send request for shell access'
143
144     # Button text changes to "sending...", then back to normal. In the
145     # test suite we can't depend on confirming the "sending..." state
146     # before it goes back to normal, though.
147     ## assert_selector 'a', text: 'Sending request...'
148     assert_selector 'a', text: 'Send request for shell access'
149     assert_text 'A request for shell access was sent'
150
151     # verify that the email was sent
152     full_name = "#{user['first_name']} #{user['last_name']}"
153     expected = "Shell account request from #{full_name} (#{user['email']}, #{user['uuid']})"
154     found_email = 0
155     ActionMailer::Base.deliveries.each do |email|
156       if email.subject.include?(expected)
157         found_email += 1
158       end
159     end
160     assert_equal 1, found_email, "Expected email after requesting shell access"
161
162     # Revisit the page and verify the request sent message along with
163     # the request button.
164     within('.navbar-fixed-top') do
165       page.find("#notifications-menu").click
166       within('.dropdown-menu') do
167         find('a', text: 'Virtual machines').click
168       end
169     end
170     assert_text 'You do not have access to any virtual machines.'
171     assert_text 'A request for shell access was sent on '
172     assert_selector 'a', text: 'Send request for shell access'
173   end
174
175   test "create new repository" do
176     visit page_with_token("active_trustedclient")
177     within('.navbar-fixed-top') do
178       page.find("#notifications-menu").click
179       within('.dropdown-menu') do
180         assert_selector 'a', text: 'Repositories'
181         find('a', text: 'Repositories').click
182       end
183     end
184     click_on "Add new repository"
185     within ".modal-dialog" do
186       fill_in "Name", with: "workbenchtest"
187       click_on "Create"
188     end
189     assert_text ":active/workbenchtest.git"
190     assert_match /git@git.*:active\/workbenchtest.git/, page.text
191     assert_match /https:\/\/git.*\/active\/workbenchtest.git/, page.text
192   end
193
194   [
195     ['virtual_machines', nil, 'Host name', 'testvm2.shell'],
196     ['repositories', 'Add new repository', 'It may take a minute or two before you can clone your new repository.', 'active/foo'],
197     ['/current_token', nil, 'HISTIGNORE=$HISTIGNORE', 'ARVADOS_API_TOKEN=3kg6k6lzmp9kj5'],
198     ['ssh_keys', 'Add new SSH key', 'Click here to learn about SSH keys in Arvados.', 'active'],
199   ].each do |page_name, button_name, look_for, content|
200     test "test user settings menu for page #{page_name}" do
201       if page_name == '/current_token'
202         visit page_with_token('active', page_name)
203       else
204         visit page_with_token('active', "/users/#{api_fixture('users')['active']['uuid']}/#{page_name}")
205       end
206
207       assert page.has_text? content
208       if button_name
209         assert_selector 'a', text: button_name
210         find('a', text: button_name).click
211       end
212
213       assert page.has_text? look_for
214     end
215   end
216
217   [
218     ['virtual_machines', 'You do not have access to any virtual machines.'],
219     ['repositories', 'You do not seem to have access to any repositories.'],
220     ['/current_token', 'HISTIGNORE=$HISTIGNORE'],
221     ['ssh_keys', 'You have not yet set up an SSH public key for use with Arvados.'],
222   ].each do |page_name, look_for|
223     test "test user settings menu for page #{page_name} when page is empty" do
224       if page_name == '/current_token'
225         visit page_with_token('user1_with_load', page_name)
226       else
227         visit page_with_token('admin', "/users/#{api_fixture('users')['user1_with_load']['uuid']}/#{page_name}")
228       end
229
230      assert page.has_text? look_for
231     end
232   end
233 end