2022-06-30 01:54:20 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe SidebarSectionLink do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2022-06-30 01:54:20 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "Validations" do
|
2022-06-30 01:54:20 -05:00
|
|
|
it "is not valid when linkable already exists for the current user" do
|
|
|
|
category_sidebar_section_link = Fabricate(:category_sidebar_section_link, user: user)
|
|
|
|
|
|
|
|
sidebar_section_link =
|
|
|
|
SidebarSectionLink.new(user: user, linkable: category_sidebar_section_link.linkable)
|
|
|
|
|
|
|
|
expect(sidebar_section_link.valid?).to eq(false)
|
|
|
|
expect(sidebar_section_link.errors.details[:user_id][0][:error]).to eq(:taken)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#linkable_type" do
|
|
|
|
it "is not valid when linkable_type is not supported" do
|
|
|
|
sidebar_section_link =
|
|
|
|
SidebarSectionLink.new(user: user, linkable_id: 1, linkable_type: "sometype")
|
|
|
|
|
|
|
|
expect(sidebar_section_link.valid?).to eq(false)
|
|
|
|
|
|
|
|
expect(sidebar_section_link.errors[:linkable_type]).to eq(
|
|
|
|
[
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid",
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
2022-06-30 01:54:20 -05:00
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is not valid when linkable_type is Tag and SiteSetting.tagging_enabled is false" do
|
|
|
|
SiteSetting.tagging_enabled = false
|
|
|
|
sidebar_section_link =
|
|
|
|
SidebarSectionLink.new(user: user, linkable_id: 1, linkable_type: "Tag")
|
|
|
|
|
|
|
|
expect(sidebar_section_link.valid?).to eq(false)
|
|
|
|
|
|
|
|
expect(sidebar_section_link.errors[:linkable_type]).to eq(
|
|
|
|
[
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid",
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
2022-06-30 01:54:20 -05:00
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-04-04 19:52:18 -05:00
|
|
|
|
|
|
|
it "uses section user for links belonging to private sections" do
|
|
|
|
private_section = Fabricate(:sidebar_section, public: false)
|
|
|
|
sidebar_section_link =
|
|
|
|
Fabricate(
|
|
|
|
:sidebar_section_link,
|
|
|
|
sidebar_section: private_section,
|
|
|
|
linkable_id: 1,
|
|
|
|
linkable_type: "Tag",
|
|
|
|
)
|
|
|
|
expect(sidebar_section_link.user_id).to eq(private_section.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses system user for links belonging to public sections" do
|
|
|
|
public_section = Fabricate(:sidebar_section, public: true)
|
|
|
|
sidebar_section_link =
|
|
|
|
Fabricate(
|
|
|
|
:sidebar_section_link,
|
|
|
|
sidebar_section: public_section,
|
|
|
|
linkable_id: 1,
|
|
|
|
linkable_type: "Tag",
|
|
|
|
)
|
|
|
|
expect(sidebar_section_link.user_id).to eq(Discourse.system_user.id)
|
|
|
|
end
|
2022-06-30 01:54:20 -05:00
|
|
|
end
|