RSpec system tests: Reset Password Token is invalid

23 views Asked by At

I've been banging my head against this one for the past 90 minutes.

I have a Ruby on Rails application using:

gem 'rails', '~> 7.1.2'
gem 'devise', '~> 4.9'
gem 'rspec-rails', '~> 6.1.0'
gem 'capybara', '~> 3.39'
gem 'selenium-webdriver', '~> 4.16'

I just cannot get my system specs to pass for the "Reset Password" function of Devise. They keep failing because I'm getting Reset Password Token is invalid. Do note that the process works perfectly if done manually on my Development environment.

Here's my spec:

require 'rails_helper'

RSpec.describe 'Password Reset', type: :system do
  let(:user) { create(:user) }

  it 'allows a user to recover their password' do
    visit new_user_password_path

    fill_in User.human_attribute_name(:email), with: user.email
    click_button I18n.t('devise.passwords.send_email_cta_button')

    expect(page).to have_content(I18n.t('devise.passwords.send_instructions'))
    expect(ActionMailer::Base.deliveries.count).to eq(1)

    email = ActionMailer::Base.deliveries.last
    html = Nokogiri::HTML(email.body.to_s)
    target_url = html.at("a:contains('#{I18n.t('devise.passwords.email.link_cta')}')")['href']

    visit target_url

    fill_in I18n.t('devise.passwords.new_password'), with: 'new_password'
    fill_in I18n.t('devise.passwords.new_password_confirmation'), with: 'new_password'
    click_button I18n.t('devise.passwords.update_cta')

    expect(page).to have_content(I18n.t('devise.passwords.updated'))
  end
end

I've also tried just navigating to the edit_password_path by grabbing the token from the User record, and also reloading the record before I do it. I've also tried to set the hidden user_reset_password_token form field manually to match the token currently in the database... all to no avail.

Any ideas on why this could be happening?

0

There are 0 answers