From 7d0a71a437cd33e694a18f84f1ae74cdf306befc Mon Sep 17 00:00:00 2001 From: Alex Dolski Date: Wed, 28 Feb 2024 11:01:01 -0600 Subject: [PATCH] Prevent the default profile from being deleted --- app/controllers/metadata_profiles_controller.rb | 4 ++++ app/controllers/submission_profiles_controller.rb | 6 +++++- test/controllers/metadata_profiles_controller_test.rb | 11 ++++++++++- .../submission_profiles_controller_test.rb | 11 ++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/controllers/metadata_profiles_controller.rb b/app/controllers/metadata_profiles_controller.rb index 644b5f3d..1c811ec5 100644 --- a/app/controllers/metadata_profiles_controller.rb +++ b/app/controllers/metadata_profiles_controller.rb @@ -62,6 +62,10 @@ def create def destroy institution = @profile.institution begin + if @profile.institution_default + raise "The default metadata profile cannot be deleted. Set a "\ + "different profile as the default and try again." + end @profile.destroy! rescue => e flash['error'] = "#{e}" diff --git a/app/controllers/submission_profiles_controller.rb b/app/controllers/submission_profiles_controller.rb index e86bcaad..e1ab356e 100644 --- a/app/controllers/submission_profiles_controller.rb +++ b/app/controllers/submission_profiles_controller.rb @@ -59,6 +59,10 @@ def create def destroy institution = @profile.institution begin + if @profile.institution_default + raise "The default metadata profile cannot be deleted. Set a "\ + "different profile as the default and try again." + end @profile.destroy! rescue => e flash['error'] = "#{e}" @@ -76,7 +80,7 @@ def destroy end ## - # Responds to `GET /submission-profiles/:id` (XHR only) + # Responds to `GET /submission-profiles/:id/edit` (XHR only) # def edit render partial: "submission_profiles/form", diff --git a/test/controllers/metadata_profiles_controller_test.rb b/test/controllers/metadata_profiles_controller_test.rb index 5942eee6..0ceb352b 100644 --- a/test/controllers/metadata_profiles_controller_test.rb +++ b/test/controllers/metadata_profiles_controller_test.rb @@ -136,7 +136,7 @@ class MetadataProfilesControllerTest < ActionDispatch::IntegrationTest assert_response :forbidden end - test "destroy() destroys the profile" do + test "destroy() destroys a non-default profile" do log_in_as(users(:southeast_admin)) profile = metadata_profiles(:southeast_unused) assert_difference "MetadataProfile.count", -1 do @@ -144,6 +144,15 @@ class MetadataProfilesControllerTest < ActionDispatch::IntegrationTest end end + test "destroy() refuses to destroy the default profile" do + log_in_as(users(:southeast_admin)) + profile = metadata_profiles(:southeast_unused) + profile.update!(institution_default: true) + assert_no_difference "MetadataProfile.count" do + delete metadata_profile_path(profile) + end + end + test "destroy() redirects to metadata profiles view for non-sysadmins" do log_in_as(users(:southeast_admin)) profile = metadata_profiles(:southeast_unused) diff --git a/test/controllers/submission_profiles_controller_test.rb b/test/controllers/submission_profiles_controller_test.rb index c6a578ea..72e39576 100644 --- a/test/controllers/submission_profiles_controller_test.rb +++ b/test/controllers/submission_profiles_controller_test.rb @@ -135,7 +135,7 @@ class SubmissionProfilesControllerTest < ActionDispatch::IntegrationTest assert_response :forbidden end - test "destroy() destroys the profile" do + test "destroy() destroys a non-default profile" do log_in_as(users(:southwest_admin)) @profile = submission_profiles(:southwest_default) assert_difference "SubmissionProfile.count", -1 do @@ -143,6 +143,15 @@ class SubmissionProfilesControllerTest < ActionDispatch::IntegrationTest end end + test "destroy() refuses to destroy the default profile" do + log_in_as(users(:southwest_admin)) + @profile = submission_profiles(:southwest_default) + @profile.update!(institution_default: true) + assert_no_difference "SubmissionProfile.count" do + delete submission_profile_path(@profile) + end + end + test "destroy() redirects to metadata profiles view for non-sysadmins" do log_in_as(users(:southwest_admin)) profile = submission_profiles(:southwest_default)