From 86d736f30eb8647596b7c3b669d513f9fc4aefd3 Mon Sep 17 00:00:00 2001 From: Dana Sherson Date: Fri, 3 May 2024 12:27:58 +1200 Subject: [PATCH] Fix DST causing sign in spec to fail (#540) The issue seems to be that within the next 2 weeks is a dst change so we aren't 14 days away, we're 14 days +- 1 hour away (i can't be bothered figuring out which direction currently), which when rounded to days at utc was sometimes out by a day by keeping the comparison to datetimes rather than a raw number of days or seconds, then ruby/rails can keep track of the impact of dst and things will just work i've included the time helpers setup in base rather than in just the devise variant because i think it's broadly useful and its provided by rails we might as well make it accessible to rspec --- variants/backend-base/spec/rails_helper.rb | 3 +++ .../spec/system/user_sign_in_feature_spec.rb | 16 +++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/variants/backend-base/spec/rails_helper.rb b/variants/backend-base/spec/rails_helper.rb index 71ba97c1..212e4296 100644 --- a/variants/backend-base/spec/rails_helper.rb +++ b/variants/backend-base/spec/rails_helper.rb @@ -91,4 +91,7 @@ config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") + + config.include ActiveSupport::Testing::TimeHelpers + config.after { travel_back } end diff --git a/variants/devise/spec/system/user_sign_in_feature_spec.rb b/variants/devise/spec/system/user_sign_in_feature_spec.rb index 7e4f0175..5ea51245 100644 --- a/variants/devise/spec/system/user_sign_in_feature_spec.rb +++ b/variants/devise/spec/system/user_sign_in_feature_spec.rb @@ -44,6 +44,8 @@ context "when a user ticks the 'remember me' box on the sign-in page" do it "user gets a new cookie which allows their login to persist for two weeks" do + freeze_time + # when we fill in the sign-in form **and** check the "Remember me" box fill_in "Email", with: email fill_in "Password", with: password @@ -67,16 +69,12 @@ # Parsing the signature can fail because it is not base64 encoded so can add extra bytes to the # parsed message remember_me_cookie_data, _remember_me_cookie_signature = remember_me_cookie.split("--", 2) - remember_me_cookie_expiry_date = JSON - .parse(Base64.decode64(remember_me_cookie_data)) - .dig("_rails", "exp") - .to_date - - # The remember_me cookie expiry is in UTC timezone so we need to - # compare it with the date as it is right now in UTC (not in NZ) - today_in_utc = Time.current.utc.to_date + remember_me_cookie_expiry = Time.zone.parse( + JSON.parse(Base64.decode64(remember_me_cookie_data)) + .dig("_rails", "exp") + ) - expect(Integer(remember_me_cookie_expiry_date - today_in_utc)).to eq(14) + expect(remember_me_cookie_expiry).to eq 2.weeks.from_now # when we click on the sign out link click_on "Sign out"