Files
openfoodnetwork/spec/models/terms_of_service_file_spec.rb
Maikel Linke 94b75540e4 Replace Timecop with Rails' time helpers
Rails 4.1 added time helpers but we never bothered using them. But now
I'm getting rid of the Timecop dependency and use standard helpers.

Beware though that the new helpers always freeze time. When you travel
to a certain date then the clock stops ticking while Timecop maintained
the passing of time.

The freezing of time could cause problems if you are trying to enforce a
timeout. But all current specs don't seem affected.

In most cases, the freezing will make it easier to avoid flaky specs.
2025-08-22 16:57:04 +10:00

58 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
RSpec.describe TermsOfServiceFile do
include FileHelper
let(:upload) { terms_pdf_file }
describe ".current" do
it "returns nil" do
expect(TermsOfServiceFile.current).to be_nil
end
it "returns the last one" do
existing = [
TermsOfServiceFile.create!(attachment: upload),
TermsOfServiceFile.create!(attachment: upload),
]
expect(TermsOfServiceFile.current).to eq existing.last
end
end
describe ".current_url" do
let(:subject) { TermsOfServiceFile.current_url }
it "points to nil if not ToS file is present" do
expect(subject).to eq nil
end
it "points to a stored file" do
file = TermsOfServiceFile.create!(attachment: upload)
expect(subject).to match /active_storage.*Terms-of-service\.pdf$/
end
end
describe ".updated_at" do
let(:subject) { TermsOfServiceFile.updated_at }
it "gives the most conservative time if not known" do
freeze_time do
expect(subject).to eq Time.zone.now
end
end
it "returns the time when the terms were last updated" do
update_time = 1.day.ago
file = TermsOfServiceFile.create!(attachment: upload)
file.update(updated_at: update_time)
# The database isn't as precise as Ruby's time and rounds.
expect(subject).to be_within(0.001).of(update_time)
end
end
end