4ffd7ae6fa4ecd030dfd42a1e500e341d4ce37a5
[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   end
12
13   def visit_publicly_accessible_project token=nil, use_config=true, path=nil
14     if use_config
15       Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
16     else
17       Rails.configuration.anonymous_user_token = false
18     end
19
20     path = "/projects/#{api_fixture('groups')['anonymously_accessible_project']['uuid']}/?public_data=true" if !path
21
22     if token
23       visit page_with_token(token, path)
24     else
25       visit path
26     end
27   end
28
29   def verify_site_navigation_anonymous_enabled user, is_active
30     if user
31       if user['is_active']
32         assert_text 'Unrestricted public data'
33         assert_selector 'a', text: 'Projects'
34       else
35         assert_text 'indicate that you have read and accepted the user agreement'
36       end
37       within('.navbar-fixed-top') do
38         assert_no_text 'You are viewing public data'
39         assert_selector 'a', text: "#{user['email']}"
40         find('a', text: "#{user['email']}").click
41         within('.dropdown-menu') do
42           assert_selector 'a', text: 'Log out'
43         end
44       end
45     else  # anonymous
46       assert_text 'Unrestricted public data'
47       within('.navbar-fixed-top') do
48         assert_text 'You are viewing public data'
49         anonymous_user = api_fixture('users')['anonymous']
50         assert_selector 'a', "#{anonymous_user['email']}"
51         find('a', text: "#{anonymous_user['email']}").click
52         within('.dropdown-menu') do
53           assert_selector 'a', text: 'Log in'
54           assert_no_selector 'a', text: 'Log out'
55         end
56       end
57     end
58   end
59
60   [
61     [nil, nil, false, false],
62     ['inactive', api_fixture('users')['inactive'], false, false],
63     ['active', api_fixture('users')['active'], true, true],
64   ].each do |token, user, is_active|
65     test "visit public project as user #{token.inspect} when anonymous browsing is enabled" do
66       visit_publicly_accessible_project token
67       verify_site_navigation_anonymous_enabled user, is_active
68     end
69   end
70
71   test "anonymous user visit public project when anonymous browsing not enabled and expect to see login page" do
72     visit_publicly_accessible_project nil, false
73     assert_text 'Please log in'
74   end
75
76   test "visit non-public project as anonymous when anonymous browsing is enabled and expect page not found" do
77     visit_publicly_accessible_project nil, true,
78         "/projects/#{api_fixture('groups')['aproject']['uuid']}/?public_data=true"
79     assert_text 'Not Found'
80   end
81
82   test "selection actions when anonymous user accesses shared project" do
83     visit_publicly_accessible_project
84
85     assert_selector 'a', text: 'Data collections'
86     assert_selector 'a', text: 'Jobs and pipelines'
87     assert_selector 'a', text: 'Pipeline templates'
88     assert_selector 'a', text: 'Advanced'
89     assert_no_selector 'a', text: 'Subprojects'
90     assert_no_selector 'a', text: 'Other objects'
91     assert_no_selector 'button', text: 'Add data'
92
93     click_button 'Selection'
94     within('.selection-action-container') do
95       assert_selector 'li', text: 'Compare selected'
96       assert_no_selector 'li', text: 'Create new collection with selected collections'
97       assert_no_selector 'li', text: 'Copy selected'
98       assert_no_selector 'li', text: 'Move selected'
99       assert_no_selector 'li', text: 'Remove selected'
100     end
101   end
102
103   [
104     ['All pipelines', 'Pipeline in publicly accessible project'],
105     ['All jobs', 'job submitted'],
106     ['All collections', 'GNU_General_Public_License,_version_3.pdf'],
107   ].each do |selector, expectation|
108     test "verify dashboard when anonymous user accesses shared project and click #{selector}" do
109       visit_publicly_accessible_project
110
111       # go to dashboard
112       click_link 'You are viewing public data'
113
114       assert_no_selector 'a', text: 'Run a pipeline'
115       assert_selector 'a', text: selector
116       click_link selector
117       assert_text expectation
118     end
119   end
120
121   test "anonymous user accesses data collections tab in shared project" do
122     visit_publicly_accessible_project
123
124     assert_selector 'a', text: 'Data collections (1)'
125
126     # click on show collection
127     within first('tr[data-kind="arvados#collection"]') do
128       click_link 'Show'
129     end
130
131     # in collection page
132     assert_no_selector 'input', text: 'Create sharing link'
133     assert_no_selector 'a', text: 'Upload'
134     assert_no_selector 'button', 'Selection'
135
136     within ('#collection_files') do
137       assert_text 'GNU_General_Public_License,_version_3.pdf'
138       # how do i assert the view and download link existence?
139     end
140   end
141
142   [
143     'running_job',
144     'completed_job',
145     'pipelineInstance'
146   ].each do |type|
147     test "anonymous user accesses jobs and pipelines tab in shared project and clicks on #{type}" do
148       visit_publicly_accessible_project
149       click_link 'Jobs and pipelines'
150       assert_text 'Pipeline in publicly accessible project'
151
152       # click on type specified collection
153       if type.include? 'job'
154         verify_job_row type
155       else
156         verify_pipeline_instance_row
157       end
158     end
159   end
160
161   def verify_job_row look_for
162     within first('tr', text: look_for) do
163       click_link 'Show'
164     end
165     assert_text 'script_version'
166
167     assert_text 'zzzzz-tpzed-xurymjxw79nv3jz' # modified by user
168     assert_no_selector 'a', text: 'zzzzz-tpzed-xurymjxw79nv3jz'
169     assert_no_selector 'a', text: 'Log'
170     assert_no_selector 'a', text: 'Move job'
171     assert_no_selector 'button', text: 'Cancel'
172     assert_no_selector 'button', text: 'Re-run job'
173   end
174
175   def verify_pipeline_instance_row
176     within first('tr[data-kind="arvados#pipelineInstance"]') do
177       assert_text 'Pipeline in publicly accessible project'
178       click_link 'Show'
179     end
180
181     # in pipeline instance page
182     assert_text 'This pipeline is complete'
183     assert_no_selector 'a', text: 'Re-run with latest'
184     assert_no_selector 'a', text: 'Re-run options'
185   end
186
187   test "anonymous user accesses pipeline templates tab in shared project" do
188     visit_publicly_accessible_project
189
190     assert_selector 'a', 'Pipeline templates (1)'
191
192     click_link 'Pipeline templates'
193     assert_text 'Pipeline template in publicly accessible project'
194
195     within first('tr[data-kind="arvados#pipelineTemplate"]') do
196       click_link 'Show'
197     end
198
199     # in template page
200     assert_text 'script version'
201     assert_no_selector 'a', text: 'Run this pipeline'
202   end
203
204   [
205     '/users',
206     '/groups',
207   ].each do |page|
208     test "anonymous user accesses publicly accessible project and then traverses to #{page}" do
209       # when anonymous, first visit publicly accessible project
210       visit_publicly_accessible_project
211       visit page
212
213       if page == '/users'
214       assert_no_selector 'a', text: 'Add a new user'
215       elsif page == '/groups'
216         assert_no_selector 'button', text: 'Add a new group'
217       end
218     end
219   end
220 end