2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Site do
|
2021-06-25 09:15:17 +08:00
|
|
|
after { Site.clear_cache }
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def expect_correct_themes(guardian)
|
|
|
|
json = Site.json_for(guardian)
|
|
|
|
parsed = JSON.parse(json)
|
|
|
|
|
2018-07-12 12:18:21 +08:00
|
|
|
expected =
|
|
|
|
Theme
|
|
|
|
.where("id = :default OR user_selectable", default: SiteSetting.default_theme_id)
|
2017-04-12 22:52:52 +08:00
|
|
|
.order(:name)
|
2020-08-28 22:36:52 +08:00
|
|
|
.pluck(:id, :name, :color_scheme_id)
|
|
|
|
.map do |id, n, cs|
|
|
|
|
{
|
|
|
|
"theme_id" => id,
|
|
|
|
"name" => n,
|
|
|
|
"default" => id == SiteSetting.default_theme_id,
|
|
|
|
"color_scheme_id" => cs,
|
|
|
|
}
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
expect(parsed["user_themes"]).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes user themes and expires them as needed" do
|
2018-08-08 12:46:34 +08:00
|
|
|
default_theme = Fabricate(:theme)
|
2018-07-12 12:18:21 +08:00
|
|
|
SiteSetting.default_theme_id = default_theme.id
|
2018-08-08 12:46:34 +08:00
|
|
|
user_theme = Fabricate(:theme, user_selectable: true)
|
2021-01-08 00:15:38 +08:00
|
|
|
second_user_theme = Fabricate(:theme, user_selectable: true)
|
|
|
|
color_scheme = Fabricate(:color_scheme)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
anon_guardian = Guardian.new
|
|
|
|
user_guardian = Guardian.new(Fabricate(:user))
|
|
|
|
|
|
|
|
expect_correct_themes(anon_guardian)
|
|
|
|
expect_correct_themes(user_guardian)
|
|
|
|
|
|
|
|
Theme.clear_default!
|
|
|
|
|
|
|
|
expect_correct_themes(anon_guardian)
|
|
|
|
expect_correct_themes(user_guardian)
|
|
|
|
|
|
|
|
user_theme.user_selectable = false
|
|
|
|
user_theme.save!
|
|
|
|
|
|
|
|
expect_correct_themes(anon_guardian)
|
|
|
|
expect_correct_themes(user_guardian)
|
|
|
|
|
2021-01-08 00:15:38 +08:00
|
|
|
second_user_theme.color_scheme_id = color_scheme.id
|
|
|
|
second_user_theme.save!
|
|
|
|
|
|
|
|
expect_correct_themes(anon_guardian)
|
|
|
|
expect_correct_themes(user_guardian)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-11-08 10:58:11 +08:00
|
|
|
it "returns correct notification level for categories" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
guardian = Guardian.new
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(guardian).categories.last[:notification_level]).to eq(1)
|
2019-11-08 10:58:11 +08:00
|
|
|
SiteSetting.mute_all_categories_by_default = true
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(guardian).categories.last[:notification_level]).to eq(0)
|
2019-11-08 10:58:11 +08:00
|
|
|
SiteSetting.default_categories_tracking = category.id.to_s
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(guardian).categories.last[:notification_level]).to eq(1)
|
2019-11-08 10:58:11 +08:00
|
|
|
end
|
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
describe "#categories" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:category)
|
|
|
|
fab!(:user)
|
2022-08-04 08:17:40 +08:00
|
|
|
let(:guardian) { Guardian.new(user) }
|
2021-06-23 15:21:11 +08:00
|
|
|
|
|
|
|
it "omits read restricted categories" do
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(guardian).categories.map { |c| c[:id] }).to contain_exactly(
|
2021-06-23 15:21:11 +08:00
|
|
|
SiteSetting.uncategorized_category_id,
|
|
|
|
category.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
category.update!(read_restricted: true)
|
|
|
|
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(guardian).categories.map { |c| c[:id] }).to contain_exactly(
|
2021-06-23 15:21:11 +08:00
|
|
|
SiteSetting.uncategorized_category_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes categories that a user's group can see" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
category.update!(read_restricted: true)
|
|
|
|
category.groups << group
|
|
|
|
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(guardian).categories.map { |c| c[:id] }).to contain_exactly(
|
2021-06-23 15:21:11 +08:00
|
|
|
SiteSetting.uncategorized_category_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
group.add(user)
|
|
|
|
|
2021-06-28 10:39:13 +08:00
|
|
|
expect(Site.new(Guardian.new(user)).categories.map { |c| c[:id] }).to contain_exactly(
|
2021-06-23 15:21:11 +08:00
|
|
|
SiteSetting.uncategorized_category_id,
|
|
|
|
category.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "omits categories users can not write to from the category list" do
|
|
|
|
expect(Site.new(guardian).categories.count).to eq(2)
|
|
|
|
|
|
|
|
category.set_permissions(everyone: :create_post)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
|
|
|
|
expect(
|
2021-06-28 10:39:13 +08:00
|
|
|
Site.new(guardian).categories.keep_if { |c| c[:name] == category.name }.first[:permission],
|
2021-06-23 15:21:11 +08:00
|
|
|
).not_to eq(CategoryGroup.permission_types[:full])
|
|
|
|
|
|
|
|
# If a parent category is not visible, the child categories should not be returned
|
|
|
|
category.set_permissions(staff: :full)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
sub_category = Fabricate(:category, parent_category_id: category.id)
|
|
|
|
expect(Site.new(guardian).categories).not_to include(sub_category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should clear the cache when custom fields are updated" do
|
|
|
|
Site.preloaded_category_custom_fields << "enable_marketplace"
|
|
|
|
categories = Site.new(Guardian.new).categories
|
|
|
|
|
|
|
|
expect(categories.last[:custom_fields]["enable_marketplace"]).to eq(nil)
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
category.custom_fields["enable_marketplace"] = true
|
|
|
|
category.save_custom_fields
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
categories = Site.new(Guardian.new).categories
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
expect(categories.last[:custom_fields]["enable_marketplace"]).to eq("t")
|
2014-02-25 03:52:21 +08:00
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
category.upsert_custom_fields(enable_marketplace: false)
|
2014-02-25 03:52:21 +08:00
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
categories = Site.new(Guardian.new).categories
|
2014-02-25 03:52:21 +08:00
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
expect(categories.last[:custom_fields]["enable_marketplace"]).to eq("f")
|
2021-06-25 09:15:17 +08:00
|
|
|
ensure
|
2022-07-14 08:54:31 +08:00
|
|
|
Site.reset_preloaded_category_custom_fields
|
2021-06-23 15:21:11 +08:00
|
|
|
end
|
2021-06-28 10:39:13 +08:00
|
|
|
|
|
|
|
it "sets the can_edit field for categories correctly" do
|
|
|
|
categories = Site.new(Guardian.new).categories
|
|
|
|
|
|
|
|
expect(categories.map { |c| c[:can_edit] }).to contain_exactly(false, false)
|
|
|
|
|
|
|
|
site = Site.new(Guardian.new(Fabricate(:moderator)))
|
|
|
|
|
|
|
|
expect(site.categories.map { |c| c[:can_edit] }).to contain_exactly(false, false)
|
|
|
|
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
|
|
|
|
site = Site.new(Guardian.new(Fabricate(:moderator)))
|
|
|
|
|
|
|
|
expect(site.categories.map { |c| c[:can_edit] }).to contain_exactly(true, true)
|
|
|
|
end
|
2023-05-31 05:41:50 +08:00
|
|
|
|
|
|
|
describe "site_all_categories_cache_query modifier" do
|
|
|
|
fab!(:cool_category) { Fabricate(:category, name: "Cool category") }
|
|
|
|
fab!(:boring_category) { Fabricate(:category, name: "Boring category") }
|
|
|
|
|
|
|
|
it "allows changing the query" do
|
|
|
|
prefetched_categories = Site.new(Guardian.new(user)).categories.map { |c| c[:id] }
|
|
|
|
expect(prefetched_categories).to include(cool_category.id, boring_category.id)
|
|
|
|
|
|
|
|
# we need to clear the cache to ensure that the categories list will be updated
|
|
|
|
Site.clear_cache
|
|
|
|
|
|
|
|
Plugin::Instance
|
|
|
|
.new
|
|
|
|
.register_modifier(:site_all_categories_cache_query) do |query|
|
|
|
|
query.where("categories.name LIKE 'Cool%'")
|
|
|
|
end
|
|
|
|
|
|
|
|
prefetched_categories = Site.new(Guardian.new(user)).categories.map { |c| c[:id] }
|
|
|
|
|
|
|
|
expect(prefetched_categories).to include(cool_category.id)
|
|
|
|
expect(prefetched_categories).not_to include(boring_category.id)
|
|
|
|
ensure
|
|
|
|
DiscoursePluginRegistry.clear_modifiers!
|
|
|
|
end
|
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
end
|
2014-02-25 03:52:21 +08:00
|
|
|
|
2019-02-14 22:35:58 +08:00
|
|
|
it "omits groups user can not see" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
site = Site.new(Guardian.new(user))
|
|
|
|
|
2019-02-15 07:24:29 +08:00
|
|
|
staff_group = Fabricate(:group, visibility_level: Group.visibility_levels[:staff])
|
|
|
|
expect(site.groups.pluck(:name)).not_to include(staff_group.name)
|
2019-02-14 22:35:58 +08:00
|
|
|
|
2019-02-15 07:24:29 +08:00
|
|
|
public_group = Fabricate(:group)
|
|
|
|
expect(site.groups.pluck(:name)).to include(public_group.name)
|
2019-02-14 22:35:58 +08:00
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
site = Site.new(Guardian.new(admin))
|
2019-02-15 07:24:29 +08:00
|
|
|
expect(site.groups.pluck(:name)).to include(staff_group.name, public_group.name, "everyone")
|
2019-02-14 22:35:58 +08:00
|
|
|
end
|
|
|
|
|
2023-05-31 05:41:50 +08:00
|
|
|
describe "site_groups_query modifier" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2023-05-31 05:41:50 +08:00
|
|
|
fab!(:cool_group) { Fabricate(:group, name: "cool-group") }
|
|
|
|
fab!(:boring_group) { Fabricate(:group, name: "boring-group") }
|
|
|
|
|
|
|
|
it "allows changing the query" do
|
|
|
|
prefetched_groups = Site.new(Guardian.new(user)).groups.map { |c| c[:id] }
|
|
|
|
expect(prefetched_groups).to include(cool_group.id, boring_group.id)
|
|
|
|
|
|
|
|
# we need to clear the cache to ensure that the groups list will be updated
|
|
|
|
Site.clear_cache
|
|
|
|
|
|
|
|
Plugin::Instance
|
|
|
|
.new
|
|
|
|
.register_modifier(:site_groups_query) { |query| query.where("groups.name LIKE 'cool%'") }
|
|
|
|
|
|
|
|
prefetched_groups = Site.new(Guardian.new(user)).groups.map { |c| c[:id] }
|
|
|
|
|
|
|
|
expect(prefetched_groups).to include(cool_group.id)
|
|
|
|
expect(prefetched_groups).not_to include(boring_group.id)
|
|
|
|
ensure
|
|
|
|
DiscoursePluginRegistry.clear_modifiers!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-31 23:18:50 +08:00
|
|
|
it "includes all enabled authentication providers" do
|
|
|
|
SiteSetting.enable_twitter_logins = true
|
|
|
|
SiteSetting.enable_facebook_logins = true
|
2018-08-07 16:20:09 +08:00
|
|
|
data = JSON.parse(Site.json_for(Guardian.new))
|
|
|
|
expect(data["auth_providers"].map { |a| a["name"] }).to contain_exactly("facebook", "twitter")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes all enabled authentication providers for anon when login_required" do
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
SiteSetting.enable_twitter_logins = true
|
|
|
|
SiteSetting.enable_facebook_logins = true
|
|
|
|
data = JSON.parse(Site.json_for(Guardian.new))
|
|
|
|
expect(data["auth_providers"].map { |a| a["name"] }).to contain_exactly("facebook", "twitter")
|
2018-07-31 23:18:50 +08:00
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
end
|