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)
10 test 'accept ASCII manifest_text' do
12 c = create_collection 'foo', Encoding::US_ASCII
17 test 'accept UTF-8 manifest_text' do
19 c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
24 test 'refuse manifest_text with invalid UTF-8 byte sequence' do
26 c = create_collection "f\xc8o", Encoding::UTF_8
28 assert_equal [:manifest_text], c.errors.messages.keys
29 assert_match /UTF-8/, c.errors.messages[:manifest_text].first
33 test 'refuse manifest_text with non-UTF-8 encoding' do
35 c = create_collection "f\xc8o", Encoding::ASCII_8BIT
37 assert_equal [:manifest_text], c.errors.messages.keys
38 assert_match /UTF-8/, c.errors.messages[:manifest_text].first
42 test 'create and update collection and verify file_names' do
44 c = create_collection 'foo', Encoding::US_ASCII
46 created_file_names = c.file_names
47 assert created_file_names
48 assert_match /foo.txt/, c.file_names
50 c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
51 assert_not_equal created_file_names, c.file_names
52 assert_match /foo2.txt/, c.file_names
59 ].each do |manifest_size, gets_truncated|
60 test "create collection with manifest size #{manifest_size} which gets truncated #{gets_truncated},
61 and not expect exceptions even on very large manifest texts" do
62 # file_names has a max size, hence there will be no errors even on large manifests
64 manifest_text = './blurfl d41d8cd98f00b204e9800998ecf8427e+0'
66 while manifest_text.length < manifest_size
67 manifest_text += ' ' + "0:0:veryverylongfilename000000000000#{index}.txt\n./subdir1"
71 c = Collection.create(manifest_text: manifest_text)
75 assert_match /veryverylongfilename0000000000001.txt/, c.file_names
76 assert_match /veryverylongfilename0000000000002.txt/, c.file_names
78 assert_match /blurfl/, c.file_names
79 assert_match /subdir1/, c.file_names
85 test "full text search for collections" do
86 # file_names column does not get populated when fixtures are loaded, hence setup test data
88 Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
89 Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
90 Collection.create(manifest_text: ". 85877ca2d7e05498dd3d109baf2df106+95+A3a4e26a366ee7e4ed3e476ccf05354761be2e4ae@545a9920 0:95:file_in_subdir1\n./subdir2/subdir3 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file1.txt 32:32:file2.txt\n./subdir2/subdir3/subdir4 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file3.txt 32:32:file4.txt")
95 ['foo bar', false], # no collection matching both
96 ['foo&bar', false], # no collection matching both
97 ['foo|bar', true], # works only no spaces between the words
98 ['Gnu public', true], # both prefixes found, though not consecutively
99 ['Gnu&public', true], # both prefixes found, though not consecutively
100 ['file4', true], # prefix match
101 ['file4.txt', true], # whole string match
102 ['filex', false], # no such prefix
103 ['subdir', true], # prefix matches
106 ['subdir2/subdir3', true],
107 ['subdir2/subdir3/subdir4', true],
108 ['subdir2 file4', true], # look for both prefixes
109 ['subdir4', false], # not a prefix match
110 ].each do |search_filter, expect_results|
111 search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
112 results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
122 test 'portable data hash with missing size hints' do
123 [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x",
124 ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x"],
125 [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x",
126 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x"],
127 [". d41d8cd98f00b204e9800998ecf8427e 0:0:x",
128 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x"],
129 ].each do |unportable, portable|
130 c = Collection.new(manifest_text: unportable)
132 assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
133 c.portable_data_hash)
137 [0, 2, 4, nil].each do |ask|
138 test "set replication_desired to #{ask.inspect}" do
139 Rails.configuration.default_collection_replication = 2
140 act_as_user users(:active) do
141 c = collections(:replication_undesired_unconfirmed)
142 c.update_attributes replication_desired: ask
143 assert_equal ask, c.replication_desired
148 test "replication_confirmed* can be set by admin user" do
149 c = collections(:replication_desired_2_unconfirmed)
150 act_as_user users(:admin) do
151 assert c.update_attributes(replication_confirmed: 2,
152 replication_confirmed_at: Time.now)
156 test "replication_confirmed* cannot be set by non-admin user" do
157 act_as_user users(:active) do
158 c = collections(:replication_desired_2_unconfirmed)
159 # Cannot set just one at a time.
160 assert_raise ArvadosModel::PermissionDeniedError do
161 c.update_attributes replication_confirmed: 1
163 assert_raise ArvadosModel::PermissionDeniedError do
164 c.update_attributes replication_confirmed_at: Time.now
166 # Cannot set both at once, either.
167 assert_raise ArvadosModel::PermissionDeniedError do
168 c.update_attributes(replication_confirmed: 1,
169 replication_confirmed_at: Time.now)
174 test "replication_confirmed* can be cleared (but only together) by non-admin user" do
175 act_as_user users(:active) do
176 c = collections(:replication_desired_2_confirmed_2)
177 # Cannot clear just one at a time.
178 assert_raise ArvadosModel::PermissionDeniedError do
179 c.update_attributes replication_confirmed: nil
182 assert_raise ArvadosModel::PermissionDeniedError do
183 c.update_attributes replication_confirmed_at: nil
185 # Can clear both at once.
187 assert c.update_attributes(replication_confirmed: nil,
188 replication_confirmed_at: nil)
192 test "clear replication_confirmed* when introducing a new block in manifest" do
193 c = collections(:replication_desired_2_confirmed_2)
194 act_as_user users(:active) do
195 assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
196 assert_nil c.replication_confirmed
197 assert_nil c.replication_confirmed_at
201 test "don't clear replication_confirmed* when just renaming a file" do
202 c = collections(:replication_desired_2_confirmed_2)
203 act_as_user users(:active) do
204 new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
205 assert c.update_attributes(manifest_text: new_manifest)
206 assert_equal 2, c.replication_confirmed
207 assert_not_nil c.replication_confirmed_at
211 test "don't clear replication_confirmed* when just deleting a data block" do
212 c = collections(:replication_desired_2_confirmed_2)
213 act_as_user users(:active) do
214 new_manifest = c.signed_manifest_text
215 new_manifest.sub!(/ \S+:bar/, '')
216 new_manifest.sub!(/ acbd\S+/, '')
218 # Confirm that we did just remove a block from the manifest (if
219 # not, this test would pass without testing the relevant case):
220 assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
222 assert c.update_attributes(manifest_text: new_manifest)
223 assert_equal 2, c.replication_confirmed
224 assert_not_nil c.replication_confirmed_at
228 test "create collection with properties" do
229 act_as_system_user do
230 c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
231 properties: {'property_1' => 'value_1'})
233 assert_equal 'value_1', c.properties['property_1']
237 test 'create, delete, recreate collection with same name and owner' do
238 act_as_user users(:active) do
239 # create collection with name
240 c = Collection.create(manifest_text: '',
241 name: "test collection name")
245 # mark collection as expired
246 c.update_attribute 'expires_at', Time.new.strftime("%Y-%m-%d")
247 c = Collection.where(uuid: uuid)
248 assert_empty c, 'Should not be able to find expired collection'
250 # recreate collection with the same name
251 c = Collection.create(manifest_text: '',
252 name: "test collection name")
257 test "find_all_for_docker_image resolves names that look like hashes" do
258 coll_list = Collection.
259 find_all_for_docker_image('a' * 64, nil, [users(:active)])
260 coll_uuids = coll_list.map(&:uuid)
261 assert_includes(coll_uuids, collections(:docker_image).uuid)