include KindAndEtag
include CommonApiTemplate
+ before_validation :check_encoding
before_validation :check_signatures
before_validation :strip_manifest_text
before_validation :set_portable_data_hash
true
end
+ def check_encoding
+ if manifest_text.encoding.name == 'UTF-8' and manifest_text.valid_encoding?
+ true
+ else
+ errors.add :manifest_text, "must use UTF-8 encoding"
+ false
+ end
+ end
+
def redundancy_status
if redundancy_confirmed_as.nil?
'unconfirmed'
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"
+ 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