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