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