2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-07-14 09:24:16 +08:00
|
|
|
|
|
|
|
describe Site do
|
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-07-28 09:20:09 +08:00
|
|
|
.order(:name)
|
2018-07-12 12:18:21 +08:00
|
|
|
.pluck(:id, :name)
|
|
|
|
.map { |id, n| { "theme_id" => id, "name" => n, "default" => id == SiteSetting.default_theme_id } }
|
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)
|
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)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
it "omits categories users can not write to from the category list" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Site.new(Guardian.new(user)).categories.count).to eq(2)
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
category.set_permissions(everyone: :create_post)
|
2013-07-14 09:24:16 +08:00
|
|
|
category.save
|
|
|
|
|
2014-02-25 03:52:21 +08:00
|
|
|
guardian = Guardian.new(user)
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Site.new(guardian)
|
2013-10-24 07:05:51 +08:00
|
|
|
.categories
|
2017-07-28 09:20:09 +08:00
|
|
|
.keep_if { |c| c.name == category.name }
|
2013-10-24 07:05:51 +08:00
|
|
|
.first
|
2015-01-06 00:04:23 +08:00
|
|
|
.permission)
|
2017-07-28 09:20:09 +08:00
|
|
|
.not_to eq(CategoryGroup.permission_types[:full])
|
2014-02-25 03:52:21 +08:00
|
|
|
|
|
|
|
# If a parent category is not visible, the child categories should not be returned
|
2017-07-28 09:20:09 +08:00
|
|
|
category.set_permissions(staff: :full)
|
2014-02-25 03:52:21 +08:00
|
|
|
category.save
|
|
|
|
|
|
|
|
sub_category = Fabricate(:category, parent_category_id: category.id)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Site.new(guardian).categories).not_to include(sub_category)
|
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
|
|
|
|
|
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
|