11917: Do not clear rails cache at boot time.
[arvados.git] / apps / workbench / test / integration / collection_upload_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 CollectionUploadTest < ActionDispatch::IntegrationTest
8   setup do
9     testfiles.each do |filename, content|
10       open(testfile_path(filename), 'w') do |io|
11         io.write content
12       end
13     end
14     # Database reset doesn't restore KeepServices; we have to
15     # save/restore manually.
16     use_token :admin do
17       @keep_services = KeepService.all.to_a
18     end
19   end
20
21   teardown do
22     use_token :admin do
23       @keep_services.each do |ks|
24         KeepService.find(ks.uuid).update_attributes(ks.attributes)
25       end
26     end
27     testfiles.each do |filename, _|
28       File.unlink(testfile_path filename)
29     end
30   end
31
32   test "Create new collection using upload button" do
33     need_javascript
34     visit page_with_token 'active', aproject_path
35     find('.btn', text: 'Add data').click
36     click_link 'Upload files from my computer'
37     # Should be looking at a new empty collection.
38     assert_text 'New collection'
39     assert_text ' 0 files'
40     assert_text ' 0 bytes'
41     # The "Upload" tab should be active and loaded.
42     assert_selector 'div#Upload.active div.panel'
43   end
44
45   test "Upload two empty files with the same name" do
46     need_selenium "to make file uploads work"
47     visit page_with_token 'active', sandbox_path
48
49     unlock_collection
50
51     find('.nav-tabs a', text: 'Upload').click
52     attach_file 'file_selector', testfile_path('empty.txt')
53     assert_selector 'div', text: 'empty.txt'
54     attach_file 'file_selector', testfile_path('empty.txt')
55     assert_selector 'div.row div span[title]', text: 'empty.txt', count: 2
56     click_button 'Start'
57     assert_text :visible, 'Done!'
58     visit sandbox_path+'.json'
59     assert_match /_text":"\. d41d8\S+ 0:0:empty.txt\\n\. d41d8\S+ 0:0:empty\\\\040\(1\).txt\\n"/, body
60   end
61
62   test "Upload non-empty files" do
63     need_selenium "to make file uploads work"
64     visit page_with_token 'active', sandbox_path
65
66     unlock_collection
67
68     find('.nav-tabs a', text: 'Upload').click
69     attach_file 'file_selector', testfile_path('a')
70     attach_file 'file_selector', testfile_path('foo.txt')
71     assert_selector 'button:not([disabled])', text: 'Start'
72     click_button 'Start'
73     assert_text :visible, 'Done!'
74     visit sandbox_path+'.json'
75     assert_match /_text":"\. 0cc1\S+ 0:1:a\\n\. acbd\S+ 0:3:foo.txt\\n"/, body
76   end
77
78   test "Report mixed-content error" do
79     skip 'Test suite does not use TLS'
80     need_selenium "to make file uploads work"
81     use_token :admin do
82       KeepService.where(service_type: 'proxy').first.
83         update_attributes(service_ssl_flag: false)
84     end
85     visit page_with_token 'active', sandbox_path
86     find('.nav-tabs a', text: 'Upload').click
87     attach_file 'file_selector', testfile_path('foo.txt')
88     assert_selector 'button:not([disabled])', text: 'Start'
89     click_button 'Start'
90     using_wait_time 5 do
91       assert_text :visible, 'server setup problem'
92       assert_text :visible, 'cannot be used from origin'
93     end
94   end
95
96   test "Report network error" do
97     need_selenium "to make file uploads work"
98     use_token :admin do
99       # Even if you somehow do port>2^16, surely nx.example.net won't
100       # respond
101       KeepService.where(service_type: 'proxy').first.
102         update_attributes(service_host: 'nx.example.net',
103                           service_port: 99999)
104     end
105     visit page_with_token 'active', sandbox_path
106
107     unlock_collection
108
109     find('.nav-tabs a', text: 'Upload').click
110     attach_file 'file_selector', testfile_path('foo.txt')
111     assert_selector 'button:not([disabled])', text: 'Start'
112     click_button 'Start'
113     using_wait_time 5 do
114       assert_text :visible, 'network error'
115     end
116   end
117
118   protected
119
120   def aproject_path
121     '/projects/' + api_fixture('groups')['aproject']['uuid']
122   end
123
124   def sandbox_uuid
125     api_fixture('collections')['upload_sandbox']['uuid']
126   end
127
128   def sandbox_path
129     '/collections/' + sandbox_uuid
130   end
131
132   def testfiles
133     {
134       'empty.txt' => '',
135       'a' => 'a',
136       'foo.txt' => 'foo'
137     }
138   end
139
140   def testfile_path filename
141     # Must be an absolute path. https://github.com/jnicklas/capybara/issues/621
142     File.join Dir.getwd, 'tmp', filename
143   end
144
145   def unlock_collection
146     first('.lock-collection-btn').click
147     accept_alert
148   end
149 end