Skip to content

A platform for community discussion. Free, open, simple.

FIX: raise 404 when sidebar section doesn't exist (#37675)

Joffrey JAFFEUXdc51fcabcb91
diff --git a/app/controllers/sidebar_sections_controller.rb b/app/controllers/sidebar_sections_controller.rb
index 957f1848fea..c4d395003fb 100644
--- a/app/controllers/sidebar_sections_controller.rb
+++ b/app/controllers/sidebar_sections_controller.rb
@@ -41,7 +41,7 @@ class SidebarSectionsController < ApplicationController
end
def update
- sidebar_section = SidebarSection.find_by(id: section_params["id"])
+ sidebar_section = SidebarSection.find(section_params["id"])
@guardian.ensure_can_edit!(sidebar_section)
ActiveRecord::Base.transaction do
@@ -91,7 +91,7 @@ class SidebarSectionsController < ApplicationController
end
def destroy
- sidebar_section = SidebarSection.find_by(id: section_params["id"])
+ sidebar_section = SidebarSection.find(section_params["id"])
@guardian.ensure_can_delete!(sidebar_section)
sidebar_section.destroy!
diff --git a/spec/requests/sidebar_sections_controller_spec.rb b/spec/requests/sidebar_sections_controller_spec.rb
index 739c292db16..12b91837a4b 100644
--- a/spec/requests/sidebar_sections_controller_spec.rb
+++ b/spec/requests/sidebar_sections_controller_spec.rb
@@ -288,6 +288,18 @@ RSpec.describe SidebarSectionsController do
)
end
+ it "returns 404 when updating a non-existent section" do
+ sign_in(user)
+
+ put "/sidebar_sections/99999999.json",
+ params: {
+ title: "custom section edited",
+ links: [{ icon: "link", name: "latest", value: "/latest" }],
+ }
+
+ expect(response.status).to eq(404)
+ end
+
it "doesn't allow to edit other's sections" do
sidebar_section_2 = Fabricate(:sidebar_section)
sidebar_url_3 = Fabricate(:sidebar_url, name: "other_tags", value: "/tags")
@@ -413,6 +425,13 @@ RSpec.describe SidebarSectionsController do
expect(user_history.subject).to eq("Sidebar section")
end
+ it "returns 404 when deleting a non-existent section" do
+ sign_in(user)
+ delete "/sidebar_sections/99999999.json"
+
+ expect(response.status).to eq(404)
+ end
+
it "doesn't allow to delete other's sidebar section" do
sidebar_section_2 = Fabricate(:sidebar_section)
sign_in(user)