replace Timecop with ActiveSupport::Testing::TimeHelpers

resolves #525
This commit is contained in:
Georg Gadinger 2022-07-19 17:18:27 +02:00
parent c5a3f1cc2a
commit 61cea34f5e
7 changed files with 23 additions and 16 deletions

View File

@ -98,7 +98,6 @@ group :development, :test do
gem "simplecov", require: false gem "simplecov", require: false
gem "simplecov-cobertura", require: false gem "simplecov-cobertura", require: false
gem "simplecov-json", require: false gem "simplecov-json", require: false
gem "timecop"
end end
group :production do group :production do

View File

@ -516,7 +516,6 @@ GEM
thor (1.2.1) thor (1.2.1)
thread_safe (0.3.6) thread_safe (0.3.6)
tilt (2.0.10) tilt (2.0.10)
timecop (0.9.5)
turbo-rails (1.1.1) turbo-rails (1.1.1)
actionpack (>= 6.0.0) actionpack (>= 6.0.0)
activejob (>= 6.0.0) activejob (>= 6.0.0)
@ -637,7 +636,6 @@ DEPENDENCIES
simplecov-cobertura simplecov-cobertura
simplecov-json simplecov-json
spring (~> 4.0) spring (~> 4.0)
timecop
turbolinks (~> 2.5.3) turbolinks (~> 2.5.3)
twitter twitter
twitter-text twitter-text

View File

@ -4,6 +4,8 @@
require "rails_helper" require "rails_helper"
describe Ajax::ModerationController, :ajax_controller, type: :controller do describe Ajax::ModerationController, :ajax_controller, type: :controller do
include ActiveSupport::Testing::TimeHelpers
shared_examples "fails when report does not exist" do shared_examples "fails when report does not exist" do
let(:report_id) { "Burgenland" } let(:report_id) { "Burgenland" }
let(:expected_response) do let(:expected_response) do
@ -147,7 +149,7 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do
let(:duration_unit) { 'hours' } let(:duration_unit) { 'hours' }
it "bans the user for 3 hours" do it "bans the user for 3 hours" do
Timecop.freeze do freeze_time do
expect { subject }.to change { target_user.reload.banned? }.from(false).to(true) expect { subject }.to change { target_user.reload.banned? }.from(false).to(true)
expect(target_user.bans.current.first.reason).to eq("just a prank, bro") expect(target_user.bans.current.first.reason).to eq("just a prank, bro")
expect(target_user.bans.current.first.expires_at.to_i).to eq((Time.now.utc + 3.hours).to_i) expect(target_user.bans.current.first.expires_at.to_i).to eq((Time.now.utc + 3.hours).to_i)

View File

@ -3,6 +3,8 @@
require "rails_helper" require "rails_helper"
describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: :controller do describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: :controller do
include ActiveSupport::Testing::TimeHelpers
let(:user) do let(:user) do
FactoryBot.create :user, FactoryBot.create :user,
otp_module: :disabled, otp_module: :disabled,
@ -47,7 +49,7 @@ describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: :
end end
it "shows an error if the user enters the incorrect code" do it "shows an error if the user enters the incorrect code" do
Timecop.freeze(Time.at(1603290888).utc) do travel_to(Time.at(1603290888).utc) do
subject subject
expect(response).to redirect_to :settings_two_factor_authentication_otp_authentication expect(response).to redirect_to :settings_two_factor_authentication_otp_authentication
expect(flash[:error]).to eq("The code you entered was invalid.") expect(flash[:error]).to eq("The code you entered was invalid.")
@ -63,7 +65,7 @@ describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: :
end end
it "enables 2FA for the logged in user and generates recovery keys" do it "enables 2FA for the logged in user and generates recovery keys" do
Timecop.freeze(Time.at(1603290888).utc) do travel_to(Time.at(1603290888).utc) do
subject subject
expect(response).to have_rendered(:recovery_keys) expect(response).to have_rendered(:recovery_keys)
@ -72,7 +74,7 @@ describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: :
end end
it "shows an error if the user attempts to use the code once it has expired" do it "shows an error if the user attempts to use the code once it has expired" do
Timecop.freeze(Time.at(1603290950).utc) do travel_to(Time.at(1603290950).utc) do
subject subject
expect(response).to redirect_to :settings_two_factor_authentication_otp_authentication expect(response).to redirect_to :settings_two_factor_authentication_otp_authentication
expect(flash[:error]).to eq(I18n.t("errors.invalid_otp")) expect(flash[:error]).to eq(I18n.t("errors.invalid_otp"))

View File

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
describe BootstrapHelper, :type => :helper do describe BootstrapHelper, :type => :helper do
include ActiveSupport::Testing::TimeHelpers
describe '#nav_entry' do describe '#nav_entry' do
it 'should return a HTML navigation item which links to a given address' do it 'should return a HTML navigation item which links to a given address' do
allow(self).to receive(:current_page?).and_return(false) allow(self).to receive(:current_page?).and_return(false)
@ -88,9 +90,9 @@ describe BootstrapHelper, :type => :helper do
describe "#time_tooltip" do describe "#time_tooltip" do
it 'should return a tooltip with proper time values' do it 'should return a tooltip with proper time values' do
Timecop.freeze(Time.utc(1984)) do travel_to(Time.utc(1984)) do
@user = FactoryBot.create(:user) @user = FactoryBot.create(:user)
Timecop.travel(Time.now.utc + 10.minutes) travel 10.minutes
expect(time_tooltip(@user)).to eq("<span title=\"Sun, 01 Jan 1984 00:00:00 +0000\" data-toggle=\"tooltip\" data-placement=\"bottom\">10 minutes</span>") expect(time_tooltip(@user)).to eq("<span title=\"Sun, 01 Jan 1984 00:00:00 +0000\" data-toggle=\"tooltip\" data-placement=\"bottom\">10 minutes</span>")
end end

View File

@ -4,6 +4,8 @@ require "rails_helper"
require "exporter" require "exporter"
RSpec.describe Exporter do RSpec.describe Exporter do
include ActiveSupport::Testing::TimeHelpers
let(:user_params) do let(:user_params) do
{ {
answered_count: 144, answered_count: 144,
@ -311,7 +313,7 @@ RSpec.describe Exporter do
subject { instance.send(:publish) } subject { instance.send(:publish) }
it "publishes an archive" do it "publishes an archive" do
Timecop.freeze do freeze_time do
expect { subject }.to change { user.export_processing }.from(true).to(false) expect { subject }.to change { user.export_processing }.from(true).to(false)
expect(File.exist?("#{fake_rails_root}/public/export/#{name}.tar.gz")).to eq(true) expect(File.exist?("#{fake_rails_root}/public/export/#{name}.tar.gz")).to eq(true)
expect(user.export_url).to eq("https://example.com/export/#{name}.tar.gz") expect(user.export_url).to eq("https://example.com/export/#{name}.tar.gz")

View File

@ -3,6 +3,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe(Announcement, type: :model) do RSpec.describe(Announcement, type: :model) do
include ActiveSupport::Testing::TimeHelpers
let!(:user) { FactoryBot.create :user } let!(:user) { FactoryBot.create :user }
let!(:me) do let!(:me) do
Announcement.new( Announcement.new(
@ -19,15 +21,15 @@ RSpec.describe(Announcement, type: :model) do
end end
it "returns false when the current time is before starts_at" do it "returns false when the current time is before starts_at" do
Timecop.freeze(me.starts_at - 1.second) travel_to(me.starts_at - 1.second)
expect(me.active?).to be(false) expect(me.active?).to be(false)
Timecop.return travel_back
end end
it "returns false when the current time is after ends_at" do it "returns false when the current time is after ends_at" do
Timecop.freeze(me.ends_at) travel_to(me.ends_at + 1.second)
expect(me.active?).to be(false) expect(me.active?).to be(false)
Timecop.return travel_back
end end
end end