discourse/spec/models/trust_level_setting_spec.rb
Martin Brennan e8deed874b
FIX: Do not allow setting admin and staff for TrustLevelSetting (#25107)
This fixes an issue where any string for an enum site setting
(such as TrustLevelSetting) would be converted to an integer
if the default value for the enum was an integer. This is an
issue because things like "admin" and "staff" would get silently
converted to 0 which is "valid" because it's TrustLevel[0],
but it's unexpected behaviour. It's best to just let the site
setting validator catch this broken value.
2024-01-03 16:55:28 +10:00

47 lines
1.7 KiB
Ruby

# frozen_string_literal: true
RSpec.describe TrustLevelSetting do
describe ".values" do
after { I18n.reload! }
it "returns translated names" do
TranslationOverride.upsert!(I18n.locale, "js.trust_levels.names.newuser", "New Member")
value = TrustLevelSetting.values.first
expect(value[:name]).to eq(
I18n.t("js.trust_levels.detailed_name", level: 0, name: "New Member"),
)
expect(value[:value]).to eq(0)
end
end
describe ".valid_value?" do
let(:deprecated_test) { "#{Rails.root}/spec/fixtures/site_settings/deprecated_test.yml" }
before { SiteSetting.load_settings(deprecated_test) }
it "allows all trust levels as valid values" do
expect(TrustLevelSetting.valid_value?(TrustLevel[0])).to eq(true)
expect(TrustLevelSetting.valid_value?(TrustLevel[1])).to eq(true)
expect(TrustLevelSetting.valid_value?(TrustLevel[2])).to eq(true)
expect(TrustLevelSetting.valid_value?(TrustLevel[3])).to eq(true)
expect(TrustLevelSetting.valid_value?(TrustLevel[4])).to eq(true)
expect(TrustLevelSetting.valid_value?(20)).to eq(false)
end
it "does not allow 'admin' or 'staff' as valid values" do
expect(TrustLevelSetting.valid_value?("admin")).to eq(false)
expect(TrustLevelSetting.valid_value?("staff")).to eq(false)
end
it "does not allow setting 'admin' or 'staff' as valid values" do
expect { SiteSetting.min_trust_level_to_allow_invite = "admin" }.to raise_error(
Discourse::InvalidParameters,
)
expect { SiteSetting.min_trust_level_to_allow_invite = "staff" }.to raise_error(
Discourse::InvalidParameters,
)
end
end
end