diff --git a/spec/requests/answer_controller_spec.rb b/spec/requests/answer_controller_spec.rb index 21b02e5..746d651 100644 --- a/spec/requests/answer_controller_spec.rb +++ b/spec/requests/answer_controller_spec.rb @@ -28,11 +28,7 @@ describe DiscourseSolved::AnswerController do it "applies rate limits to regular users" do sign_in(user) - # First request should succeed - post "/solution/accept.json", params: { id: solution_post.id } - expect(response.status).to eq(200) - - # Second request should be rate limited + # 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) @@ -41,38 +37,43 @@ describe DiscourseSolved::AnswerController do it "does not apply rate limits to staff" do sign_in(staff_user) - # Staff can make multiple requests without hitting limits - 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) end end context "with plugin modifier" do - it "allows plugins to bypass rate limiting" do - sign_in(user) - # Store the block in a variable so we can reference it for unregistration - block = ->(_, _) { false } - # Register modifier with proper parameters - plugin instance (self) and name - DiscoursePluginRegistry.register_modifier( - self, - :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( - self, # plugin_instance parameter - :solved_answers_controller_run_rate_limiter, # name parameter - &block # same block used for registration - ) - end - end + 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