552a9cd5e8fc53af4a484fbbd9b84798fc86f59a
[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
45     unlock_collection
46
47     find('.nav-tabs a', text: 'Upload').click
48     attach_file 'file_selector', testfile_path('empty.txt')
49     assert_selector 'div', text: 'empty.txt'
50     attach_file 'file_selector', testfile_path('empty.txt')
51     assert_selector 'div.row div span[title]', text: 'empty.txt', count: 2
52     click_button 'Start'
53     assert_text :visible, 'Done!'
54     visit sandbox_path+'.json'
55     assert_match /_text":"\. d41d8\S+ 0:0:empty.txt\\n\. d41d8\S+ 0:0:empty\\\\040\(1\).txt\\n"/, body
56   end
57
58   test "Upload non-empty files" do
59     need_selenium "to make file uploads work"
60     visit page_with_token 'active', sandbox_path
61
62     unlock_collection
63
64     find('.nav-tabs a', text: 'Upload').click
65     attach_file 'file_selector', testfile_path('a')
66     attach_file 'file_selector', testfile_path('foo.txt')
67     assert_selector 'button:not([disabled])', text: 'Start'
68     click_button 'Start'
69     assert_text :visible, 'Done!'
70     visit sandbox_path+'.json'
71     assert_match /_text":"\. 0cc1\S+ 0:1:a\\n\. acbd\S+ 0:3:foo.txt\\n"/, body
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
103     unlock_collection
104
105     find('.nav-tabs a', text: 'Upload').click
106     attach_file 'file_selector', testfile_path('foo.txt')
107     assert_selector 'button:not([disabled])', text: 'Start'
108     click_button 'Start'
109     using_wait_time 5 do
110       assert_text :visible, 'network error'
111     end
112   end
113
114   protected
115
116   def aproject_path
117     '/projects/' + api_fixture('groups')['aproject']['uuid']
118   end
119
120   def sandbox_uuid
121     api_fixture('collections')['upload_sandbox']['uuid']
122   end
123
124   def sandbox_path
125     '/collections/' + sandbox_uuid
126   end
127
128   def testfiles
129     {
130       'empty.txt' => '',
131       'a' => 'a',
132       'foo.txt' => 'foo'
133     }
134   end
135
136   def testfile_path filename
137     # Must be an absolute path. https://github.com/jnicklas/capybara/issues/621
138     File.join Dir.getwd, 'tmp', filename
139   end
140
141   def unlock_collection
142     first('.lock-collection-btn').click
143     accept_alert
144   end
145 end