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