require "rails_helper" # Automatically generated by the orthodox gem (https://github.com/katanacode/orthodox) # (c) Copyright 2019 Katana Code Ltd. All Rights Reserved. RSpec.describe "<%= plural_class_name %>::PasswordResets", type: :system do include ActiveSupport::Testing::TimeHelpers let(:<%= singular_name %>) { create(:<%= singular_name %>) } scenario "<%= class_name %> resets password successfully" do visit new_<%= plural_name %>_session_path click_link "Forgot password" fill_in :<%= singular_name %>_email, with: <%= singular_name %>.email click_button "Send" expect(current_path).to eql(new_<%= plural_name %>_session_path) expect(enqueued_jobs) expect(last_mailer_job[:args][0..1]).to eql(["<%= class_name %>Mailer", "password_reset_link"]) visit(edit_<%= plural_name %>_password_reset_path(token: <%= singular_name %>.password_reset_token.secret)) fill_in(:<%= singular_name %>_password, with: "new-password") fill_in(:<%= singular_name %>_password_confirmation, with: "new-password") click_button("Save") expect(current_path).to eql(new_<%= plural_name %>_session_path) expect(<%= singular_name %>.reload.authenticate("new-password")).to eql(<%= singular_name %>) end scenario "<%= class_name %> clicks link once already used" do visit new_<%= plural_name %>_session_path click_link "Forgot password" fill_in :<%= singular_name %>_email, with: <%= singular_name %>.email click_button "Send" expect(current_path).to eql(new_<%= plural_name %>_session_path) expect(enqueued_jobs) expect(last_mailer_job[:args][0..1]).to eql(["<%= class_name %>Mailer", "password_reset_link"]) visit(edit_<%= plural_name %>_password_reset_path(token: <%= singular_name %>.password_reset_token.secret)) fill_in(:<%= singular_name %>_password, with: "new-password") fill_in(:<%= singular_name %>_password_confirmation, with: "new-password") click_button("Save") expect(current_path).to eql(new_<%= plural_name %>_session_path) visit(edit_<%= plural_name %>_password_reset_path(token: <%= singular_name %>.password_reset_token.secret)) expect(current_path).to eql(new_<%= plural_name %>_session_path) end scenario "<%= class_name %> clicks link once expired" do visit new_<%= plural_name %>_session_path click_link "Forgot password" fill_in :<%= singular_name %>_email, with: <%= singular_name %>.email click_button "Send" expect(current_path).to eql(new_<%= plural_name %>_session_path) expect(enqueued_jobs) expect(last_mailer_job[:args][0..1]).to eql(["<%= class_name %>Mailer", "password_reset_link"]) travel(16.minutes) do visit(edit_<%= plural_name %>_password_reset_path(token: <%= singular_name %>.password_reset_token.secret)) expect(current_path).to eql(new_<%= plural_name %>_session_path) end end scenario "<%= class_name %> email address not recognised" do visit new_<%= plural_name %>_session_path click_link "Forgot password" fill_in :<%= singular_name %>_email, with: "wrong-email@example.com" click_button "Send" expect(current_path).to eql(new_<%= plural_name %>_session_path) expect(last_mailer_job).to be_nil end private def last_mailer_job enqueued_jobs.select { |job| job[:job] == ActionMailer::MailDeliveryJob }.last end end