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