before_validation :strip_signatures_and_update_replication_confirmed
validate :ensure_pdh_matches_manifest_text
before_save :set_file_names
+ before_save :expires_at_not_in_past
# Query only undeleted collections by default.
- default_scope where("expires_at IS NULL or expires_at > CURRENT_TIMESTAMP")
+ default_scope where("expires_at IS NULL or expires_at > statement_timestamp()")
api_accessible :user, extend: :common do |t|
t.add :name
end
super
end
+
+ # If expires_at is being changed to a time in the past, change it to
+ # now. This allows clients to say "expires {client-current-time}"
+ # without failing due to clock skew, while avoiding odd log entries
+ # like "expiry date changed to {1 year ago}".
+ def expires_at_not_in_past
+ if expires_at_changed? and expires_at
+ self.expires_at = [db_current_time, expires_at].max
+ end
+ end
end
require 'test_helper'
class CollectionTest < ActiveSupport::TestCase
+ include DbCurrentTime
+
def create_collection name, enc=nil
txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
txt.force_encoding(enc) if enc
coll_uuids = coll_list.map(&:uuid)
assert_includes(coll_uuids, collections(:docker_image).uuid)
end
+
+ test 'expires_at cannot be set too far in the past' do
+ act_as_user users(:active) do
+ t0 = db_current_time
+ c = Collection.create!(manifest_text: '', name: 'foo')
+ c.update_attributes! expires_at: (t0 - 2.weeks)
+ c.reload
+ assert_operator c.expires_at, :>, t0
+ end
+ end
end