From 1b1add38f29278404b19c48095c0bdc67a57f7c5 Mon Sep 17 00:00:00 2001 From: Josh Pigford Date: Sun, 20 Apr 2025 20:15:49 -0500 Subject: [PATCH] Enhance member removal process by destroying associated invitations and updating flash messages in ProfilesController. Add test to verify invitation deletion upon member removal. Fixes #2066 --- .../settings/profiles_controller.rb | 6 +++-- .../settings/profiles_controller_test.rb | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/controllers/settings/profiles_controller.rb b/app/controllers/settings/profiles_controller.rb index d62eecd6..a5631da4 100644 --- a/app/controllers/settings/profiles_controller.rb +++ b/app/controllers/settings/profiles_controller.rb @@ -23,9 +23,11 @@ class Settings::ProfilesController < ApplicationController end if @user.destroy - flash[:notice] = t("settings.profiles.destroy.member_removed") + # Also destroy the invitation associated with this user for this family + Current.family.invitations.find_by(email: @user.email)&.destroy + flash[:notice] = "Member removed successfully." else - flash[:alert] = t("settings.profiles.destroy.member_removal_failed") + flash[:alert] = "Failed to remove member." end redirect_to settings_profile_path diff --git a/test/controllers/settings/profiles_controller_test.rb b/test/controllers/settings/profiles_controller_test.rb index 27d62c17..deae066d 100644 --- a/test/controllers/settings/profiles_controller_test.rb +++ b/test/controllers/settings/profiles_controller_test.rb @@ -19,7 +19,7 @@ class Settings::ProfilesControllerTest < ActionDispatch::IntegrationTest end assert_redirected_to settings_profile_path - assert_equal I18n.t("settings.profiles.destroy.member_removed"), flash[:notice] + assert_equal "Member removed successfully.", flash[:notice] assert_raises(ActiveRecord::RecordNotFound) { User.find(@member.id) } end @@ -44,4 +44,24 @@ class Settings::ProfilesControllerTest < ActionDispatch::IntegrationTest assert_equal I18n.t("settings.profiles.destroy.not_authorized"), flash[:alert] assert User.find(@admin.id) end + + test "admin removing a family member also destroys their invitation" do + # Create an invitation for the member + invitation = @admin.family.invitations.create!( + email: @member.email, + role: "member", + inviter: @admin + ) + + sign_in @admin + + assert_difference [ "User.count", "Invitation.count" ], -1 do + delete settings_profile_path(user_id: @member) + end + + assert_redirected_to settings_profile_path + assert_equal "Member removed successfully.", flash[:notice] + assert_raises(ActiveRecord::RecordNotFound) { User.find(@member.id) } + assert_raises(ActiveRecord::RecordNotFound) { Invitation.find(invitation.id) } + end end