Skip to content

Commit

Permalink
Prevent the default profile from being deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Dolski committed Feb 28, 2024
1 parent 3abe3d9 commit 7d0a71a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/controllers/metadata_profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/submission_profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion test/controllers/metadata_profiles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,23 @@ 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
delete metadata_profile_path(profile)
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)
Expand Down
11 changes: 10 additions & 1 deletion test/controllers/submission_profiles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,23 @@ 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
delete submission_profile_path(@profile)
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)
Expand Down

0 comments on commit 7d0a71a

Please sign in to comment.