X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/90209af8fa35bc99c9821db0c815404d1234ef31..07aa5aa282a388fc1bcb3d0cfe7520b96b3e7e38:/services/api/test/unit/collection_test.rb diff --git a/services/api/test/unit/collection_test.rb b/services/api/test/unit/collection_test.rb index 882e26059c..d425bc63c0 100644 --- a/services/api/test/unit/collection_test.rb +++ b/services/api/test/unit/collection_test.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'test_helper' require 'sweep_trashed_collections' @@ -217,6 +221,81 @@ class CollectionTest < ActiveSupport::TestCase end end + test "storage_classes_desired cannot be empty" do + act_as_user users(:active) do + c = collections(:collection_owned_by_active) + c.update_attributes storage_classes_desired: ["hot"] + assert_equal ["hot"], c.storage_classes_desired + assert_raise ArvadosModel::InvalidStateTransitionError do + c.update_attributes storage_classes_desired: [] + end + end + end + + test "storage classes lists should only contain non-empty strings" do + c = collections(:storage_classes_desired_default_unconfirmed) + act_as_user users(:admin) do + assert c.update_attributes(storage_classes_desired: ["default", "a_string"], + storage_classes_confirmed: ["another_string"]) + [ + ["storage_classes_desired", ["default", 42]], + ["storage_classes_confirmed", [{the_answer: 42}]], + ["storage_classes_desired", ["default", ""]], + ["storage_classes_confirmed", [""]], + ].each do |attr, val| + assert_raise ArvadosModel::InvalidStateTransitionError do + assert c.update_attributes({attr => val}) + end + end + end + end + + test "storage_classes_confirmed* can be set by admin user" do + c = collections(:storage_classes_desired_default_unconfirmed) + act_as_user users(:admin) do + assert c.update_attributes(storage_classes_confirmed: ["default"], + storage_classes_confirmed_at: Time.now) + end + end + + test "storage_classes_confirmed* cannot be set by non-admin user" do + act_as_user users(:active) do + c = collections(:storage_classes_desired_default_unconfirmed) + # Cannot set just one at a time. + assert_raise ArvadosModel::PermissionDeniedError do + c.update_attributes storage_classes_confirmed: ["default"] + end + c.reload + assert_raise ArvadosModel::PermissionDeniedError do + c.update_attributes storage_classes_confirmed_at: Time.now + end + # Cannot set bot at once, either. + c.reload + assert_raise ArvadosModel::PermissionDeniedError do + assert c.update_attributes(storage_classes_confirmed: ["default"], + storage_classes_confirmed_at: Time.now) + end + end + end + + test "storage_classes_confirmed* can be cleared (but only together) by non-admin user" do + act_as_user users(:active) do + c = collections(:storage_classes_desired_default_confirmed_default) + # Cannot clear just one at a time. + assert_raise ArvadosModel::PermissionDeniedError do + c.update_attributes storage_classes_confirmed: [] + end + c.reload + assert_raise ArvadosModel::PermissionDeniedError do + c.update_attributes storage_classes_confirmed_at: nil + end + # Can clear both at once. + c.reload + assert c.update_attributes(storage_classes_confirmed: [], + storage_classes_confirmed_at: nil) + end + end + [0, 2, 4, nil].each do |ask| test "set replication_desired to #{ask.inspect}" do Rails.configuration.default_collection_replication = 2 @@ -348,9 +427,12 @@ class CollectionTest < ActiveSupport::TestCase assert c.valid? uuid = c.uuid + c = Collection.readable_by(current_user).where(uuid: uuid) + assert_not_empty c, 'Should be able to find live collection' + # mark collection as expired - c.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d")) - c = Collection.where(uuid: uuid) + c.first.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d")) + c = Collection.readable_by(current_user).where(uuid: uuid) assert_empty c, 'Should not be able to find expired collection' # recreate collection with the same name @@ -415,7 +497,7 @@ class CollectionTest < ActiveSupport::TestCase if fixture_name == :expired_collection # Fixture-finder shorthand doesn't find trashed collections # because they're not in the default scope. - c = Collection.unscoped.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih') + c = Collection.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih') else c = collections(fixture_name) end @@ -484,7 +566,7 @@ class CollectionTest < ActiveSupport::TestCase end end SweepTrashedCollections.sweep_now - c = Collection.unscoped.where('uuid=? and is_trashed=true', c.uuid).first + c = Collection.where('uuid=? and is_trashed=true', c.uuid).first assert c act_as_user users(:active) do assert Collection.create!(owner_uuid: c.owner_uuid, @@ -494,9 +576,9 @@ class CollectionTest < ActiveSupport::TestCase test "delete in SweepTrashedCollections" do uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep - assert_not_empty Collection.unscoped.where(uuid: uuid) + assert_not_empty Collection.where(uuid: uuid) SweepTrashedCollections.sweep_now - assert_empty Collection.unscoped.where(uuid: uuid) + assert_empty Collection.where(uuid: uuid) end test "delete referring links in SweepTrashedCollections" do @@ -508,10 +590,10 @@ class CollectionTest < ActiveSupport::TestCase name: 'something') end past = db_current_time - Collection.unscoped.where(uuid: uuid). + Collection.where(uuid: uuid). update_all(is_trashed: true, trash_at: past, delete_at: past) - assert_not_empty Collection.unscoped.where(uuid: uuid) + assert_not_empty Collection.where(uuid: uuid) SweepTrashedCollections.sweep_now - assert_empty Collection.unscoped.where(uuid: uuid) + assert_empty Collection.where(uuid: uuid) end end