This repository was archived by the owner on Jul 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
FEATURE: Provide generic modifier interface for rate limiting. #369
Merged
markvanlan
merged 5 commits into
discourse:main
from
zhanfengzeng:feature/solution-rate-modifier
May 22, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8a1f5e
FEATURE: Provide generic modifier interface for rate limiting.
zhanfengzeng acbad99
FIX: Fix spec use cases
zhanfengzeng 4cdc5c6
FIX: Correct spec execution error.
zhanfengzeng a7bb1b1
FIX: Fix the issue of spec case execution failure.
zhanfengzeng 639e012
TEST: use Plugin::Instance instead of mock object in rate limiter tests.
zhanfengzeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe DiscourseSolved::AnswerController do | ||
fab!(:user) | ||
fab!(:staff_user) { Fabricate(:admin) } | ||
fab!(:category) | ||
fab!(:topic) { Fabricate(:topic, category: category) } | ||
fab!(:p) { Fabricate(:post, topic: topic) } | ||
fab!(:solution_post) { Fabricate(:post, topic: topic) } | ||
|
||
before do | ||
SiteSetting.solved_enabled = true | ||
SiteSetting.allow_solved_on_all_topics = true | ||
category.custom_fields[DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD] = "true" | ||
category.save_custom_fields | ||
|
||
# Give permission to accept solutions | ||
user.update!(trust_level: 1) | ||
|
||
# Make user the topic creator so they can accept answers | ||
topic.update!(user_id: user.id) | ||
end | ||
|
||
describe "#accept" do | ||
context "with default rate limiting" do | ||
it "applies rate limits to regular users" do | ||
sign_in(user) | ||
|
||
# Should be rate limited | ||
RateLimiter.any_instance.expects(:performed!).raises(RateLimiter::LimitExceeded.new(60)) | ||
post "/solution/accept.json", params: { id: solution_post.id } | ||
expect(response.status).to eq(429) | ||
end | ||
|
||
it "does not apply rate limits to staff" do | ||
sign_in(staff_user) | ||
|
||
post "/solution/accept.json", params: { id: solution_post.id } | ||
expect(response.status).to eq(200) | ||
end | ||
end | ||
|
||
context "with plugin modifier" do | ||
it "allows plugins to bypass rate limiting" do | ||
sign_in(user) | ||
# Create a mock plugin instance with enabled? method | ||
plugin_instance = Object.new | ||
def plugin_instance.enabled? | ||
true | ||
end | ||
|
||
# Store the block in a variable so we can reference it for unregistration | ||
block = ->(_, _) { false } | ||
|
||
# Register modifier with proper parameters - using mock plugin instance instead of self | ||
DiscoursePluginRegistry.register_modifier( | ||
plugin_instance, | ||
:solved_answers_controller_run_rate_limiter, | ||
&block | ||
) | ||
|
||
post "/solution/accept.json", params: { id: solution_post.id } | ||
expect(response.status).to eq(200) | ||
post "/solution/accept.json", params: { id: solution_post.id } | ||
expect(response.status).to eq(200) | ||
|
||
# Unregister with the same plugin instance and block | ||
DiscoursePluginRegistry.unregister_modifier( | ||
plugin_instance, # Using the same mock plugin instance | ||
:solved_answers_controller_run_rate_limiter, | ||
&block | ||
) | ||
end | ||
end | ||
end | ||
describe "#unaccept" do | ||
before do | ||
# Setup an accepted solution | ||
sign_in(user) | ||
post "/solution/accept.json", params: { id: solution_post.id } | ||
expect(response.status).to eq(200) | ||
sign_out | ||
end | ||
|
||
it "applies rate limits to regular users" do | ||
sign_in(user) | ||
|
||
# Should be rate limited | ||
RateLimiter.any_instance.expects(:performed!).raises(RateLimiter::LimitExceeded.new(60)) | ||
post "/solution/unaccept.json", params: { id: solution_post.id } | ||
expect(response.status).to eq(429) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.