Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / test / integration / anonymous_access_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'integration_helper'
6
7 class AnonymousAccessTest < ActionDispatch::IntegrationTest
8   # These tests don't do state-changing API calls. Save some time by
9   # skipping the database reset.
10   reset_api_fixtures :after_each_test, false
11   reset_api_fixtures :after_suite, true
12
13   setup do
14     need_javascript
15     Rails.configuration.Users.AnonymousUserToken = api_fixture('api_client_authorizations')['anonymous']['api_token']
16   end
17
18   PUBLIC_PROJECT = "/projects/#{api_fixture('groups')['anonymously_accessible_project']['uuid']}"
19
20   def verify_site_navigation_anonymous_enabled user, is_active
21     if user
22       if user['is_active']
23         assert_text 'Unrestricted public data'
24         assert_selector 'a', text: 'Projects'
25         page.find("#projects-menu").click
26         within('.dropdown-menu') do
27           assert_selector 'a', text: 'Search all projects'
28           assert_selector "a[href=\"/projects/public\"]", text: 'Browse public projects'
29           assert_selector 'a', text: 'Add a new project'
30           assert_selector 'li[class="dropdown-header"]', text: 'My projects'
31         end
32       else
33         assert_text 'indicate that you have read and accepted the user agreement'
34       end
35       within('.navbar-fixed-top') do
36         assert_selector 'a', text: Rails.configuration.Workbench.SiteName.downcase
37         assert(page.has_link?("notifications-menu"), 'no user menu')
38         page.find("#notifications-menu").click
39         within('.dropdown-menu') do
40           assert_selector 'a', text: 'Log out'
41         end
42       end
43     else  # anonymous
44       assert_text 'Unrestricted public data'
45       within('.navbar-fixed-top') do
46         assert_text Rails.configuration.Workbench.SiteName.downcase
47         assert_no_selector 'a', text: Rails.configuration.Workbench.SiteName.downcase
48         assert_selector 'a', text: 'Log in'
49         assert_selector 'a', text: 'Browse public projects'
50       end
51     end
52   end
53
54   [
55     [nil, nil, false, false],
56     ['inactive', api_fixture('users')['inactive'], false, false],
57     ['active', api_fixture('users')['active'], true, true],
58   ].each do |token, user, is_active|
59     test "visit public project as user #{token.inspect} when anonymous browsing is enabled" do
60       if !token
61         visit PUBLIC_PROJECT
62       else
63         visit page_with_token(token, PUBLIC_PROJECT)
64       end
65
66       verify_site_navigation_anonymous_enabled user, is_active
67     end
68   end
69
70   test "selection actions when anonymous user accesses shared project" do
71     visit PUBLIC_PROJECT
72
73     assert_selector 'a', text: 'Description'
74     assert_selector 'a', text: 'Data collections'
75     assert_selector 'a', text: 'Pipelines and processes'
76     assert_selector 'a', text: 'Pipeline templates'
77     assert_selector 'a', text: 'Subprojects'
78     assert_selector 'a', text: 'Advanced'
79     assert_no_selector 'a', text: 'Other objects'
80     assert_no_selector 'button', text: 'Add data'
81
82     click_link 'Data collections'
83     click_button 'Selection'
84     within('.selection-action-container') do
85       assert_selector 'li', text: 'Compare selected'
86       assert_no_selector 'li', text: 'Create new collection with selected collections'
87       assert_no_selector 'li', text: 'Copy selected'
88       assert_no_selector 'li', text: 'Move selected'
89       assert_no_selector 'li', text: 'Remove selected'
90     end
91   end
92
93   test "anonymous user accesses data collections tab in shared project" do
94     visit PUBLIC_PROJECT
95     click_link 'Data collections'
96     collection = api_fixture('collections')['user_agreement_in_anonymously_accessible_project']
97     assert_text 'GNU General Public License'
98
99     assert_selector 'a', text: 'Data collections'
100
101     # click on show collection
102     within "tr[data-object-uuid=\"#{collection['uuid']}\"]" do
103       click_link 'Show'
104     end
105
106     # in collection page
107     assert_no_selector 'input', text: 'Create sharing link'
108     assert_no_text 'Sharing and permissions'
109     assert_no_selector 'a', text: 'Upload'
110     assert_no_selector 'button', 'Selection'
111
112     within '#collection_files tr,li', text: 'GNU_General_Public_License,_version_3.pdf' do
113       assert page.has_no_selector?('[value*="GNU_General_Public_License"]')
114       find 'a[title~=View]'
115       find 'a[title~=Download]'
116     end
117   end
118
119   test 'view file' do
120     need_selenium "phantomjs does not follow redirects reliably, maybe https://github.com/ariya/phantomjs/issues/10389"
121     magic = rand(2**512).to_s 36
122     owner = api_fixture('groups')['anonymously_accessible_project']['uuid']
123     col = upload_data_and_get_collection(magic, 'admin', "Hello\\040world.txt", owner)
124     visit '/collections/' + col.uuid
125     find('tr,li', text: 'Hello world.txt').
126       find('a[title~=View]').click
127     assert_text magic
128   end
129
130   [
131     'running anonymously accessible cr',
132     'pipelineInstance'
133   ].each do |proc|
134     test "anonymous user accesses pipelines and processes tab in shared project and clicks on '#{proc}'" do
135       visit PUBLIC_PROJECT
136       click_link 'Data collections'
137       assert_text 'GNU General Public License'
138
139       click_link 'Pipelines and processes'
140       assert_text 'Pipeline in publicly accessible project'
141
142       if proc.include? 'pipeline'
143         verify_pipeline_instance_row
144       else
145         verify_container_request_row proc
146       end
147     end
148   end
149
150   def verify_container_request_row look_for
151     within first('tr', text: look_for) do
152       click_link 'Show'
153     end
154     assert_text 'Public Projects Unrestricted public data'
155     assert_text 'command'
156
157     assert_text 'zzzzz-tpzed-xurymjxw79nv3jz' # modified by user
158     assert_no_selector 'a', text: 'zzzzz-tpzed-xurymjxw79nv3jz'
159     assert_no_selector 'button', text: 'Cancel'
160   end
161
162   def verify_pipeline_instance_row
163     within first('tr[data-kind="arvados#pipelineInstance"]') do
164       assert_text 'Pipeline in publicly accessible project'
165       click_link 'Show'
166     end
167
168     # in pipeline instance page
169     assert_text 'Public Projects Unrestricted public data'
170     assert_text 'This pipeline is complete'
171     assert_no_selector 'a', text: 'Re-run with latest'
172     assert_no_selector 'a', text: 'Re-run options'
173   end
174
175   [
176     'pipelineTemplate',
177     'workflow'
178   ].each do |type|
179     test "anonymous user accesses pipeline templates tab in shared project and click on #{type}" do
180       visit PUBLIC_PROJECT
181       click_link 'Data collections'
182       assert_text 'GNU General Public License'
183
184       assert_selector 'a', text: 'Pipeline templates'
185
186       click_link 'Pipeline templates'
187       assert_text 'Pipeline template in publicly accessible project'
188       assert_text 'Workflow with input specifications'
189
190       if type == 'pipelineTemplate'
191         within first('tr[data-kind="arvados#pipelineTemplate"]') do
192           click_link 'Show'
193         end
194
195         # in template page
196         assert_text 'Public Projects Unrestricted public data'
197         assert_text 'script version'
198         assert_no_selector 'a', text: 'Run this pipeline'
199       else
200         within 'tr[data-kind="arvados#workflow"]', text: "Workflow with default input specifications" do
201           click_link 'Show'
202         end
203
204         # in workflow page
205         assert_text 'Public Projects Unrestricted public data'
206         assert_text 'this workflow has inputs specified'
207       end
208     end
209   end
210
211   test "anonymous user accesses subprojects tab in shared project" do
212     visit PUBLIC_PROJECT + '#Subprojects'
213
214     assert_text 'Subproject in anonymous accessible project'
215
216     within first('tr[data-kind="arvados#group"]') do
217       click_link 'Show'
218     end
219
220     # in subproject
221     assert_text 'Description for subproject in anonymous accessible project'
222   end
223
224   [
225     ['pipeline_in_publicly_accessible_project', true],
226     ['pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false],
227     ['pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false, 'spectator'],
228     ['pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', true, 'admin'],
229
230     ['completed_job_in_publicly_accessible_project', true],
231     ['running_job_in_publicly_accessible_project', true],
232     ['job_in_publicly_accessible_project_but_other_objects_elsewhere', false],
233   ].each do |fixture, objects_readable, user=nil|
234     test "access #{fixture} in public project with objects readable=#{objects_readable} with user #{user}" do
235       pipeline_page = true if fixture.include?('pipeline')
236
237       if pipeline_page
238         object = api_fixture('pipeline_instances')[fixture]
239         page_link = "/pipeline_instances/#{object['uuid']}"
240         expect_log_text = "Log for foo"
241       else      # job
242         object = api_fixture('jobs')[fixture]
243         page_link = "/jobs/#{object['uuid']}"
244         expect_log_text = "stderr crunchstat"
245       end
246
247       if user
248         visit page_with_token user, page_link
249       else
250         visit page_link
251       end
252
253       # click job link, if in pipeline page
254       click_link 'foo' if pipeline_page
255
256       if objects_readable
257         assert_selector 'a[href="#Log"]', text: 'Log'
258         assert_no_selector 'a[data-toggle="disabled"]', text: 'Log'
259         assert_no_text 'zzzzz-4zz18-bv31uwvy3neko21 (Unavailable)'
260         if pipeline_page
261           assert_text 'This pipeline was created from'
262           job_id = object['components']['foo']['job']['uuid']
263           assert_selector 'a', text: job_id
264           assert_selector "a[href=\"/jobs/#{job_id}#Log\"]", text: 'Log'
265
266           # We'd like to test the Log tab on job pages too, but we can't right
267           # now because Poltergeist 1.x doesn't support JavaScript's
268           # Function.prototype.bind, which is used by job_log_graph.js.
269           find(:xpath, "//a[@href='#Log']").click
270           assert_text expect_log_text
271         end
272       else
273         assert_selector 'a[data-toggle="disabled"]', text: 'Log'
274         assert_text 'zzzzz-4zz18-bv31uwvy3neko21 (Unavailable)'
275         assert_text object['job']
276         if pipeline_page
277           assert_no_text 'This pipeline was created from'  # template is not readable
278           assert_no_selector 'a', text: object['components']['foo']['job']['uuid']
279           assert_text 'Log unavailable'
280         end
281         find(:xpath, "//a[@href='#Log']").click
282         assert_text 'zzzzz-4zz18-bv31uwvy3neko21 (Unavailable)'
283         assert_no_text expect_log_text
284       end
285     end
286   end
287
288   [
289     ['new_pipeline_in_publicly_accessible_project', true],
290     ['new_pipeline_in_publicly_accessible_project', true, 'spectator'],
291     ['new_pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false],
292     ['new_pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false, 'spectator'],
293     ['new_pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', true, 'admin'],
294     ['new_pipeline_in_publicly_accessible_project_with_dataclass_file_and_other_objects_elsewhere', false],
295     ['new_pipeline_in_publicly_accessible_project_with_dataclass_file_and_other_objects_elsewhere', false, 'spectator'],
296     ['new_pipeline_in_publicly_accessible_project_with_dataclass_file_and_other_objects_elsewhere', true, 'admin'],
297   ].each do |fixture, objects_readable, user=nil|
298     test "access #{fixture} in public project with objects readable=#{objects_readable} with user #{user}" do
299       object = api_fixture('pipeline_instances')[fixture]
300       page = "/pipeline_instances/#{object['uuid']}"
301       if user
302         visit page_with_token user, page
303       else
304         visit page
305       end
306
307       # click Components tab
308       click_link 'Components'
309
310       if objects_readable
311         assert_text 'This pipeline was created from'
312         if user == 'admin'
313           assert_text 'input'
314           assert_selector 'a', text: 'Choose'
315           assert_selector 'a', text: 'Run'
316           assert_no_selector 'a.disabled', text: 'Run'
317         else
318           assert_selector 'a', text: object['components']['foo']['script_parameters']['input']['value']
319           user ? (assert_selector 'a', text: 'Run') : (assert_no_selector 'a', text: 'Run')
320         end
321       else
322         assert_no_text 'This pipeline was created from'  # template is not readable
323         input = object['components']['foo']['script_parameters']['input']['value']
324         assert_no_selector 'a', text: input
325         if user
326           input = input.gsub('/', '\\/')
327           assert_text "One or more inputs provided are not readable"
328           assert_selector "input[type=text][value=#{input}]"
329           assert_selector 'a.disabled', text: 'Run'
330         else
331           assert_no_text "One or more inputs provided are not readable"
332           assert_text input
333           assert_no_selector 'a', text: 'Run'
334         end
335       end
336     end
337   end
338 end