13561: Fix validation & update related test.
authorLucas Di Pentima <ldipentima@veritasgenetics.com>
Thu, 11 Oct 2018 14:09:37 +0000 (11:09 -0300)
committerLucas Di Pentima <ldipentima@veritasgenetics.com>
Thu, 11 Oct 2018 14:09:37 +0000 (11:09 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <ldipentima@veritasgenetics.com>

services/api/app/models/collection.rb
services/api/test/unit/collection_test.rb

index 4163d314afbea102fa61c3d037ca2856e2c9719d..1cf0a401ce380de06ed8ffd8f1b8fd69edf96e11 100644 (file)
@@ -27,7 +27,7 @@ class Collection < ArvadosModel
   validate :ensure_pdh_matches_manifest_text
   validate :ensure_storage_classes_desired_is_not_empty
   validate :ensure_storage_classes_contain_non_empty_strings
-  validate :old_versions_cannot_be_updated, on: :update
+  validate :past_versions_cannot_be_updated, on: :update
   before_save :set_file_names
   around_update :prepare_for_versioning
   after_update :save_old_version, if: Proc.new { |c| c.should_preserve_version? }
@@ -622,11 +622,12 @@ class Collection < ArvadosModel
     end
   end
 
-  def old_versions_cannot_be_updated
+  def past_versions_cannot_be_updated
     # We check for the '_was' values just in case the update operation
     # includes a change on current_version_uuid or uuid.
     if current_version_uuid_was != uuid_was
-      raise ArvadosModel::PermissionDeniedError.new("previous versions cannot be updated")
+      errors.add(:base, "past versions cannot be updated")
+      false
     end
   end
 
index b0c225dda6f0a92310f5dab45bd292d5522677f4..d8cb469d67cc5ca593a018c5817c2807791bc25f 100644 (file)
@@ -245,25 +245,18 @@ class CollectionTest < ActiveSupport::TestCase
       c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
       assert_not_nil c_old
       # With collection versioning still being enabled, try to update
-      assert_raises ArvadosModel::PermissionDeniedError do
-        c_old.update_attributes(name: 'this was foo')
-      end
+      c_old.name = 'this was foo'
+      assert c_old.invalid?
       c_old.reload
-      assert_equal 'foo', c_old.name
       # Try to fool the validator attempting to make c_old to look like a
       # current version, it should also fail.
-      assert_raises ArvadosModel::PermissionDeniedError do
-        c_old.update_attributes(current_version_uuid: c_old.uuid)
-      end
+      c_old.current_version_uuid = c_old.uuid
+      assert c_old.invalid?
       c_old.reload
-      assert_equal c.uuid, c_old.current_version_uuid
       # Now disable collection versioning, it should behave the same way
       Rails.configuration.collection_versioning = false
-      assert_raises ArvadosModel::PermissionDeniedError do
-        c_old.update_attributes(name: 'this was foo')
-      end
-      c_old.reload
-      assert_equal 'foo', c_old.name
+      c_old.name = 'this was foo'
+      assert c_old.invalid?
     end
   end