Merge branch '5824-keep-web' into 5824-keep-web-workbench
[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" 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     assert_text :visible, 'Done!'
64     visit sandbox_path+'.json'
65     assert_match /_text":"\. 0cc1\S+ 0:1:a\\n\. acbd\S+ 0:3:foo.txt\\n"/, body
66   end
67
68   test "Report mixed-content error" do
69     skip 'Test suite does not use TLS'
70     need_selenium "to make file uploads work"
71     use_token :admin do
72       KeepService.where(service_type: 'proxy').first.
73         update_attributes(service_ssl_flag: false)
74     end
75     visit page_with_token 'active', sandbox_path
76     find('.nav-tabs a', text: 'Upload').click
77     attach_file 'file_selector', testfile_path('foo.txt')
78     assert_selector 'button:not([disabled])', text: 'Start'
79     click_button 'Start'
80     using_wait_time 5 do
81       assert_text :visible, 'server setup problem'
82       assert_text :visible, 'cannot be used from origin'
83     end
84   end
85
86   test "Report network error" do
87     need_selenium "to make file uploads work"
88     use_token :admin do
89       # Even if you somehow do port>2^16, surely nx.example.net won't
90       # respond
91       KeepService.where(service_type: 'proxy').first.
92         update_attributes(service_host: 'nx.example.net',
93                           service_port: 99999)
94     end
95     visit page_with_token 'active', sandbox_path
96     find('.nav-tabs a', text: 'Upload').click
97     attach_file 'file_selector', testfile_path('foo.txt')
98     assert_selector 'button:not([disabled])', text: 'Start'
99     click_button 'Start'
100     using_wait_time 5 do
101       assert_text :visible, 'network error'
102     end
103   end
104
105   protected
106
107   def aproject_path
108     '/projects/' + api_fixture('groups')['aproject']['uuid']
109   end
110
111   def sandbox_uuid
112     api_fixture('collections')['upload_sandbox']['uuid']
113   end
114
115   def sandbox_path
116     '/collections/' + sandbox_uuid
117   end
118
119   def testfiles
120     {
121       'empty.txt' => '',
122       'a' => 'a',
123       'foo.txt' => 'foo'
124     }
125   end
126
127   def testfile_path filename
128     # Must be an absolute path. https://github.com/jnicklas/capybara/issues/621
129     File.join Dir.getwd, 'tmp', filename
130   end
131 end