Merge branch '3198-inode-cache' into 3198-writable-fuse, fix tests.
[arvados.git] / apps / workbench / test / integration / anonymous_access_test.rb
1 require 'integration_helper'
2
3 class AnonymousAccessTest < ActionDispatch::IntegrationTest
4   # These tests don't do state-changing API calls. Save some time by
5   # skipping the database reset.
6   reset_api_fixtures :after_each_test, false
7   reset_api_fixtures :after_suite, true
8
9   setup do
10     need_javascript
11     Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
12   end
13
14   PUBLIC_PROJECT = "/projects/#{api_fixture('groups')['anonymously_accessible_project']['uuid']}"
15
16   def verify_site_navigation_anonymous_enabled user, is_active
17     if user
18       if user['is_active']
19         assert_text 'Unrestricted public data'
20         assert_selector 'a', text: 'Projects'
21       else
22         assert_text 'indicate that you have read and accepted the user agreement'
23       end
24       within('.navbar-fixed-top') do
25         assert_selector 'a', text: Rails.configuration.site_name.downcase
26         assert_selector 'a', text: "#{user['email']}"
27         find('a', text: "#{user['email']}").click
28         within('.dropdown-menu') do
29           assert_selector 'a', text: 'Log out'
30         end
31       end
32     else  # anonymous
33       assert_text 'Unrestricted public data'
34       within('.navbar-fixed-top') do
35         assert_text Rails.configuration.site_name.downcase
36         assert_no_selector 'a', text: Rails.configuration.site_name.downcase
37         assert_selector 'a', text: 'Log in'
38       end
39     end
40   end
41
42   [
43     [nil, nil, false, false],
44     ['inactive', api_fixture('users')['inactive'], false, false],
45     ['active', api_fixture('users')['active'], true, true],
46   ].each do |token, user, is_active|
47     test "visit public project as user #{token.inspect} when anonymous browsing is enabled" do
48       if !token
49         visit PUBLIC_PROJECT
50       else
51         visit page_with_token(token, PUBLIC_PROJECT)
52       end
53
54       verify_site_navigation_anonymous_enabled user, is_active
55     end
56   end
57
58   test "selection actions when anonymous user accesses shared project" do
59     visit PUBLIC_PROJECT
60
61     assert_selector 'a', text: 'Description'
62     assert_selector 'a', text: 'Data collections'
63     assert_selector 'a', text: 'Jobs and pipelines'
64     assert_selector 'a', text: 'Pipeline templates'
65     assert_selector 'a', text: 'Advanced'
66     assert_no_selector 'a', text: 'Subprojects'
67     assert_no_selector 'a', text: 'Other objects'
68     assert_no_selector 'button', text: 'Add data'
69
70     click_link 'Data collections'
71     click_button 'Selection'
72     within('.selection-action-container') do
73       assert_selector 'li', text: 'Compare selected'
74       assert_no_selector 'li', text: 'Create new collection with selected collections'
75       assert_no_selector 'li', text: 'Copy selected'
76       assert_no_selector 'li', text: 'Move selected'
77       assert_no_selector 'li', text: 'Remove selected'
78     end
79   end
80
81   test "anonymous user accesses data collections tab in shared project" do
82     visit PUBLIC_PROJECT
83     click_link 'Data collections'
84     collection = api_fixture('collections')['user_agreement_in_anonymously_accessible_project']
85     assert_text 'GNU General Public License'
86
87     assert_selector 'a', text: 'Data collections'
88
89     # click on show collection
90     within "tr[data-object-uuid=\"#{collection['uuid']}\"]" do
91       click_link 'Show'
92     end
93
94     # in collection page
95     assert_no_selector 'input', text: 'Create sharing link'
96     assert_no_text 'Sharing and permissions'
97     assert_no_selector 'a', text: 'Upload'
98     assert_no_selector 'button', 'Selection'
99
100     within '#collection_files tr,li', text: 'GNU_General_Public_License,_version_3.pdf' do
101       assert page.has_no_selector?('[value*="GNU_General_Public_License"]')
102       find 'a[title~=View]'
103       find 'a[title~=Download]'
104     end
105   end
106
107   test 'view file' do
108     magic = rand(2**512).to_s 36
109     CollectionsController.any_instance.stubs(:file_enumerator).returns([magic])
110     collection = api_fixture('collections')['public_text_file']
111     visit '/collections/' + collection['uuid']
112     find('tr,li', text: 'Hello world.txt').
113       find('a[title~=View]').click
114     assert_text magic
115   end
116
117   [
118     'running_job',
119     'completed_job',
120     'pipelineInstance'
121   ].each do |type|
122     test "anonymous user accesses jobs and pipelines tab in shared project and clicks on #{type}" do
123       visit PUBLIC_PROJECT
124       click_link 'Data collections'
125       assert_text 'GNU General Public License'
126
127       click_link 'Jobs and pipelines'
128       assert_text 'Pipeline in publicly accessible project'
129
130       # click on the specified job
131       if type.include? 'job'
132         verify_job_row type
133       else
134         verify_pipeline_instance_row
135       end
136     end
137   end
138
139   def verify_job_row look_for
140     within first('tr', text: look_for) do
141       click_link 'Show'
142     end
143     assert_text 'Public Projects Unrestricted public data'
144     assert_text 'script_version'
145
146     assert_text 'zzzzz-tpzed-xurymjxw79nv3jz' # modified by user
147     assert_no_selector 'a', text: 'zzzzz-tpzed-xurymjxw79nv3jz'
148     assert_no_selector 'a', text: 'Move job'
149     assert_no_selector 'button', text: 'Cancel'
150     assert_no_selector 'button', text: 'Re-run job'
151   end
152
153   def verify_pipeline_instance_row
154     within first('tr[data-kind="arvados#pipelineInstance"]') do
155       assert_text 'Pipeline in publicly accessible project'
156       click_link 'Show'
157     end
158
159     # in pipeline instance page
160     assert_text 'Public Projects Unrestricted public data'
161     assert_text 'This pipeline is complete'
162     assert_no_selector 'a', text: 'Re-run with latest'
163     assert_no_selector 'a', text: 'Re-run options'
164   end
165
166   test "anonymous user accesses pipeline templates tab in shared project" do
167     visit PUBLIC_PROJECT
168     click_link 'Data collections'
169     assert_text 'GNU General Public License'
170
171     assert_selector 'a', text: 'Pipeline templates'
172
173     click_link 'Pipeline templates'
174     assert_text 'Pipeline template in publicly accessible project'
175
176     within first('tr[data-kind="arvados#pipelineTemplate"]') do
177       click_link 'Show'
178     end
179
180     # in template page
181     assert_text 'Public Projects Unrestricted public data'
182     assert_text 'script version'
183     assert_no_selector 'a', text: 'Run this pipeline'
184   end
185
186   [
187     ['pipeline_in_publicly_accessible_project', true],
188     ['pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false],
189     ['pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false, 'spectator'],
190     ['pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', true, 'admin'],
191
192     ['completed_job_in_publicly_accessible_project', true],
193     ['job_in_publicly_accessible_project_but_other_objects_elsewhere', false],
194   ].each do |fixture, objects_readable, user=nil|
195     test "access #{fixture} in public project with objects readable=#{objects_readable} with user #{user}" do
196       pipeline_page = true if fixture.include?('pipeline')
197
198       if pipeline_page
199         object = api_fixture('pipeline_instances')[fixture]
200         page = "/pipeline_instances/#{object['uuid']}"
201       else      # job
202         object = api_fixture('jobs')[fixture]
203         page = "/jobs/#{object['uuid']}"
204       end
205
206       if user
207         visit page_with_token user, page
208       else
209         visit page
210       end
211
212       # click job link, if in pipeline page
213       click_link 'foo' if pipeline_page
214
215       if objects_readable
216         if pipeline_page
217           assert_text 'This pipeline was created from'
218           assert_selector 'a', text: object['components']['foo']['job']['uuid']
219         end
220         assert_no_text 'Output data not available'
221         assert_selector 'a[href="#Log"]', text: 'Log'
222         assert_no_selector 'a[data-toggle="disabled"]', text: 'Log'
223       else
224         if pipeline_page
225           assert_no_text 'This pipeline was created from'  # template is not readable
226           assert_no_selector 'a', text: object['components']['foo']['job']['uuid']
227         end
228         assert_text 'Output data not available'
229         assert_text object['job']
230         assert_selector 'a[data-toggle="disabled"]', text: 'Log'
231       end
232
233       click_link 'Log'
234       if objects_readable
235         assert_no_text 'foo'  # should be in Log tab
236         assert_text 'stderr crunchstat'   if pipeline_page
237       else
238         assert_text 'foo'     # Log tab disabled and hence still in first tab
239         assert_no_text 'stderr crunchstat'  # log line shouldn't be seen
240       end
241     end
242   end
243
244   [
245     ['new_pipeline_in_publicly_accessible_project', true],
246     ['new_pipeline_in_publicly_accessible_project', true, 'spectator'],
247     ['new_pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false],
248     ['new_pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false, 'spectator'],
249     ['new_pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', true, 'admin'],
250     ['new_pipeline_in_publicly_accessible_project_with_dataclass_file_and_other_objects_elsewhere', false],
251     ['new_pipeline_in_publicly_accessible_project_with_dataclass_file_and_other_objects_elsewhere', false, 'spectator'],
252     ['new_pipeline_in_publicly_accessible_project_with_dataclass_file_and_other_objects_elsewhere', true, 'admin'],
253   ].each do |fixture, objects_readable, user=nil|
254     test "access #{fixture} in public project with objects readable=#{objects_readable} with user #{user}" do
255       object = api_fixture('pipeline_instances')[fixture]
256       page = "/pipeline_instances/#{object['uuid']}"
257       if user
258         visit page_with_token user, page
259       else
260         visit page
261       end
262
263       # click Components tab
264       click_link 'Components'
265
266       if objects_readable
267         assert_text 'This pipeline was created from'
268         if user == 'admin'
269           assert_text 'input'
270           assert_selector 'a', text: 'Choose'
271           assert_selector 'a', text: 'Run'
272           assert_no_selector 'a.disabled', text: 'Run'
273         else
274           assert_selector 'a', text: object['components']['foo']['script_parameters']['input']['value']
275           user ? (assert_selector 'a', text: 'Run') : (assert_no_selector 'a', text: 'Run')
276         end
277       else
278         assert_no_text 'This pipeline was created from'  # template is not readable
279         input = object['components']['foo']['script_parameters']['input']['value']
280         assert_no_selector 'a', text: input
281         if user
282           input = input.gsub('/', '\\/')
283           assert_text "One or more inputs provided are not readable"
284           assert_selector "input[type=text][value=#{input}]"
285           assert_selector 'a.disabled', text: 'Run'
286         else
287           assert_no_text "One or more inputs provided are not readable"
288           assert_text input
289           assert_no_selector 'a', text: 'Run'
290         end
291       end
292     end
293   end
294
295   test "anonymous user accesses collection in shared project" do
296     visit "/collections/#{api_fixture('collections')['public_text_file']['uuid']}"
297
298     # in collection page
299     assert_text 'Public Projects Unrestricted public data'
300     assert_text 'Hello world'
301     assert_text 'Content address'
302     assert_selector 'a', text: 'Provenance graph'
303   end
304 end