Skip to content

Commit

Permalink
Fix DST causing sign in spec to fail (#540)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
robotdana authored May 3, 2024
1 parent 77bf447 commit 86d736f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 3 additions & 0 deletions variants/backend-base/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 7 additions & 9 deletions variants/devise/spec/system/user_sign_in_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 86d736f

Please sign in to comment.