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', Encoding::US_ASCII
+ 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