Merge branch '3381-job-progress-bar-bug' closes #3381
[arvados.git] / services / api / test / unit / arvados_model_test.rb
index a0c132759ab399bacbbb5951a7d458834c3173c6..e47aa3b8921fc067a8ea5d24c0b7f59eaa231853 100644 (file)
@@ -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]}},
@@ -51,11 +69,21 @@ class ArvadosModelTest < ActiveSupport::TestCase
   end
 
   test "Stringify symbols coming from serialized attribute in database" do
+    set_user_from_auth :admin_trustedclient
     fixed = Link.find_by_uuid(links(:has_symbol_keys_in_database_somehow).uuid)
     assert_equal(["baz", "foo"], fixed.properties.keys.sort,
                  "Hash symbol keys from DB did not get stringified.")
     assert_equal(['waz', 'waz', 'waz', 1, nil, false, true],
                  fixed.properties['baz'],
                  "Array symbol values from DB did not get stringified.")
+    assert_equal true, fixed.save, "Failed to save fixed model back to db."
+  end
+
+  test "No HashWithIndifferentAccess in database" do
+    set_user_from_auth :admin_trustedclient
+    assert_raises ActiveRecord::RecordInvalid do
+      Link.create!(link_class: 'test',
+                   properties: {'foo' => 'bar'}.with_indifferent_access)
+    end
   end
 end