From 61cea34f5eaf8748cd9643a7a33fe31eb4af053a Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Tue, 19 Jul 2022 17:18:27 +0200 Subject: [PATCH] replace Timecop with ActiveSupport::Testing::TimeHelpers resolves #525 --- Gemfile | 1 - Gemfile.lock | 2 -- spec/controllers/ajax/moderation_controller_spec.rb | 4 +++- .../otp_authentication_controller_spec.rb | 8 +++++--- spec/helpers/bootstrap_helper_spec.rb | 8 +++++--- spec/lib/exporter_spec.rb | 4 +++- spec/models/announcement_spec.rb | 12 +++++++----- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Gemfile b/Gemfile index 2a355dbc..51a5fbd6 100644 --- a/Gemfile +++ b/Gemfile @@ -98,7 +98,6 @@ group :development, :test do gem "simplecov", require: false gem "simplecov-cobertura", require: false gem "simplecov-json", require: false - gem "timecop" end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index e54b1c38..4fed0d76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -516,7 +516,6 @@ GEM thor (1.2.1) thread_safe (0.3.6) tilt (2.0.10) - timecop (0.9.5) turbo-rails (1.1.1) actionpack (>= 6.0.0) activejob (>= 6.0.0) @@ -637,7 +636,6 @@ DEPENDENCIES simplecov-cobertura simplecov-json spring (~> 4.0) - timecop turbolinks (~> 2.5.3) twitter twitter-text diff --git a/spec/controllers/ajax/moderation_controller_spec.rb b/spec/controllers/ajax/moderation_controller_spec.rb index ef7ab7f2..97462e81 100644 --- a/spec/controllers/ajax/moderation_controller_spec.rb +++ b/spec/controllers/ajax/moderation_controller_spec.rb @@ -4,6 +4,8 @@ require "rails_helper" describe Ajax::ModerationController, :ajax_controller, type: :controller do + include ActiveSupport::Testing::TimeHelpers + shared_examples "fails when report does not exist" do let(:report_id) { "Burgenland" } let(:expected_response) do @@ -147,7 +149,7 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do let(:duration_unit) { 'hours' } 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(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) diff --git a/spec/controllers/settings/two_factor_authentication/otp_authentication_controller_spec.rb b/spec/controllers/settings/two_factor_authentication/otp_authentication_controller_spec.rb index 809446e1..270c255d 100644 --- a/spec/controllers/settings/two_factor_authentication/otp_authentication_controller_spec.rb +++ b/spec/controllers/settings/two_factor_authentication/otp_authentication_controller_spec.rb @@ -3,6 +3,8 @@ require "rails_helper" describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: :controller do + include ActiveSupport::Testing::TimeHelpers + let(:user) do FactoryBot.create :user, otp_module: :disabled, @@ -47,7 +49,7 @@ describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: : end 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 expect(response).to redirect_to :settings_two_factor_authentication_otp_authentication expect(flash[:error]).to eq("The code you entered was invalid.") @@ -63,7 +65,7 @@ describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: : end 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 expect(response).to have_rendered(:recovery_keys) @@ -72,7 +74,7 @@ describe Settings::TwoFactorAuthentication::OtpAuthenticationController, type: : end 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 expect(response).to redirect_to :settings_two_factor_authentication_otp_authentication expect(flash[:error]).to eq(I18n.t("errors.invalid_otp")) diff --git a/spec/helpers/bootstrap_helper_spec.rb b/spec/helpers/bootstrap_helper_spec.rb index 61620716..b415b851 100644 --- a/spec/helpers/bootstrap_helper_spec.rb +++ b/spec/helpers/bootstrap_helper_spec.rb @@ -1,6 +1,8 @@ require "rails_helper" describe BootstrapHelper, :type => :helper do + include ActiveSupport::Testing::TimeHelpers + describe '#nav_entry' do it 'should return a HTML navigation item which links to a given address' do allow(self).to receive(:current_page?).and_return(false) @@ -88,9 +90,9 @@ describe BootstrapHelper, :type => :helper do describe "#time_tooltip" 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) - Timecop.travel(Time.now.utc + 10.minutes) + travel 10.minutes expect(time_tooltip(@user)).to eq("10 minutes") end @@ -102,4 +104,4 @@ describe BootstrapHelper, :type => :helper do expect(hidespan("Hidden Text", "d-none")).to eq("Hidden Text") end end -end \ No newline at end of file +end diff --git a/spec/lib/exporter_spec.rb b/spec/lib/exporter_spec.rb index fb8bc0a2..65f0dccb 100644 --- a/spec/lib/exporter_spec.rb +++ b/spec/lib/exporter_spec.rb @@ -4,6 +4,8 @@ require "rails_helper" require "exporter" RSpec.describe Exporter do + include ActiveSupport::Testing::TimeHelpers + let(:user_params) do { answered_count: 144, @@ -311,7 +313,7 @@ RSpec.describe Exporter do subject { instance.send(:publish) } it "publishes an archive" do - Timecop.freeze do + freeze_time do 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(user.export_url).to eq("https://example.com/export/#{name}.tar.gz") diff --git a/spec/models/announcement_spec.rb b/spec/models/announcement_spec.rb index 35a76d78..e25ea2ba 100644 --- a/spec/models/announcement_spec.rb +++ b/spec/models/announcement_spec.rb @@ -3,6 +3,8 @@ require "rails_helper" RSpec.describe(Announcement, type: :model) do + include ActiveSupport::Testing::TimeHelpers + let!(:user) { FactoryBot.create :user } let!(:me) do Announcement.new( @@ -19,15 +21,15 @@ RSpec.describe(Announcement, type: :model) do end 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) - Timecop.return + travel_back end 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) - Timecop.return + travel_back end end @@ -44,4 +46,4 @@ RSpec.describe(Announcement, type: :model) do expect(me.link_present?).to be(false) end end -end \ No newline at end of file +end