FIX: Don't error out on nested top topic period param (#29275)

We're expecting the period param to be something that neatly coerces into a symbol. If we receive something like a nested parameter, this will blow up.

This commit raises an InvalidParameters exception in the case of a non-stringy period parameter.
This commit is contained in:
Ted Johansson 2024-10-21 10:44:43 +08:00 committed by GitHub
parent 698748bfec
commit fc2093fc7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 5 deletions

View File

@ -45,11 +45,12 @@ class TopTopic < ActiveRecord::Base
end
def self.validate_period(period)
if period.blank? || !periods.include?(period.to_sym)
raise Discourse::InvalidParameters.new(
"Invalid period. Valid periods are #{periods.join(", ")}",
)
end
@invalid_period_error ||=
Discourse::InvalidParameters.new("Invalid period. Valid periods are #{periods.join(", ")}")
raise @invalid_period_error if period.blank? || !periods.include?(period.to_sym)
rescue NoMethodError
raise @invalid_period_error
end
private

View File

@ -34,6 +34,36 @@ RSpec.describe TopTopic do
end
end
describe ".validate_period" do
context "when passing a valid period" do
it do
expect { described_class.validate_period(described_class.periods.first) }.not_to raise_error
end
end
context "when passing a blank value" do
it do
expect { described_class.validate_period(nil) }.to raise_error(Discourse::InvalidParameters)
end
end
context "when passing an invalid period" do
it do
expect { described_class.validate_period("bi-weekly") }.to raise_error(
Discourse::InvalidParameters,
)
end
end
context "when passing a non-string value" do
it do
expect { described_class.validate_period(ActionController::Parameters) }.to raise_error(
Discourse::InvalidParameters,
)
end
end
end
describe "#compute_top_score_for" do
fab!(:user)
fab!(:coding_horror)