A platform for community discussion. Free, open, simple.
DEV: Minor improvements to SidebarSectionsController spec (#21734)
Alan Guo Xiang Tane8b138e24fbc
diff --git a/app/controllers/sidebar_sections_controller.rb b/app/controllers/sidebar_sections_controller.rbindex 019709b6d88..a9eea34f3dd 100644--- a/app/controllers/sidebar_sections_controller.rb+++ b/app/controllers/sidebar_sections_controller.rb@@ -85,7 +85,7 @@ class SidebarSectionsController < ApplicationControllerorder = reorder_params["links_order"].map(&:to_i).each_with_index.to_hset_order(sidebar_section, order)- render_serialized_sidebar_section(sidebar_section)+ render_serialized(sidebar_section, SidebarSectionSerializer)rescue Discourse::InvalidAccessrender json: failed_json, status: 403enddiff --git a/spec/requests/sidebar_sections_controller_spec.rb b/spec/requests/sidebar_sections_controller_spec.rbindex 08b003d56e3..d593dcc682d 100644--- a/spec/requests/sidebar_sections_controller_spec.rb+++ b/spec/requests/sidebar_sections_controller_spec.rb@@ -89,6 +89,7 @@ RSpec.describe SidebarSectionsController doit "does not allow regular user to create public section" dosign_in(user)+post "/sidebar_sections.json",params: {title: "custom section",@@ -98,11 +99,13 @@ RSpec.describe SidebarSectionsController do{ icon: "address-book", name: "tags", value: "/tags" },],}+expect(response.status).to eq(403)endit "does not allow moderator to create public section" dosign_in(moderator)+post "/sidebar_sections.json",params: {title: "custom section",@@ -112,11 +115,13 @@ RSpec.describe SidebarSectionsController do{ icon: "address-book", name: "tags", value: "/tags" },],}+expect(response.status).to eq(403)endit "allows admin to create public section" dosign_in(admin)+post "/sidebar_sections.json",params: {title: "custom section",@@ -126,6 +131,7 @@ RSpec.describe SidebarSectionsController do{ icon: "address-book", name: "tags", value: "/tags" },],}+expect(response.status).to eq(200)sidebar_section = SidebarSection.last@@ -144,18 +150,22 @@ RSpec.describe SidebarSectionsController dofab!(:sidebar_section) { Fabricate(:sidebar_section, user: user) }fab!(:sidebar_url_1) { Fabricate(:sidebar_url, name: "tags", value: "/tags") }fab!(:sidebar_url_2) { Fabricate(:sidebar_url, name: "categories", value: "/categories") }+fab!(:section_link_1) doFabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url_1)end+fab!(:section_link_2) doFabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url_2)end+let(:community_section) doSidebarSection.find_by(section_type: SidebarSection.section_types[:community])endit "allows user to update their own section and links" dosign_in(user)+put "/sidebar_sections/#{sidebar_section.id}.json",params: {title: "custom section edited",@@ -178,6 +188,7 @@ RSpec.describe SidebarSectionsController doit "allows admin to update public section and links" dosign_in(admin)sidebar_section.update!(public: true)+put "/sidebar_sections/#{sidebar_section.id}.json",params: {title: "custom section edited",@@ -230,6 +241,7 @@ RSpec.describe SidebarSectionsController dosidebar_url_3 = Fabricate(:sidebar_url, name: "other_tags", value: "/tags")Fabricate(:sidebar_section_link, sidebar_section: sidebar_section_2, linkable: sidebar_url_3)sign_in(user)+put "/sidebar_sections/#{sidebar_section_2.id}.json",params: {title: "custom section edited",@@ -242,6 +254,7 @@ RSpec.describe SidebarSectionsController doit "doesn't allow to edit public sections" dosign_in(user)sidebar_section.update!(public: true)+put "/sidebar_sections/#{sidebar_section.id}.json",params: {title: "custom section edited",@@ -250,17 +263,21 @@ RSpec.describe SidebarSectionsController do{ icon: "link", id: sidebar_url_2.id, name: "tags", value: "/tags", _destroy: "1" },],}+expect(response.status).to eq(403)endit "doesn't allow to edit other's links" dosidebar_url_3 = Fabricate(:sidebar_url, name: "other_tags", value: "/tags")+Fabricate(:sidebar_section_link,sidebar_section: Fabricate(:sidebar_section),linkable: sidebar_url_3,)+sign_in(user)+put "/sidebar_sections/#{sidebar_section.id}.json",params: {title: "custom section edited",@@ -268,12 +285,12 @@ RSpec.describe SidebarSectionsController do}expect(response.status).to eq(404)-expect(sidebar_url_3.reload.name).to eq("other_tags")endit "doesn't allow users to edit community section" dosign_in(user)+put "/sidebar_sections/#{community_section.id}.json",params: {title: "custom section edited",@@ -285,8 +302,10 @@ RSpec.describe SidebarSectionsController doit "allows admin to edit community section" dosign_in(admin)+everything_link = community_section.sidebar_urls.find_by(name: "Everything")my_posts_link = community_section.sidebar_urls.find_by(name: "My Posts")+community_section.sidebar_section_links.where.not(linkable_id: [everything_link.id, my_posts_link.id])@@ -317,58 +336,74 @@ RSpec.describe SidebarSectionsController doenddescribe "#reorder" do+ fab!(:user2) { Fabricate(:user) }fab!(:sidebar_section) { Fabricate(:sidebar_section, user: user) }fab!(:sidebar_url_1) { Fabricate(:sidebar_url, name: "tags", value: "/tags") }fab!(:sidebar_url_2) { Fabricate(:sidebar_url, name: "categories", value: "/categories") }fab!(:sidebar_url_3) { Fabricate(:sidebar_url, name: "topic", value: "/t/1") }+fab!(:section_link_1) doFabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url_1)end+fab!(:section_link_2) doFabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url_2)end+fab!(:section_link_3) doFabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url_3)endit "sorts links" do- serializer = SidebarSectionSerializer.new(sidebar_section, root: false).as_json- expect(serializer[:links].map(&:id)).to eq(+ expect(sidebar_section.sidebar_urls.pluck(:id)).to eq([sidebar_url_1.id, sidebar_url_2.id, sidebar_url_3.id],)sign_in(user)+post "/sidebar_sections/reorder.json",params: {sidebar_section_id: sidebar_section.id,links_order: [sidebar_url_2.id, sidebar_url_3.id, sidebar_url_1.id],}- serializer = SidebarSectionSerializer.new(sidebar_section.reload, root: false).as_json- expect(serializer[:links].map(&:id)).to eq(+ expect(response.status).to eq(200)++ expect(sidebar_section.reload.sidebar_urls.pluck(:id)).to eq([sidebar_url_2.id, sidebar_url_3.id, sidebar_url_1.id],)end- it "is not allowed for not own sections" do- sidebar_section_2 = Fabricate(:sidebar_section)+ it "returns 403 when a user tries to reorder a section that doesn't belong to them" do+ sign_in(user2)+post "/sidebar_sections/reorder.json",params: {- sidebar_section_id: sidebar_section_2.id,+ sidebar_section_id: sidebar_section.id,links_order: [sidebar_url_2.id, sidebar_url_3.id, sidebar_url_1.id],}expect(response.status).to eq(403)- serializer = SidebarSectionSerializer.new(sidebar_section, root: false).as_json- expect(serializer[:links].map(&:id)).to eq(+ expect(sidebar_section.reload.sidebar_urls.pluck(:id)).to eq([sidebar_url_1.id, sidebar_url_2.id, sidebar_url_3.id],)end++ it "returns 403 for an non user" do+ post "/sidebar_sections/reorder.json",+ params: {+ sidebar_section_id: sidebar_section.id,+ links_order: [sidebar_url_2.id, sidebar_url_3.id, sidebar_url_1.id],+ }++ expect(response.status).to eql(403)+ endenddescribe "#destroy" dofab!(:sidebar_section) { Fabricate(:sidebar_section, user: user) }+let(:community_section) doSidebarSection.find_by(section_type: SidebarSection.section_types[:community])end@@ -380,7 +415,6 @@ RSpec.describe SidebarSectionsController doexpect(response.status).to eq(200)expect { sidebar_section.reload }.to raise_error(ActiveRecord::RecordNotFound)-expect(UserHistory.count).to eq(0)end