70645615a062d3a967de2af32e60ef67ff2f50d2
[arvados.git] / services / api / test / unit / collection_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'sweep_trashed_objects'
7 require 'fix_collection_versions_timestamps'
8
9 class CollectionTest < ActiveSupport::TestCase
10   include DbCurrentTime
11
12   def create_collection name, enc=nil
13     txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
14     txt.force_encoding(enc) if enc
15     return Collection.create(manifest_text: txt, name: name)
16   end
17
18   test 'accept ASCII manifest_text' do
19     act_as_system_user do
20       c = create_collection 'foo', Encoding::US_ASCII
21       assert c.valid?
22     end
23   end
24
25   test 'accept UTF-8 manifest_text' do
26     act_as_system_user do
27       c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
28       assert c.valid?
29     end
30   end
31
32   test 'refuse manifest_text with invalid UTF-8 byte sequence' do
33     act_as_system_user do
34       c = create_collection "f\xc8o", Encoding::UTF_8
35       assert !c.valid?
36       assert_equal [:manifest_text], c.errors.messages.keys
37       assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
38     end
39   end
40
41   test 'refuse manifest_text with non-UTF-8 encoding' do
42     act_as_system_user do
43       c = create_collection "f\xc8o", Encoding::ASCII_8BIT
44       assert !c.valid?
45       assert_equal [:manifest_text], c.errors.messages.keys
46       assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
47     end
48   end
49
50   [
51     ". 0:0:foo.txt",
52     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
53     "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
54     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
55   ].each do |manifest_text|
56     test "create collection with invalid manifest text #{manifest_text} and expect error" do
57       act_as_system_user do
58         c = Collection.create(manifest_text: manifest_text)
59         assert !c.valid?
60       end
61     end
62   end
63
64   [
65     [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", 1, 34],
66     [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:30:foo.txt 0:30:foo1.txt 0:30:foo2.txt 0:30:foo3.txt 0:30:foo4.txt\n", 5, 184],
67     [". d41d8cd98f00b204e9800998ecf8427e 0:0:.\n", 0, 0]
68   ].each do |manifest, count, size|
69     test "file stats on create collection with #{manifest}" do
70       act_as_system_user do
71         c = Collection.create(manifest_text: manifest)
72         assert_equal count, c.file_count
73         assert_equal size, c.file_size_total
74       end
75     end
76   end
77
78   test "file stats cannot be changed unless through manifest change" do
79     act_as_system_user do
80       # Direct changes to file stats should be ignored
81       c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n")
82       c.file_count = 6
83       c.file_size_total = 30
84       assert c.valid?
85       assert_equal 1, c.file_count
86       assert_equal 34, c.file_size_total
87
88       # File stats specified on create should be ignored and overwritten
89       c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", file_count: 10, file_size_total: 10)
90       assert c.valid?
91       assert_equal 1, c.file_count
92       assert_equal 34, c.file_size_total
93
94       # Updating the manifest should change file stats
95       c.update_attributes(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:34:foo2.txt\n")
96       assert c.valid?
97       assert_equal 2, c.file_count
98       assert_equal 68, c.file_size_total
99
100       # Updating file stats and the manifest should use manifest values
101       c.update_attributes(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", file_count:10, file_size_total: 10)
102       assert c.valid?
103       assert_equal 1, c.file_count
104       assert_equal 34, c.file_size_total
105
106       # Updating just the file stats should be ignored
107       c.update_attributes(file_count: 10, file_size_total: 10)
108       assert c.valid?
109       assert_equal 1, c.file_count
110       assert_equal 34, c.file_size_total
111     end
112   end
113
114   [
115     nil,
116     "",
117     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
118   ].each do |manifest_text|
119     test "create collection with valid manifest text #{manifest_text.inspect} and expect success" do
120       act_as_system_user do
121         c = Collection.create(manifest_text: manifest_text)
122         assert c.valid?
123       end
124     end
125   end
126
127   [
128     ". 0:0:foo.txt",
129     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
130     "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
131     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
132   ].each do |manifest_text|
133     test "update collection with invalid manifest text #{manifest_text} and expect error" do
134       act_as_system_user do
135         c = create_collection 'foo', Encoding::US_ASCII
136         assert c.valid?
137
138         c.update_attribute 'manifest_text', manifest_text
139         assert !c.valid?
140       end
141     end
142   end
143
144   [
145     nil,
146     "",
147     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
148   ].each do |manifest_text|
149     test "update collection with valid manifest text #{manifest_text.inspect} and expect success" do
150       act_as_system_user do
151         c = create_collection 'foo', Encoding::US_ASCII
152         assert c.valid?
153
154         c.update_attribute 'manifest_text', manifest_text
155         assert c.valid?
156       end
157     end
158   end
159
160   test "auto-create version after idle setting" do
161     Rails.configuration.Collections.CollectionVersioning = true
162     Rails.configuration.Collections.PreserveVersionIfIdle = 600 # 10 minutes
163     act_as_user users(:active) do
164       # Set up initial collection
165       c = create_collection 'foo', Encoding::US_ASCII
166       assert c.valid?
167       assert_equal 1, c.version
168       assert_equal false, c.preserve_version
169       # Make a versionable update, it shouldn't create a new version yet
170       c.update_attributes!({'name' => 'bar'})
171       c.reload
172       assert_equal 'bar', c.name
173       assert_equal 1, c.version
174       # Update modified_at to trigger a version auto-creation
175       fifteen_min_ago = Time.now - 15.minutes
176       c.update_column('modified_at', fifteen_min_ago) # Update without validations/callbacks
177       c.reload
178       assert_equal fifteen_min_ago.to_i, c.modified_at.to_i
179       c.update_attributes!({'name' => 'baz'})
180       c.reload
181       assert_equal 'baz', c.name
182       assert_equal 2, c.version
183       # Make another update, no new version should be created
184       c.update_attributes!({'name' => 'foobar'})
185       c.reload
186       assert_equal 'foobar', c.name
187       assert_equal 2, c.version
188     end
189   end
190
191   test "preserve_version updates" do
192     Rails.configuration.Collections.CollectionVersioning = true
193     Rails.configuration.Collections.PreserveVersionIfIdle = 3600
194     act_as_user users(:active) do
195       # Set up initial collection
196       c = create_collection 'foo', Encoding::US_ASCII
197       assert c.valid?
198       assert_equal 1, c.version
199       assert_equal false, c.preserve_version
200       # This update shouldn't produce a new version, as the idle time is not up
201       c.update_attributes!({
202         'name' => 'bar'
203       })
204       c.reload
205       assert_equal 1, c.version
206       assert_equal 'bar', c.name
207       assert_equal false, c.preserve_version
208       # This update should produce a new version, even if the idle time is not up
209       # and also keep the preserve_version=true flag to persist it.
210       c.update_attributes!({
211         'name' => 'baz',
212         'preserve_version' => true
213       })
214       c.reload
215       assert_equal 2, c.version
216       assert_equal 'baz', c.name
217       assert_equal true, c.preserve_version
218       # Make sure preserve_version is not disabled after being enabled, unless
219       # a new version is created.
220       # This is a non-versionable update
221       c.update_attributes!({
222         'preserve_version' => false,
223         'replication_desired' => 2
224       })
225       c.reload
226       assert_equal 2, c.version
227       assert_equal 2, c.replication_desired
228       assert_equal true, c.preserve_version
229       # This is a versionable update
230       c.update_attributes!({'name' => 'foobar'})
231       c.reload
232       assert_equal 3, c.version
233       assert_equal false, c.preserve_version
234       assert_equal 'foobar', c.name
235       # Flipping only 'preserve_version' to true doesn't create a new version
236       c.update_attributes!({'preserve_version' => true})
237       c.reload
238       assert_equal 3, c.version
239       assert_equal true, c.preserve_version
240     end
241   end
242
243   test "preserve_version updates don't change modified_at timestamp" do
244     act_as_user users(:active) do
245       c = create_collection 'foo', Encoding::US_ASCII
246       assert c.valid?
247       assert_equal false, c.preserve_version
248       modified_at = c.modified_at.to_f
249       c.update_attributes!({'preserve_version' => true})
250       c.reload
251       assert_equal true, c.preserve_version
252       assert_equal modified_at, c.modified_at.to_f,
253         'preserve_version updates should not trigger modified_at changes'
254     end
255   end
256
257   [
258     ['version', 10],
259     ['current_version_uuid', 'zzzzz-4zz18-bv31uwvy3neko21'],
260   ].each do |name, new_value|
261     test "'#{name}' updates on current version collections are not allowed" do
262       act_as_user users(:active) do
263         # Set up initial collection
264         c = create_collection 'foo', Encoding::US_ASCII
265         assert c.valid?
266         assert_equal 1, c.version
267
268         assert_raises(ActiveRecord::RecordInvalid) do
269           c.update_attributes!({
270             name => new_value
271           })
272         end
273       end
274     end
275   end
276
277   test "uuid updates on current version make older versions update their pointers" do
278     Rails.configuration.Collections.CollectionVersioning = true
279     Rails.configuration.Collections.PreserveVersionIfIdle = 0
280     act_as_system_user do
281       # Set up initial collection
282       c = create_collection 'foo', Encoding::US_ASCII
283       assert c.valid?
284       assert_equal 1, c.version
285       # Make changes so that a new version is created
286       c.update_attributes!({'name' => 'bar'})
287       c.reload
288       assert_equal 2, c.version
289       assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
290       new_uuid = 'zzzzz-4zz18-somefakeuuidnow'
291       assert_empty Collection.where(uuid: new_uuid)
292       # Update UUID on current version, check that both collections point to it
293       c.update_attributes!({'uuid' => new_uuid})
294       c.reload
295       assert_equal new_uuid, c.uuid
296       assert_equal 2, Collection.where(current_version_uuid: new_uuid).count
297     end
298   end
299
300   # This test exposes a bug related to JSONB attributes, see #15725.
301   test "recently loaded collection shouldn't list changed attributes" do
302     col = Collection.where("properties != '{}'::jsonb").limit(1).first
303     refute col.properties_changed?, 'Properties field should not be seen as changed'
304   end
305
306   [
307     [
308       true,
309       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
310       {:foo=>:bar, :lst=>[1, 3, 5, 7], :hsh=>{'baz'=>'qux', :foobar=>true, 'hsh'=>{:nested=>true}}, :delete_at=>nil},
311     ],
312     [
313       true,
314       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
315       {'delete_at'=>nil, 'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}},
316     ],
317     [
318       true,
319       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
320       {'delete_at'=>nil, 'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'foobar'=>true, 'hsh'=>{'nested'=>true}, 'baz'=>'qux'}},
321     ],
322     [
323       false,
324       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
325       {'foo'=>'bar', 'lst'=>[1, 3, 5, 42], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
326     ],
327     [
328       false,
329       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
330       {'foo'=>'bar', 'lst'=>[1, 3, 7, 5], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
331     ],
332     [
333       false,
334       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
335       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>false}}, 'delete_at'=>nil},
336     ],
337     [
338       false,
339       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil},
340       {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>1234567890},
341     ],
342   ].each do |should_be_equal, value_1, value_2|
343     test "JSONB properties #{value_1} is#{should_be_equal ? '' : ' not'} equal to #{value_2}" do
344       act_as_user users(:active) do
345         # Set up initial collection
346         c = create_collection 'foo', Encoding::US_ASCII
347         assert c.valid?
348         c.update_attributes!({'properties' => value_1})
349         c.reload
350         assert c.changes.keys.empty?
351         c.properties = value_2
352         if should_be_equal
353           assert c.changes.keys.empty?, "Properties #{value_1.inspect} should be equal to #{value_2.inspect}"
354         else
355           refute c.changes.keys.empty?, "Properties #{value_1.inspect} should not be equal to #{value_2.inspect}"
356         end
357       end
358     end
359   end
360
361   test "older versions' modified_at indicate when they're created" do
362     Rails.configuration.Collections.CollectionVersioning = true
363     Rails.configuration.Collections.PreserveVersionIfIdle = 0
364     act_as_user users(:active) do
365       # Set up initial collection
366       c = create_collection 'foo', Encoding::US_ASCII
367       assert c.valid?
368       original_version_modified_at = c.modified_at.to_f
369       # Make changes so that a new version is created
370       c.update_attributes!({'name' => 'bar'})
371       c.reload
372       assert_equal 2, c.version
373       # Get the old version
374       c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
375       assert_not_nil c_old
376
377       version_creation_datetime = c_old.modified_at.to_f
378       assert_equal c.created_at.to_f, c_old.created_at.to_f
379       assert_equal original_version_modified_at, version_creation_datetime
380
381       # Make update on current version so old version get the attribute synced;
382       # its modified_at should not change.
383       new_replication = 3
384       c.update_attributes!({'replication_desired' => new_replication})
385       c.reload
386       assert_equal new_replication, c.replication_desired
387       c_old.reload
388       assert_equal new_replication, c_old.replication_desired
389       assert_equal version_creation_datetime, c_old.modified_at.to_f
390       assert_operator c.modified_at.to_f, :>, c_old.modified_at.to_f
391     end
392   end
393
394   # Bug #17152 - This test relies on fixtures simulating the problem.
395   test "migration fixing collection versions' modified_at timestamps" do
396     versioned_collection_fixtures = [
397       collections(:w_a_z_file).uuid,
398       collections(:collection_owned_by_active).uuid
399     ]
400     versioned_collection_fixtures.each do |uuid|
401       cols = Collection.where(current_version_uuid: uuid).order(version: :desc)
402       assert_equal cols.size, 2
403       # cols[0] -> head version // cols[1] -> old version
404       assert_operator (cols[0].modified_at.to_f - cols[1].modified_at.to_f), :==, 0
405       assert cols[1].modified_at != cols[1].created_at
406     end
407     fix_collection_versions_timestamps
408     versioned_collection_fixtures.each do |uuid|
409       cols = Collection.where(current_version_uuid: uuid).order(version: :desc)
410       assert_equal cols.size, 2
411       # cols[0] -> head version // cols[1] -> old version
412       assert_operator (cols[0].modified_at.to_f - cols[1].modified_at.to_f), :>, 1
413       assert_operator cols[1].modified_at, :==, cols[1].created_at
414     end
415   end
416
417   test "past versions should not be directly updatable" do
418     Rails.configuration.Collections.CollectionVersioning = true
419     Rails.configuration.Collections.PreserveVersionIfIdle = 0
420     act_as_system_user do
421       # Set up initial collection
422       c = create_collection 'foo', Encoding::US_ASCII
423       assert c.valid?
424       # Make changes so that a new version is created
425       c.update_attributes!({'name' => 'bar'})
426       c.reload
427       assert_equal 2, c.version
428       # Get the old version
429       c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
430       assert_not_nil c_old
431       # With collection versioning still being enabled, try to update
432       c_old.name = 'this was foo'
433       assert c_old.invalid?
434       c_old.reload
435       # Try to fool the validator attempting to make c_old to look like a
436       # current version, it should also fail.
437       c_old.current_version_uuid = c_old.uuid
438       assert c_old.invalid?
439       c_old.reload
440       # Now disable collection versioning, it should behave the same way
441       Rails.configuration.Collections.CollectionVersioning = false
442       c_old.name = 'this was foo'
443       assert c_old.invalid?
444     end
445   end
446
447   [
448     ['owner_uuid', 'zzzzz-tpzed-d9tiejq69daie8f', 'zzzzz-tpzed-xurymjxw79nv3jz'],
449     ['replication_desired', 2, 3],
450     ['storage_classes_desired', ['hot'], ['archive']],
451   ].each do |attr, first_val, second_val|
452     test "sync #{attr} with older versions" do
453       Rails.configuration.Collections.CollectionVersioning = true
454       Rails.configuration.Collections.PreserveVersionIfIdle = 0
455       act_as_system_user do
456         # Set up initial collection
457         c = create_collection 'foo', Encoding::US_ASCII
458         assert c.valid?
459         assert_equal 1, c.version
460         assert_not_equal first_val, c.attributes[attr]
461         # Make changes so that a new version is created and a synced field is
462         # updated on both
463         c.update_attributes!({'name' => 'bar', attr => first_val})
464         c.reload
465         assert_equal 2, c.version
466         assert_equal first_val, c.attributes[attr]
467         assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
468         assert_equal first_val, Collection.where(current_version_uuid: c.uuid, version: 1).first.attributes[attr]
469         # Only make an update on the same synced field & check that the previously
470         # created version also gets it.
471         c.update_attributes!({attr => second_val})
472         c.reload
473         assert_equal 2, c.version
474         assert_equal second_val, c.attributes[attr]
475         assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
476         assert_equal second_val, Collection.where(current_version_uuid: c.uuid, version: 1).first.attributes[attr]
477       end
478     end
479   end
480
481   [
482     [false, 'name', 'bar', false],
483     [false, 'description', 'The quick brown fox jumps over the lazy dog', false],
484     [false, 'properties', {'new_version' => true}, false],
485     [false, 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", false],
486     [true, 'name', 'bar', true],
487     [true, 'description', 'The quick brown fox jumps over the lazy dog', true],
488     [true, 'properties', {'new_version' => true}, true],
489     [true, 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", true],
490     # Non-versionable attribute updates shouldn't create new versions
491     [true, 'replication_desired', 5, false],
492     [false, 'replication_desired', 5, false],
493   ].each do |versioning, attr, val, new_version_expected|
494     test "update #{attr} with versioning #{versioning ? '' : 'not '}enabled should #{new_version_expected ? '' : 'not '}create a new version" do
495       Rails.configuration.Collections.CollectionVersioning = versioning
496       Rails.configuration.Collections.PreserveVersionIfIdle = 0
497       act_as_user users(:active) do
498         # Create initial collection
499         c = create_collection 'foo', Encoding::US_ASCII
500         assert c.valid?
501         assert_equal 'foo', c.name
502
503         # Check current version attributes
504         assert_equal 1, c.version
505         assert_equal c.uuid, c.current_version_uuid
506
507         # Update attribute and check if version number should be incremented
508         old_value = c.attributes[attr]
509         c.update_attributes!({attr => val})
510         assert_equal new_version_expected, c.version == 2
511         assert_equal val, c.attributes[attr]
512
513         if versioning && new_version_expected
514           # Search for the snapshot & previous value
515           assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
516           s = Collection.where(current_version_uuid: c.uuid, version: 1).first
517           assert_not_nil s
518           assert_equal old_value, s.attributes[attr]
519         else
520           # If versioning is disabled or no versionable attribute was updated,
521           # only the current version should exist
522           assert_equal 1, Collection.where(current_version_uuid: c.uuid).count
523           assert_equal c, Collection.where(current_version_uuid: c.uuid).first
524         end
525       end
526     end
527   end
528
529   test 'current_version_uuid is ignored during update' do
530     Rails.configuration.Collections.CollectionVersioning = true
531     Rails.configuration.Collections.PreserveVersionIfIdle = 0
532     act_as_user users(:active) do
533       # Create 1st collection
534       col1 = create_collection 'foo', Encoding::US_ASCII
535       assert col1.valid?
536       assert_equal 1, col1.version
537
538       # Create 2nd collection, update it so it becomes version:2
539       # (to avoid unique index violation)
540       col2 = create_collection 'bar', Encoding::US_ASCII
541       assert col2.valid?
542       assert_equal 1, col2.version
543       col2.update_attributes({name: 'baz'})
544       assert_equal 2, col2.version
545
546       # Try to make col2 a past version of col1. It shouldn't be possible
547       col2.update_attributes({current_version_uuid: col1.uuid})
548       assert col2.invalid?
549       col2.reload
550       assert_not_equal col1.uuid, col2.current_version_uuid
551     end
552   end
553
554   test 'with versioning enabled, simultaneous updates increment version correctly' do
555     Rails.configuration.Collections.CollectionVersioning = true
556     Rails.configuration.Collections.PreserveVersionIfIdle = 0
557     act_as_user users(:active) do
558       # Create initial collection
559       col = create_collection 'foo', Encoding::US_ASCII
560       assert col.valid?
561       assert_equal 1, col.version
562
563       # Simulate simultaneous updates
564       c1 = Collection.where(uuid: col.uuid).first
565       assert_equal 1, c1.version
566       c1.name = 'bar'
567       c2 = Collection.where(uuid: col.uuid).first
568       c2.description = 'foo collection'
569       c1.save!
570       assert_equal 1, c2.version
571       # with_lock forces a reload, so this shouldn't produce an unique violation error
572       c2.save!
573       assert_equal 3, c2.version
574       assert_equal 'foo collection', c2.description
575     end
576   end
577
578   test 'create and update collection and verify file_names' do
579     act_as_system_user do
580       c = create_collection 'foo', Encoding::US_ASCII
581       assert c.valid?
582       created_file_names = c.file_names
583       assert created_file_names
584       assert_match(/foo.txt/, c.file_names)
585
586       c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
587       assert_not_equal created_file_names, c.file_names
588       assert_match(/foo2.txt/, c.file_names)
589     end
590   end
591
592   [
593     [2**8, false],
594     [2**18, true],
595   ].each do |manifest_size, allow_truncate|
596     test "create collection with manifest size #{manifest_size} with allow_truncate=#{allow_truncate},
597           and not expect exceptions even on very large manifest texts" do
598       # file_names has a max size, hence there will be no errors even on large manifests
599       act_as_system_user do
600         manifest_text = ''
601         index = 0
602         while manifest_text.length < manifest_size
603           manifest_text += "./blurfl d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylongfilename000000000000#{index}.txt\n"
604           index += 1
605         end
606         manifest_text += "./laststreamname d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylastfilename.txt\n"
607         c = Collection.create(manifest_text: manifest_text)
608
609         assert c.valid?
610         assert c.file_names
611         assert_match(/veryverylongfilename0000000000001.txt/, c.file_names)
612         assert_match(/veryverylongfilename0000000000002.txt/, c.file_names)
613         if not allow_truncate
614           assert_match(/veryverylastfilename/, c.file_names)
615           assert_match(/laststreamname/, c.file_names)
616         end
617       end
618     end
619   end
620
621   test "full text search for collections" do
622     # file_names column does not get populated when fixtures are loaded, hence setup test data
623     act_as_system_user do
624       Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
625       Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
626       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\n")
627     end
628
629     [
630       ['foo', true],
631       ['foo bar', false],                     # no collection matching both
632       ['foo&bar', false],                     # no collection matching both
633       ['foo|bar', true],                      # works only no spaces between the words
634       ['Gnu public', true],                   # both prefixes found, though not consecutively
635       ['Gnu&public', true],                   # both prefixes found, though not consecutively
636       ['file4', true],                        # prefix match
637       ['file4.txt', true],                    # whole string match
638       ['filex', false],                       # no such prefix
639       ['subdir', true],                       # prefix matches
640       ['subdir2', true],
641       ['subdir2/', true],
642       ['subdir2/subdir3', true],
643       ['subdir2/subdir3/subdir4', true],
644       ['subdir2 file4', true],                # look for both prefixes
645       ['subdir4', false],                     # not a prefix match
646     ].each do |search_filter, expect_results|
647       search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
648       results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
649                                  "#{search_filters}")
650       if expect_results
651         refute_empty results
652       else
653         assert_empty results
654       end
655     end
656   end
657
658   test 'portable data hash with missing size hints' do
659     [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x\n",
660       ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"],
661      [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x\n",
662       ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
663      [". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n",
664       ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
665     ].each do |unportable, portable|
666       c = Collection.new(manifest_text: unportable)
667       assert c.valid?
668       assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
669                    c.portable_data_hash)
670     end
671   end
672
673   pdhmanifest = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"
674   pdhmd5 = Digest::MD5.hexdigest pdhmanifest
675   [[true, nil],
676    [true, pdhmd5],
677    [true, pdhmd5+'+12345'],
678    [true, pdhmd5+'+'+pdhmanifest.length.to_s],
679    [true, pdhmd5+'+12345+Foo'],
680    [true, pdhmd5+'+Foo'],
681    [false, Digest::MD5.hexdigest(pdhmanifest.strip)],
682    [false, Digest::MD5.hexdigest(pdhmanifest.strip)+'+'+pdhmanifest.length.to_s],
683    [false, pdhmd5[0..30]],
684    [false, pdhmd5[0..30]+'z'],
685    [false, pdhmd5[0..24]+'000000000'],
686    [false, pdhmd5[0..24]+'000000000+0']].each do |isvalid, pdh|
687     test "portable_data_hash #{pdh.inspect} valid? == #{isvalid}" do
688       c = Collection.new manifest_text: pdhmanifest, portable_data_hash: pdh
689       assert_equal isvalid, c.valid?, c.errors.full_messages.to_s
690     end
691   end
692
693   test "storage_classes_desired cannot be empty" do
694     act_as_user users(:active) do
695       c = collections(:collection_owned_by_active)
696       c.update_attributes storage_classes_desired: ["hot"]
697       assert_equal ["hot"], c.storage_classes_desired
698       assert_raise ArvadosModel::InvalidStateTransitionError do
699         c.update_attributes storage_classes_desired: []
700       end
701     end
702   end
703
704   test "storage classes lists should only contain non-empty strings" do
705     c = collections(:storage_classes_desired_default_unconfirmed)
706     act_as_user users(:admin) do
707       assert c.update_attributes(storage_classes_desired: ["default", "a_string"],
708                                  storage_classes_confirmed: ["another_string"])
709       [
710         ["storage_classes_desired", ["default", 42]],
711         ["storage_classes_confirmed", [{the_answer: 42}]],
712         ["storage_classes_desired", ["default", ""]],
713         ["storage_classes_confirmed", [""]],
714       ].each do |attr, val|
715         assert_raise ArvadosModel::InvalidStateTransitionError do
716           assert c.update_attributes({attr => val})
717         end
718       end
719     end
720   end
721
722   test "storage_classes_confirmed* can be set by admin user" do
723     c = collections(:storage_classes_desired_default_unconfirmed)
724     act_as_user users(:admin) do
725       assert c.update_attributes(storage_classes_confirmed: ["default"],
726                                  storage_classes_confirmed_at: Time.now)
727     end
728   end
729
730   test "storage_classes_confirmed* cannot be set by non-admin user" do
731     act_as_user users(:active) do
732       c = collections(:storage_classes_desired_default_unconfirmed)
733       # Cannot set just one at a time.
734       assert_raise ArvadosModel::PermissionDeniedError do
735         c.update_attributes storage_classes_confirmed: ["default"]
736       end
737       c.reload
738       assert_raise ArvadosModel::PermissionDeniedError do
739         c.update_attributes storage_classes_confirmed_at: Time.now
740       end
741       # Cannot set bot at once, either.
742       c.reload
743       assert_raise ArvadosModel::PermissionDeniedError do
744         assert c.update_attributes(storage_classes_confirmed: ["default"],
745                                    storage_classes_confirmed_at: Time.now)
746       end
747     end
748   end
749
750   test "storage_classes_confirmed* can be cleared (but only together) by non-admin user" do
751     act_as_user users(:active) do
752       c = collections(:storage_classes_desired_default_confirmed_default)
753       # Cannot clear just one at a time.
754       assert_raise ArvadosModel::PermissionDeniedError do
755         c.update_attributes storage_classes_confirmed: []
756       end
757       c.reload
758       assert_raise ArvadosModel::PermissionDeniedError do
759         c.update_attributes storage_classes_confirmed_at: nil
760       end
761       # Can clear both at once.
762       c.reload
763       assert c.update_attributes(storage_classes_confirmed: [],
764                                  storage_classes_confirmed_at: nil)
765     end
766   end
767
768   [0, 2, 4, nil].each do |ask|
769     test "set replication_desired to #{ask.inspect}" do
770       Rails.configuration.Collections.DefaultReplication = 2
771       act_as_user users(:active) do
772         c = collections(:replication_undesired_unconfirmed)
773         c.update_attributes replication_desired: ask
774         assert_equal ask, c.replication_desired
775       end
776     end
777   end
778
779   test "replication_confirmed* can be set by admin user" do
780     c = collections(:replication_desired_2_unconfirmed)
781     act_as_user users(:admin) do
782       assert c.update_attributes(replication_confirmed: 2,
783                                  replication_confirmed_at: Time.now)
784     end
785   end
786
787   test "replication_confirmed* cannot be set by non-admin user" do
788     act_as_user users(:active) do
789       c = collections(:replication_desired_2_unconfirmed)
790       # Cannot set just one at a time.
791       assert_raise ArvadosModel::PermissionDeniedError do
792         c.update_attributes replication_confirmed: 1
793       end
794       assert_raise ArvadosModel::PermissionDeniedError do
795         c.update_attributes replication_confirmed_at: Time.now
796       end
797       # Cannot set both at once, either.
798       assert_raise ArvadosModel::PermissionDeniedError do
799         c.update_attributes(replication_confirmed: 1,
800                             replication_confirmed_at: Time.now)
801       end
802     end
803   end
804
805   test "replication_confirmed* can be cleared (but only together) by non-admin user" do
806     act_as_user users(:active) do
807       c = collections(:replication_desired_2_confirmed_2)
808       # Cannot clear just one at a time.
809       assert_raise ArvadosModel::PermissionDeniedError do
810         c.update_attributes replication_confirmed: nil
811       end
812       c.reload
813       assert_raise ArvadosModel::PermissionDeniedError do
814         c.update_attributes replication_confirmed_at: nil
815       end
816       # Can clear both at once.
817       c.reload
818       assert c.update_attributes(replication_confirmed: nil,
819                                  replication_confirmed_at: nil)
820     end
821   end
822
823   test "clear replication_confirmed* when introducing a new block in manifest" do
824     c = collections(:replication_desired_2_confirmed_2)
825     act_as_user users(:active) do
826       assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
827       assert_nil c.replication_confirmed
828       assert_nil c.replication_confirmed_at
829     end
830   end
831
832   test "don't clear replication_confirmed* when just renaming a file" do
833     c = collections(:replication_desired_2_confirmed_2)
834     act_as_user users(:active) do
835       new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
836       assert c.update_attributes(manifest_text: new_manifest)
837       assert_equal 2, c.replication_confirmed
838       assert_not_nil c.replication_confirmed_at
839     end
840   end
841
842   test "don't clear replication_confirmed* when just deleting a data block" do
843     c = collections(:replication_desired_2_confirmed_2)
844     act_as_user users(:active) do
845       new_manifest = c.signed_manifest_text
846       new_manifest.sub!(/ \S+:bar/, '')
847       new_manifest.sub!(/ acbd\S+/, '')
848
849       # Confirm that we did just remove a block from the manifest (if
850       # not, this test would pass without testing the relevant case):
851       assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
852
853       assert c.update_attributes(manifest_text: new_manifest)
854       assert_equal 2, c.replication_confirmed
855       assert_not_nil c.replication_confirmed_at
856     end
857   end
858
859   test 'signature expiry does not exceed trash_at' do
860     act_as_user users(:active) do
861       t0 = db_current_time
862       c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n", name: 'foo')
863       c.update_attributes! trash_at: (t0 + 1.hours)
864       c.reload
865       sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
866       assert_operator sig_exp.to_i, :<=, (t0 + 1.hours).to_i
867     end
868   end
869
870   test 'far-future expiry date cannot be used to circumvent configured permission ttl' do
871     act_as_user users(:active) do
872       c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n",
873                              name: 'foo',
874                              trash_at: db_current_time + 1.years)
875       sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
876       expect_max_sig_exp = db_current_time.to_i + Rails.configuration.Collections.BlobSigningTTL.to_i
877       assert_operator c.trash_at.to_i, :>, expect_max_sig_exp
878       assert_operator sig_exp.to_i, :<=, expect_max_sig_exp
879     end
880   end
881
882   test "create collection with properties" do
883     act_as_system_user do
884       c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
885                             properties: {'property_1' => 'value_1'})
886       assert c.valid?
887       assert_equal 'value_1', c.properties['property_1']
888     end
889   end
890
891   test 'create, delete, recreate collection with same name and owner' do
892     act_as_user users(:active) do
893       # create collection with name
894       c = Collection.create(manifest_text: '',
895                             name: "test collection name")
896       assert c.valid?
897       uuid = c.uuid
898
899       c = Collection.readable_by(current_user).where(uuid: uuid)
900       assert_not_empty c, 'Should be able to find live collection'
901
902       # mark collection as expired
903       c.first.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d"))
904       c = Collection.readable_by(current_user).where(uuid: uuid)
905       assert_empty c, 'Should not be able to find expired collection'
906
907       # recreate collection with the same name
908       c = Collection.create(manifest_text: '',
909                             name: "test collection name")
910       assert c.valid?
911     end
912   end
913
914   test 'trash_at cannot be set too far in the past' do
915     act_as_user users(:active) do
916       t0 = db_current_time
917       c = Collection.create!(manifest_text: '', name: 'foo')
918       c.update_attributes! trash_at: (t0 - 2.weeks)
919       c.reload
920       assert_operator c.trash_at, :>, t0
921     end
922   end
923
924   now = Time.now
925   [['trash-to-delete interval negative',
926     :collection_owned_by_active,
927     {trash_at: now+2.weeks, delete_at: now},
928     {state: :invalid}],
929    ['now-to-delete interval short',
930     :collection_owned_by_active,
931     {trash_at: now+3.days, delete_at: now+7.days},
932     {state: :trash_future}],
933    ['now-to-delete interval short, trash=delete',
934     :collection_owned_by_active,
935     {trash_at: now+3.days, delete_at: now+3.days},
936     {state: :trash_future}],
937    ['trash-to-delete interval ok',
938     :collection_owned_by_active,
939     {trash_at: now, delete_at: now+15.days},
940     {state: :trash_now}],
941    ['trash-to-delete interval short, but far enough in future',
942     :collection_owned_by_active,
943     {trash_at: now+13.days, delete_at: now+15.days},
944     {state: :trash_future}],
945    ['trash by setting is_trashed bool',
946     :collection_owned_by_active,
947     {is_trashed: true},
948     {state: :trash_now}],
949    ['trash in future by setting just trash_at',
950     :collection_owned_by_active,
951     {trash_at: now+1.week},
952     {state: :trash_future}],
953    ['trash in future by setting trash_at and delete_at',
954     :collection_owned_by_active,
955     {trash_at: now+1.week, delete_at: now+4.weeks},
956     {state: :trash_future}],
957    ['untrash by clearing is_trashed bool',
958     :expired_collection,
959     {is_trashed: false},
960     {state: :not_trash}],
961   ].each do |test_name, fixture_name, updates, expect|
962     test test_name do
963       act_as_user users(:active) do
964         min_exp = (db_current_time +
965                    Rails.configuration.Collections.BlobSigningTTL)
966         if fixture_name == :expired_collection
967           # Fixture-finder shorthand doesn't find trashed collections
968           # because they're not in the default scope.
969           c = Collection.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih')
970         else
971           c = collections(fixture_name)
972         end
973         updates_ok = c.update_attributes(updates)
974         expect_valid = expect[:state] != :invalid
975         assert_equal expect_valid, updates_ok, c.errors.full_messages.to_s
976         case expect[:state]
977         when :invalid
978           refute c.valid?
979         when :trash_now
980           assert c.is_trashed
981           assert_not_nil c.trash_at
982           assert_operator c.trash_at, :<=, db_current_time
983           assert_not_nil c.delete_at
984           assert_operator c.delete_at, :>=, min_exp
985         when :trash_future
986           refute c.is_trashed
987           assert_not_nil c.trash_at
988           assert_operator c.trash_at, :>, db_current_time
989           assert_not_nil c.delete_at
990           assert_operator c.delete_at, :>=, c.trash_at
991           # Currently this minimum interval is needed to prevent early
992           # garbage collection:
993           assert_operator c.delete_at, :>=, min_exp
994         when :not_trash
995           refute c.is_trashed
996           assert_nil c.trash_at
997           assert_nil c.delete_at
998         else
999           raise "bad expect[:state]==#{expect[:state].inspect} in test case"
1000         end
1001       end
1002     end
1003   end
1004
1005   test 'default trash interval > blob signature ttl' do
1006     Rails.configuration.Collections.DefaultTrashLifetime = 86400 * 21 # 3 weeks
1007     start = db_current_time
1008     act_as_user users(:active) do
1009       c = Collection.create!(manifest_text: '', name: 'foo')
1010       c.update_attributes!(trash_at: start + 86400.seconds)
1011       assert_operator c.delete_at, :>=, start + (86400*22).seconds
1012       assert_operator c.delete_at, :<, start + (86400*22 + 30).seconds
1013       c.destroy
1014
1015       c = Collection.create!(manifest_text: '', name: 'foo')
1016       c.update_attributes!(is_trashed: true)
1017       assert_operator c.delete_at, :>=, start + (86400*21).seconds
1018     end
1019   end
1020
1021   test "find_all_for_docker_image resolves names that look like hashes" do
1022     coll_list = Collection.
1023       find_all_for_docker_image('a' * 64, nil, [users(:active)])
1024     coll_uuids = coll_list.map(&:uuid)
1025     assert_includes(coll_uuids, collections(:docker_image).uuid)
1026   end
1027
1028   test "move collections to trash in SweepTrashedObjects" do
1029     c = collections(:trashed_on_next_sweep)
1030     refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
1031     assert_raises(ActiveRecord::RecordNotUnique) do
1032       act_as_user users(:active) do
1033         Collection.create!(owner_uuid: c.owner_uuid,
1034                            name: c.name)
1035       end
1036     end
1037     SweepTrashedObjects.sweep_now
1038     c = Collection.where('uuid=? and is_trashed=true', c.uuid).first
1039     assert c
1040     act_as_user users(:active) do
1041       assert Collection.create!(owner_uuid: c.owner_uuid,
1042                                 name: c.name)
1043     end
1044   end
1045
1046   test "delete collections in SweepTrashedObjects" do
1047     uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
1048     assert_not_empty Collection.where(uuid: uuid)
1049     SweepTrashedObjects.sweep_now
1050     assert_empty Collection.where(uuid: uuid)
1051   end
1052
1053   test "delete referring links in SweepTrashedObjects" do
1054     uuid = collections(:trashed_on_next_sweep).uuid
1055     act_as_system_user do
1056       assert_raises ActiveRecord::RecordInvalid do
1057         # Cannot create because :trashed_on_next_sweep is already trashed
1058         Link.create!(head_uuid: uuid,
1059                      tail_uuid: system_user_uuid,
1060                      link_class: 'whatever',
1061                      name: 'something')
1062       end
1063
1064       # Bump trash_at to now + 1 minute
1065       Collection.where(uuid: uuid).
1066         update(trash_at: db_current_time + (1).minute)
1067
1068       # Not considered trashed now
1069       Link.create!(head_uuid: uuid,
1070                    tail_uuid: system_user_uuid,
1071                    link_class: 'whatever',
1072                    name: 'something')
1073     end
1074     past = db_current_time
1075     Collection.where(uuid: uuid).
1076       update_all(is_trashed: true, trash_at: past, delete_at: past)
1077     assert_not_empty Collection.where(uuid: uuid)
1078     SweepTrashedObjects.sweep_now
1079     assert_empty Collection.where(uuid: uuid)
1080   end
1081
1082   test "empty names are exempt from name uniqueness" do
1083     act_as_user users(:active) do
1084       c1 = Collection.new(name: nil, manifest_text: '', owner_uuid: groups(:aproject).uuid)
1085       assert c1.save
1086       c2 = Collection.new(name: '', manifest_text: '', owner_uuid: groups(:aproject).uuid)
1087       assert c2.save
1088       c3 = Collection.new(name: '', manifest_text: '', owner_uuid: groups(:aproject).uuid)
1089       assert c3.save
1090       c4 = Collection.new(name: 'c4', manifest_text: '', owner_uuid: groups(:aproject).uuid)
1091       assert c4.save
1092       c5 = Collection.new(name: 'c4', manifest_text: '', owner_uuid: groups(:aproject).uuid)
1093       assert_raises(ActiveRecord::RecordNotUnique) do
1094         c5.save
1095       end
1096     end
1097   end
1098
1099   test "create collections with managed properties" do
1100     Rails.configuration.Collections.ManagedProperties = ConfigLoader.to_OrderedOptions({
1101       'default_prop1' => {'Value' => 'prop1_value'},
1102       'responsible_person_uuid' => {'Function' => 'original_owner'}
1103     })
1104     # Test collection without initial properties
1105     act_as_user users(:active) do
1106       c = create_collection 'foo', Encoding::US_ASCII
1107       assert c.valid?
1108       assert_not_empty c.properties
1109       assert_equal 'prop1_value', c.properties['default_prop1']
1110       assert_equal users(:active).uuid, c.properties['responsible_person_uuid']
1111     end
1112     # Test collection with default_prop1 property already set
1113     act_as_user users(:active) do
1114       c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n",
1115                             properties: {'default_prop1' => 'custom_value'})
1116       assert c.valid?
1117       assert_not_empty c.properties
1118       assert_equal 'custom_value', c.properties['default_prop1']
1119       assert_equal users(:active).uuid, c.properties['responsible_person_uuid']
1120     end
1121     # Test collection inside a sub project
1122     act_as_user users(:active) do
1123       c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n",
1124                             owner_uuid: groups(:asubproject).uuid)
1125       assert c.valid?
1126       assert_not_empty c.properties
1127       assert_equal users(:active).uuid, c.properties['responsible_person_uuid']
1128     end
1129   end
1130
1131   test "update collection with protected managed properties" do
1132     Rails.configuration.Collections.ManagedProperties = ConfigLoader.to_OrderedOptions({
1133       'default_prop1' => {'Value' => 'prop1_value', 'Protected' => true},
1134     })
1135     act_as_user users(:active) do
1136       c = create_collection 'foo', Encoding::US_ASCII
1137       assert c.valid?
1138       assert_not_empty c.properties
1139       assert_equal 'prop1_value', c.properties['default_prop1']
1140       # Add new property
1141       c.properties['prop2'] = 'value2'
1142       c.save!
1143       c.reload
1144       assert_equal 'value2', c.properties['prop2']
1145       # Try to change protected property's value
1146       c.properties['default_prop1'] = 'new_value'
1147       assert_raises(ArvadosModel::PermissionDeniedError) do
1148         c.save!
1149       end
1150       # Admins are allowed to change protected properties
1151       act_as_system_user do
1152         c.properties['default_prop1'] = 'new_value'
1153         c.save!
1154         c.reload
1155         assert_equal 'new_value', c.properties['default_prop1']
1156       end
1157     end
1158   end
1159
1160   test "collection names must be displayable in a filesystem" do
1161     set_user_from_auth :active
1162     ["", "{SOLIDUS}"].each do |subst|
1163       Rails.configuration.Collections.ForwardSlashNameSubstitution = subst
1164       c = Collection.create
1165       [[nil, true],
1166        ["", true],
1167        [".", false],
1168        ["..", false],
1169        ["...", true],
1170        ["..z..", true],
1171        ["foo/bar", subst != ""],
1172        ["../..", subst != ""],
1173        ["/", subst != ""],
1174       ].each do |name, valid|
1175         c.name = name
1176         assert_equal valid, c.valid?, "#{name.inspect} should be #{valid ? "valid" : "invalid"}"
1177       end
1178     end
1179   end
1180 end