Merge branch 'master' into 4523-search-index
[arvados.git] / services / api / test / unit / collection_test.rb
1 require 'test_helper'
2
3 class CollectionTest < ActiveSupport::TestCase
4   def create_collection name, enc=nil
5     txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
6     txt.force_encoding(enc) if enc
7     return Collection.create(manifest_text: txt)
8   end
9
10   test 'accept ASCII manifest_text' do
11     act_as_system_user do
12       c = create_collection 'foo', Encoding::US_ASCII
13       assert c.valid?
14     end
15   end
16
17   test 'accept UTF-8 manifest_text' do
18     act_as_system_user do
19       c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
20       assert c.valid?
21     end
22   end
23
24   test 'refuse manifest_text with invalid UTF-8 byte sequence' do
25     act_as_system_user do
26       c = create_collection "f\xc8o", Encoding::UTF_8
27       assert !c.valid?
28       assert_equal [:manifest_text], c.errors.messages.keys
29       assert_match /UTF-8/, c.errors.messages[:manifest_text].first
30     end
31   end
32
33   test 'refuse manifest_text with non-UTF-8 encoding' do
34     act_as_system_user do
35       c = create_collection "f\xc8o", Encoding::ASCII_8BIT
36       assert !c.valid?
37       assert_equal [:manifest_text], c.errors.messages.keys
38       assert_match /UTF-8/, c.errors.messages[:manifest_text].first
39     end
40   end
41
42   test 'create and update collection and verify file_names' do
43     act_as_system_user do
44       c = create_collection 'foo', Encoding::US_ASCII
45       assert c.valid?
46       created_file_names = c.file_names
47       assert created_file_names
48
49       c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
50       assert_not_equal created_file_names, c.file_names
51     end
52   end
53
54   [
55     2**8,
56     2**18,
57   ].each do |manifest_size|
58     test "create collection with manifest size #{manifest_size} and
59           not expect exceptions even on very large manifest texts" do
60       # file_names has a max size, hence there will be no errors even on large manifests
61       act_as_system_user do
62         manifest_text = '. d41d8cd98f00b204e9800998ecf8427e+0'
63         index = 0
64         while manifest_text.length < manifest_size
65           manifest_text += ' ' + "0:0:veryverylongfilename000000000000#{index}.txt"
66           index += 1
67         end
68         manifest_text += "\n"
69
70         c = Collection.create(manifest_text: manifest_text)
71
72         assert c.valid?
73         created_file_names = c.file_names
74         assert created_file_names
75       end
76     end
77   end
78 end