Add unique and valid name constraints.
authorTom Clegg <tom@curoverse.com>
Thu, 1 May 2014 22:01:37 +0000 (18:01 -0400)
committerTom Clegg <tom@curoverse.com>
Thu, 1 May 2014 22:06:11 +0000 (18:06 -0400)
services/api/app/models/link.rb
services/api/test/functional/arvados/v1/links_controller_test.rb
services/api/test/unit/link_test.rb

index 26e7183be385dba38da43b85eec02d6649311c6d..37b4c899f9fdaa97e9c41b7f92ace611befd833f 100644 (file)
@@ -9,6 +9,7 @@ class Link < ArvadosModel
   after_create :maybe_invalidate_permissions_cache
   after_destroy :maybe_invalidate_permissions_cache
   attr_accessor :head_kind, :tail_kind
+  validate :name_link_has_valid_name
 
   api_accessible :user, extend: :common do |t|
     t.add :tail_uuid
@@ -81,4 +82,14 @@ class Link < ArvadosModel
       User.invalidate_permissions_cache
     end
   end
+
+  def name_link_has_valid_name
+    if link_class == 'name'
+      unless name.is_a? String and !name.empty?
+        errors.add('name', 'must be a non-empty string')
+      end
+    else
+      true
+    end
+  end
 end
index 804e1bdfdb1260abe075ceecba6710d84e03050a..dfce78b13f7f79c4f5927bbbda749882758071c6 100644 (file)
@@ -270,4 +270,17 @@ class Arvados::V1::LinksControllerTest < ActionController::TestCase
     assert_response :success
   end
 
+  test "refuse duplicate name" do
+    the_name = links(:job_name_in_afolder).name
+    the_folder = links(:job_name_in_afolder).tail_uuid
+    authorize_with :active
+    post :create, link: {
+      tail_uuid: the_folder,
+      head_uuid: specimens(:owned_by_active_user).uuid,
+      link_class: 'name',
+      name: the_name,
+      properties: {this_s: "a duplicate name"}
+    }
+    assert_response 422
+  end
 end
index 944bfced3d30423ff2b83e0d6cf7c134ce1f8e88..72d6017ce7ac9c6beac74fe67579c4a35034290e 100644 (file)
@@ -1,7 +1,48 @@
 require 'test_helper'
 
 class LinkTest < ActiveSupport::TestCase
-  # test "the truth" do
-  #   assert true
-  # end
+  fixtures :all
+
+  setup do
+    Thread.current[:user] = users(:active)
+  end
+
+  test 'name links with the same tail_uuid must be unique' do
+    a = Link.create!(tail_uuid: groups(:afolder).uuid,
+                     head_uuid: specimens(:owned_by_active_user).uuid,
+                     link_class: 'name',
+                     name: 'foo')
+    assert a.valid?, a.errors.to_s
+    assert_raises ActiveRecord::RecordNotUnique do
+      b = Link.create!(tail_uuid: groups(:afolder).uuid,
+                       head_uuid: specimens(:owned_by_active_user).uuid,
+                       link_class: 'name',
+                       name: 'foo')
+    end
+  end
+
+  test 'name links with different tail_uuid need not be unique' do
+    a = Link.create!(tail_uuid: groups(:afolder).uuid,
+                     head_uuid: specimens(:owned_by_active_user).uuid,
+                     link_class: 'name',
+                     name: 'foo')
+    assert a.valid?, a.errors.to_s
+    b = Link.create!(tail_uuid: groups(:asubfolder).uuid,
+                     head_uuid: specimens(:owned_by_active_user).uuid,
+                     link_class: 'name',
+                     name: 'foo')
+    assert b.valid?, b.errors.to_s
+    assert_not_equal(a.uuid, b.uuid,
+                     "created two links and got the same uuid back.")
+  end
+
+  [nil, '', false].each do |name|
+    test "name links cannot have name=#{name.inspect}" do
+      a = Link.create(tail_uuid: groups(:afolder).uuid,
+                      head_uuid: specimens(:owned_by_active_user).uuid,
+                      link_class: 'name',
+                      name: name)
+      assert a.invalid?, "invalid name was accepted as valid?"
+    end
+  end
 end