2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe SiteSerializer do
|
2019-03-16 07:48:57 -04:00
|
|
|
let(:guardian) { Guardian.new }
|
2020-08-19 15:05:04 -04:00
|
|
|
let(:category) { Fabricate(:category) }
|
2019-03-16 07:48:57 -04:00
|
|
|
|
2021-06-23 03:21:11 -04:00
|
|
|
after do
|
|
|
|
Site.clear_cache
|
|
|
|
end
|
|
|
|
|
2019-03-16 07:48:57 -04:00
|
|
|
it "includes category custom fields only if its preloaded" do
|
|
|
|
category.custom_fields["enable_marketplace"] = true
|
|
|
|
category.save_custom_fields
|
|
|
|
|
2021-06-23 03:21:11 -04:00
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
|
|
|
|
|
|
|
expect(c1[:custom_fields]).to eq(nil)
|
2019-03-16 07:48:57 -04:00
|
|
|
|
|
|
|
Site.preloaded_category_custom_fields << "enable_marketplace"
|
2021-06-23 03:21:11 -04:00
|
|
|
Site.clear_cache
|
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
|
|
|
|
|
|
|
expect(c1[:custom_fields]["enable_marketplace"]).to eq("t")
|
2021-06-24 21:15:17 -04:00
|
|
|
ensure
|
2022-07-13 20:54:31 -04:00
|
|
|
Site.reset_preloaded_category_custom_fields
|
2021-06-23 03:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "includes category tags" do
|
|
|
|
tag = Fabricate(:tag)
|
|
|
|
tag_group = Fabricate(:tag_group)
|
|
|
|
tag_group_2 = Fabricate(:tag_group)
|
|
|
|
|
|
|
|
category.tags << tag
|
|
|
|
category.tag_groups << tag_group
|
2022-04-06 09:08:06 -04:00
|
|
|
category.update!(category_required_tag_groups: [CategoryRequiredTagGroup.new(tag_group: tag_group_2, min_count: 1)])
|
2021-06-23 03:21:11 -04:00
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
2019-03-16 07:48:57 -04:00
|
|
|
|
2021-06-23 03:21:11 -04:00
|
|
|
expect(c1[:allowed_tags]).to contain_exactly(tag.name)
|
|
|
|
expect(c1[:allowed_tag_groups]).to contain_exactly(tag_group.name)
|
2022-04-06 09:08:06 -04:00
|
|
|
expect(c1[:required_tag_groups]).to eq([{ name: tag_group_2.name, min_count: 1 }])
|
2019-03-16 07:48:57 -04:00
|
|
|
end
|
2020-08-19 15:05:04 -04:00
|
|
|
|
2022-04-21 13:18:35 -04:00
|
|
|
it "doesn't explode when category_required_tag_group is missing" do
|
|
|
|
tag = Fabricate(:tag)
|
|
|
|
tag_group = Fabricate(:tag_group)
|
|
|
|
crtg = CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1)
|
|
|
|
category.update!(category_required_tag_groups: [ crtg ])
|
|
|
|
|
|
|
|
tag_group.delete # Bypassing hooks like this should never happen in the app
|
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
|
|
|
|
|
|
|
expect(c1[:required_tag_groups]).to eq([{ name: nil, min_count: 1 }])
|
|
|
|
end
|
|
|
|
|
2020-08-19 15:05:04 -04:00
|
|
|
it "returns correct notification level for categories" do
|
|
|
|
SiteSetting.mute_all_categories_by_default = true
|
2022-06-19 23:49:33 -04:00
|
|
|
SiteSetting.default_categories_normal = category.id.to_s
|
2020-08-19 15:05:04 -04:00
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
categories = serialized[:categories]
|
|
|
|
expect(categories[0][:notification_level]).to eq(0)
|
|
|
|
expect(categories[-1][:notification_level]).to eq(1)
|
|
|
|
end
|
2020-08-28 10:36:52 -04:00
|
|
|
|
|
|
|
it "includes user-selectable color schemes" do
|
2020-10-14 10:18:02 -04:00
|
|
|
# it includes seeded color schemes
|
2020-08-28 10:36:52 -04:00
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
2022-07-08 17:46:32 -04:00
|
|
|
expect(serialized[:user_color_schemes].count).to eq(6)
|
2020-08-28 10:36:52 -04:00
|
|
|
|
2020-10-15 14:05:48 -04:00
|
|
|
scheme_names = serialized[:user_color_schemes].map { |x| x[:name] }
|
|
|
|
expect(scheme_names).to include(I18n.t("color_schemes.dark"))
|
|
|
|
expect(scheme_names).to include(I18n.t("color_schemes.wcag"))
|
|
|
|
expect(scheme_names).to include(I18n.t("color_schemes.wcag_dark"))
|
2022-07-08 17:46:32 -04:00
|
|
|
expect(scheme_names).to include(I18n.t("color_schemes.solarized_light"))
|
|
|
|
expect(scheme_names).to include(I18n.t("color_schemes.solarized_dark"))
|
|
|
|
expect(scheme_names).to include(I18n.t("color_schemes.dracula"))
|
2020-10-15 14:05:48 -04:00
|
|
|
|
|
|
|
dark_scheme = ColorScheme.create_from_base(name: "AnotherDarkScheme", base_scheme_id: "Dark")
|
2020-08-28 10:36:52 -04:00
|
|
|
dark_scheme.user_selectable = true
|
|
|
|
dark_scheme.save!
|
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
2022-07-08 17:46:32 -04:00
|
|
|
expect(serialized[:user_color_schemes].count).to eq(7)
|
2020-08-28 10:36:52 -04:00
|
|
|
expect(serialized[:user_color_schemes][0][:is_dark]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes default dark mode scheme" do
|
|
|
|
scheme = ColorScheme.last
|
|
|
|
SiteSetting.default_dark_mode_color_scheme_id = scheme.id
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
default_dark_scheme =
|
|
|
|
expect(serialized[:default_dark_color_scheme]["name"]).to eq(scheme.name)
|
|
|
|
|
|
|
|
SiteSetting.default_dark_mode_color_scheme_id = -1
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
expect(serialized[:default_dark_color_scheme]).to eq(nil)
|
|
|
|
end
|
2021-05-06 15:09:31 -04:00
|
|
|
|
|
|
|
it 'does not include shared_drafts_category_id if the category is Uncategorized' do
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
admin_guardian = Guardian.new(admin)
|
|
|
|
|
|
|
|
SiteSetting.shared_drafts_category = SiteSetting.uncategorized_category_id
|
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(admin_guardian), scope: admin_guardian, root: false).as_json
|
|
|
|
expect(serialized[:shared_drafts_category_id]).to eq(nil)
|
|
|
|
end
|
2022-08-09 12:22:39 -04:00
|
|
|
|
|
|
|
it 'includes show_welcome_topic_banner' do
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
admin_guardian = Guardian.new(admin)
|
|
|
|
UserAuthToken.generate!(user_id: admin.id)
|
|
|
|
|
|
|
|
first_post = Fabricate(:post, created_at: 25.days.ago)
|
|
|
|
SiteSetting.welcome_topic_id = first_post.topic.id
|
|
|
|
|
|
|
|
serialized = described_class.new(Site.new(admin_guardian), scope: admin_guardian, root: false).as_json
|
|
|
|
expect(serialized[:show_welcome_topic_banner]).to eq(true)
|
|
|
|
end
|
2022-08-23 04:20:46 -04:00
|
|
|
|
|
|
|
it 'includes anonymous_default_sidebar_tags' do
|
|
|
|
Fabricate(:tag, name: "dev")
|
|
|
|
Fabricate(:tag, name: "random")
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
expect(serialized[:anonymous_default_sidebar_tags]).to eq(nil)
|
|
|
|
|
|
|
|
SiteSetting.default_sidebar_tags = "dev|random"
|
|
|
|
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
|
|
|
expect(serialized[:anonymous_default_sidebar_tags]).to eq(["dev", "random"])
|
|
|
|
end
|
2019-03-16 07:48:57 -04:00
|
|
|
end
|