20850: Fix test selector
[arvados.git] / apps / workbench / test / controllers / actions_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class ActionsControllerTest < ActionController::TestCase
8
9   test "send report" do
10     post :report_issue, params: {format: 'js'}, session: session_for(:admin)
11     assert_response :success
12
13     found_email = false
14     ActionMailer::Base.deliveries.andand.each do |email|
15       if email.subject.include? "Issue reported by admin"
16         found_email = true
17         break
18       end
19     end
20     assert_equal true, found_email, 'Expected email after issue reported'
21   end
22
23   test "combine files into new collection" do
24     post(:combine_selected_files_into_collection, params: {
25            selection: ['zzzzz-4zz18-znfnqtbbv4spc3w/foo',
26                        'zzzzz-4zz18-ehbhgtheo8909or/bar',
27                        'zzzzz-4zz18-y9vne9npefyxh8g/baz',
28                        '7a6ef4c162a5c6413070a8bd0bffc818+150'],
29            format: "json"},
30          session: session_for(:active))
31
32     assert_response 302   # collection created and redirected to new collection page
33
34     assert_includes(response.headers['Location'], '/collections/')
35     new_collection_uuid = response.headers['Location'].split('/')[-1]
36
37     use_token :active
38     collection = Collection.select([:uuid, :manifest_text]).where(uuid: new_collection_uuid).first
39     manifest_text = collection['manifest_text']
40     assert_includes(manifest_text, "foo")
41     assert_includes(manifest_text, "bar")
42     assert_includes(manifest_text, "baz")
43     assert_includes(manifest_text, "0:0:file1 0:0:file2 0:0:file3")
44     assert_includes(manifest_text, "dir1/subdir")
45     assert_includes(manifest_text, "dir2")
46   end
47
48   test "combine files  with repeated names into new collection" do
49     post(:combine_selected_files_into_collection, params: {
50            selection: ['zzzzz-4zz18-znfnqtbbv4spc3w/foo',
51                        'zzzzz-4zz18-00000nonamecoll/foo',
52                        'zzzzz-4zz18-abcd6fx123409f7/foo',
53                        'zzzzz-4zz18-ehbhgtheo8909or/bar',
54                        'zzzzz-4zz18-y9vne9npefyxh8g/baz',
55                        '7a6ef4c162a5c6413070a8bd0bffc818+150'],
56            format: "json"},
57          session: session_for(:active))
58
59     assert_response 302   # collection created and redirected to new collection page
60
61     assert_includes(response.headers['Location'], '/collections/')
62     new_collection_uuid = response.headers['Location'].split('/')[-1]
63
64     use_token :active
65     collection = Collection.select([:uuid, :manifest_text]).where(uuid: new_collection_uuid).first
66     manifest_text = collection['manifest_text']
67     assert_includes(manifest_text, "foo(1)")
68     assert_includes(manifest_text, "foo(2)")
69     assert_includes(manifest_text, "bar")
70     assert_includes(manifest_text, "baz")
71     assert_includes(manifest_text, "0:0:file1 0:0:file2 0:0:file3")
72     assert_includes(manifest_text, "dir1/subdir")
73     assert_includes(manifest_text, "dir2")
74   end
75
76   test "combine collections with repeated filenames in almost similar directories and expect files with proper suffixes" do
77     post(:combine_selected_files_into_collection, params: {
78            selection: ['zzzzz-4zz18-duplicatenames1',
79                        'zzzzz-4zz18-duplicatenames2',
80                        'zzzzz-4zz18-znfnqtbbv4spc3w/foo',
81                        'zzzzz-4zz18-00000nonamecoll/foo',],
82            format: "json"},
83          session: session_for(:active))
84
85     assert_response 302   # collection created and redirected to new collection page
86
87     assert response.headers['Location'].include? '/collections/'
88     new_collection_uuid = response.headers['Location'].split('/')[-1]
89
90     use_token :active
91     collection = Collection.select([:uuid, :manifest_text]).where(uuid: new_collection_uuid).first
92     manifest_text = collection['manifest_text']
93
94     assert_includes(manifest_text, 'foo')
95     assert_includes(manifest_text, 'foo(1)')
96
97     streams = manifest_text.split "\n"
98     streams.each do |stream|
99       if stream.start_with? './dir1'
100         # dir1 stream
101         assert_includes(stream, ':alice(1)')
102         assert_includes(stream, ':alice.txt')
103         assert_includes(stream, ':alice(1).txt')
104         assert_includes(stream, ':bob.txt')
105         assert_includes(stream, ':carol.txt')
106       elsif stream.start_with? './dir2'
107         # dir2 stream
108         assert_includes(stream, ':alice.txt')
109         assert_includes(stream, ':alice(1).txt')
110       elsif stream.start_with? '. '
111         # . stream
112         assert_includes(stream, ':foo')
113         assert_includes(stream, ':foo(1)')
114       end
115     end
116   end
117
118   test "combine collections with same filename in two different streams and expect no suffixes for filenames" do
119     post(:combine_selected_files_into_collection, params: {
120            selection: ['zzzzz-4zz18-znfnqtbbv4spc3w',
121                        'zzzzz-4zz18-foonbarfilesdir'],
122            format: "json"},
123          session: session_for(:active))
124
125     assert_response 302   # collection created and redirected to new collection page
126
127     assert_includes(response.headers['Location'], '/collections/')
128     new_collection_uuid = response.headers['Location'].split('/')[-1]
129
130     use_token :active
131     collection = Collection.select([:uuid, :manifest_text]).where(uuid: new_collection_uuid).first
132     manifest_text = collection['manifest_text']
133
134     streams = manifest_text.split "\n"
135     assert_equal 2, streams.length
136     streams.each do |stream|
137       if stream.start_with? './dir1'
138         assert_includes(stream, 'foo')
139       elsif stream.start_with? '. '
140         assert_includes(stream, 'foo')
141       end
142     end
143     refute_includes(manifest_text, 'foo(1)')
144   end
145
146   test "combine foo files from two different collection streams and expect proper filename suffixes" do
147     post(:combine_selected_files_into_collection, params: {
148            selection: ['zzzzz-4zz18-znfnqtbbv4spc3w/foo',
149                        'zzzzz-4zz18-foonbarfilesdir/dir1/foo'],
150            format: "json"},
151          session: session_for(:active))
152
153     assert_response 302   # collection created and redirected to new collection page
154
155     assert_includes(response.headers['Location'], '/collections/')
156     new_collection_uuid = response.headers['Location'].split('/')[-1]
157
158     use_token :active
159     collection = Collection.select([:uuid, :manifest_text]).where(uuid: new_collection_uuid).first
160     manifest_text = collection['manifest_text']
161
162     streams = manifest_text.split "\n"
163     assert_equal 1, streams.length, "Incorrect number of streams in #{manifest_text}"
164     assert_includes(manifest_text, 'foo')
165     assert_includes(manifest_text, 'foo(1)')
166   end
167
168   [
169     ['collections', 'user_agreement_in_anonymously_accessible_project'],
170     ['groups', 'anonymously_accessible_project'],
171     ['jobs', 'running_job_in_publicly_accessible_project'],
172     ['pipeline_instances', 'pipeline_in_publicly_accessible_project'],
173     ['pipeline_templates', 'pipeline_template_in_publicly_accessible_project'],
174   ].each do |dm, fixture|
175     test "access show method for public #{dm} and expect to see page" do
176       Rails.configuration.Users.AnonymousUserToken = api_fixture('api_client_authorizations')['anonymous']['api_token']
177       get(:show, params: {uuid: api_fixture(dm)[fixture]['uuid']})
178       assert_response :redirect
179       if dm == 'groups'
180         assert_includes @response.redirect_url, "projects/#{fixture['uuid']}"
181       else
182         assert_includes @response.redirect_url, "#{dm}/#{fixture['uuid']}"
183       end
184     end
185   end
186
187   [
188     ['collections', 'foo_collection_in_aproject', 404],
189     ['groups', 'subproject_in_asubproject_with_same_name_as_one_in_active_user_home', 404],
190     ['jobs', 'job_with_latest_version', 404],
191     ['pipeline_instances', 'pipeline_owned_by_active_in_home', 404],
192     ['pipeline_templates', 'template_in_asubproject_with_same_name_as_one_in_active_user_home', 404],
193     ['traits', 'owned_by_aproject_with_no_name', :redirect],
194   ].each do |dm, fixture, expected|
195     test "access show method for non-public #{dm} and expect #{expected}" do
196       Rails.configuration.Users.AnonymousUserToken = api_fixture('api_client_authorizations')['anonymous']['api_token']
197       get(:show, params: {uuid: api_fixture(dm)[fixture]['uuid']})
198       assert_response expected
199       if expected == 404
200         assert_includes @response.inspect, 'Log in'
201       else
202         assert_match /\/users\/welcome/, @response.redirect_url
203       end
204     end
205   end
206 end