4363: Explicit UTF-8 for test string.
[arvados.git] / services / api / test / unit / collection_test.rb
index 4f73670750e0f2ab0d70425f7d880dd2ab9da7b2..593bd4e3a3866ef3ab091afa5279720b85af9607 100644 (file)
@@ -1,7 +1,41 @@
 require 'test_helper'
 
 class CollectionTest < ActiveSupport::TestCase
-  # test "the truth" do
-  #   assert true
-  # end
+  def create_collection name, enc=nil
+    txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
+    txt.force_encoding(enc) if enc
+    return Collection.create(manifest_text: txt)
+  end
+
+  test 'accept ASCII manifest_text' do
+    act_as_system_user do
+      c = create_collection 'foo'
+      assert c.valid?
+    end
+  end
+
+  test 'accept UTF-8 manifest_text' do
+    act_as_system_user do
+      c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
+      assert c.valid?
+    end
+  end
+
+  test 'refuse manifest_text with invalid UTF-8 byte sequence' do
+    act_as_system_user do
+      c = create_collection "f\xc8o", Encoding::UTF_8
+      assert !c.valid?
+      assert_equal [:manifest_text], c.errors.messages.keys
+      assert_match /UTF-8/, c.errors.messages[:manifest_text].first
+    end
+  end
+
+  test 'refuse manifest_text with non-UTF-8 encoding' do
+    act_as_system_user do
+      c = create_collection "f\xc8o", Encoding::ASCII_8BIT
+      assert !c.valid?
+      assert_equal [:manifest_text], c.errors.messages.keys
+      assert_match /UTF-8/, c.errors.messages[:manifest_text].first
+    end
+  end
 end