1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00

Fixed reset password after the Devise migration.

This commit is contained in:
2025-04-11 07:59:55 -04:00
parent 2d433b680a
commit 4828d9b876
8 changed files with 75 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base class ApplicationMailer < ActionMailer::Base
default from: ENV["WRESTLINGDEV_EMAIL"] default from: ENV["WRESTLINGDEV_EMAIL"] || 'noreply@wrestlingdev.com'
layout 'mailer' layout 'mailer'
end end

View File

@@ -6,6 +6,6 @@ class UserMailer < ApplicationMailer
# #
def password_reset(user) def password_reset(user)
@user = user @user = user
mail to: user.email, subject: "Password reset" mail to: user.email, subject: "WrestlingDev - Password reset"
end end
end end

View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<%= yield %>
</body>
</html>

View File

@@ -0,0 +1 @@
<%= yield %>

View File

@@ -11,6 +11,7 @@
<div class="form-group"> <div class="form-group">
<%= f.label :password %> <%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %> <%= f.password_field :password, class: 'form-control' %>
<%= link_to "Forgot password?", new_password_reset_path %>
</div> </div>
<%= f.submit "Log in", class: "btn btn-primary" %> <%= f.submit "Log in", class: "btn btn-primary" %>

View File

@@ -6,12 +6,16 @@ class PasswordResetsControllerTest < ActionController::TestCase
@user.email = 'user@example.com' @user.email = 'user@example.com'
@user.password_digest = BCrypt::Password.create('password') @user.password_digest = BCrypt::Password.create('password')
@user.save @user.save
# Configure email for testing
setup_test_mailer
end end
test "should get new" do test "should get new" do
get :new get :new
assert_response :success assert_response :success
assert_select 'h1', 'Forgot password' assert_select 'h1', 'Forgot password'
assert_select 'form[action=?]', password_resets_path
end end
test "should not create password reset with invalid email" do test "should not create password reset with invalid email" do
@@ -19,4 +23,42 @@ class PasswordResetsControllerTest < ActionController::TestCase
assert_template 'new' assert_template 'new'
assert_not_nil flash[:alert] assert_not_nil flash[:alert]
end end
test "should create password reset with valid email" do
post :create, params: { password_reset: { email: @user.email } }
assert_not_equal @user.reset_digest, @user.reload.reset_digest
assert_equal 1, ActionMailer::Base.deliveries.size
assert_not_nil flash[:notice]
assert_redirected_to root_url
end
test "should get edit with valid token" do
post :create, params: { password_reset: { email: @user.email } }
user = assigns(:user)
# Valid token, correct email
get :edit, params: { id: user.reset_token, email: user.email }
assert_response :success
assert_select "input[name=?]", "user[password]"
assert_select "input[name=?]", "user[password_confirmation]"
end
test "should update password with valid info" do
post :create, params: { password_reset: { email: @user.email } }
user = assigns(:user)
# Valid token, correct email, valid info
patch :update, params: {
id: user.reset_token,
email: user.email,
user: {
password: "newpassword",
password_confirmation: "newpassword"
}
}
assert_not_nil session[:user_id]
assert_not_nil flash[:notice]
assert_redirected_to root_url
end
end end

View File

@@ -12,6 +12,13 @@ class SessionsControllerTest < ActionController::TestCase
get :new get :new
assert_response :success assert_response :success
assert_select 'h1', 'Log in' assert_select 'h1', 'Log in'
assert_select "a[href=?]", new_password_reset_path, text: "Forgot password?"
end
test "login page should have forgot password link" do
get :new
assert_response :success
assert_select "a[href=?]", new_password_reset_path, text: "Forgot password?"
end end
test "should create session with valid credentials" do test "should create session with valid credentials" do

View File

@@ -13,6 +13,15 @@ class ActiveSupport::TestCase
# Add more helper methods to be used by all tests here... # Add more helper methods to be used by all tests here...
# Configure email for testing
def setup_test_mailer
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
Rails.application.routes.default_url_options[:host] = 'example.com'
ActionMailer::Base.default from: 'test@example.com'
end
# Authentication helpers for tests - replaces Devise test helpers # Authentication helpers for tests - replaces Devise test helpers
def sign_in(user) def sign_in(user)
# Set the password_digest for the user if it's not already set # Set the password_digest for the user if it's not already set