X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/83304d43227dce877cc86d87f8f25463a4fac096..964ab3dd90ff1508efc0c77378cde2b3a4da1029:/services/api/test/unit/arvados_model_test.rb diff --git a/services/api/test/unit/arvados_model_test.rb b/services/api/test/unit/arvados_model_test.rb index f7db88c7b8..8c75764789 100644 --- a/services/api/test/unit/arvados_model_test.rb +++ b/services/api/test/unit/arvados_model_test.rb @@ -12,8 +12,7 @@ class ArvadosModelTest < ActiveSupport::TestCase set_user_from_auth :active_trustedclient want_uuid = Specimen.generate_uuid a = create_with_attrs(uuid: want_uuid) - assert_not_equal want_uuid, a.uuid, "Non-admin should not assign uuid." - assert a.uuid.length==27, "Auto assigned uuid length is wrong." + assert_nil a, "Non-admin should not assign uuid." end test 'admin can assign valid uuid' do @@ -24,14 +23,33 @@ class ArvadosModelTest < ActiveSupport::TestCase assert a.uuid.length==27, "Auto assigned uuid length is wrong." end + test 'admin cannot assign uuid with wrong object type' do + set_user_from_auth :admin_trustedclient + want_uuid = Human.generate_uuid + a = create_with_attrs(uuid: want_uuid) + assert_nil a, "Admin should not be able to assign invalid uuid." + end + + test 'admin cannot assign badly formed uuid' do + set_user_from_auth :admin_trustedclient + a = create_with_attrs(uuid: "ntoheunthaoesunhasoeuhtnsaoeunhtsth") + assert_nil a, "Admin should not be able to assign invalid uuid." + end + test 'admin cannot assign empty uuid' do set_user_from_auth :admin_trustedclient a = create_with_attrs(uuid: "") - assert_not_equal "", a.uuid, "Admin should not assign empty uuid." - assert a.uuid.length==27, "Auto assigned uuid length is wrong." + assert_nil a, "Admin cannot assign empty uuid." end [ {:a => 'foo'}, + {'a' => :foo}, + {:a => ['foo', 'bar']}, + {'a' => [:foo, 'bar']}, + {'a' => ['foo', :bar]}, + {:a => [:foo, :bar]}, + {:a => {'foo' => {'bar' => 'baz'}}}, + {'a' => {:foo => {'bar' => 'baz'}}}, {'a' => {'foo' => {:bar => 'baz'}}}, {'a' => {'foo' => {'bar' => :baz}}}, {'a' => {'foo' => ['bar', :baz]}}, @@ -68,4 +86,27 @@ class ArvadosModelTest < ActiveSupport::TestCase properties: {'foo' => 'bar'}.with_indifferent_access) end end + + [['uuid', {unique: true}], + ['owner_uuid', {}]].each do |the_column, requires| + test "unique index on all models with #{the_column}" do + checked = 0 + ActiveRecord::Base.connection.tables.each do |table| + columns = ActiveRecord::Base.connection.columns(table) + + next unless columns.collect(&:name).include? the_column + + indexes = ActiveRecord::Base.connection.indexes(table).reject do |index| + requires.map do |key, val| + index.send(key) == val + end.include? false + end + assert_includes indexes.collect(&:columns), [the_column], 'no index' + checked += 1 + end + # Sanity check: make sure we didn't just systematically miss everything. + assert_operator(10, :<, checked, + "Only #{checked} tables have a #{the_column}?!") + end + end end