From 7d30cf707dd97953ad578a605cdcf1fface0d224 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Thu, 9 Jul 2020 20:44:17 -0300 Subject: [PATCH] DEV: Fix search rate limit tests --- spec/requests/search_controller_spec.rb | 54 +++++++++++++++++++++---- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rb index 8416ae4f7e9..6ea66b8385f 100644 --- a/spec/requests/search_controller_spec.rb +++ b/spec/requests/search_controller_spec.rb @@ -181,12 +181,31 @@ describe SearchController do end context 'rate limited' do - before do - SiteSetting.rate_limit_search_user = 3 + it 'rate limits anon searches per user' do SiteSetting.rate_limit_search_anon_user = 2 + RateLimiter.enable + RateLimiter.clear_all! + + 2.times do + get "/search/query.json", params: { + term: 'wookie' + } + + expect(response.status).to eq(200) + json = response.parsed_body + expect(json["grouped_search_result"]["error"]).to eq(nil) + end + + get "/search/query.json", params: { + term: 'wookie' + } + expect(response.status).to eq(200) + json = response.parsed_body + expect(json["grouped_search_result"]["error"]).to eq(I18n.t("rate_limiter.slow_down")) end - it 'rate limits searches' do + it 'rate limits anon searches globally' do + SiteSetting.rate_limit_search_anon_global = 2 RateLimiter.enable RateLimiter.clear_all! @@ -212,6 +231,7 @@ describe SearchController do before { sign_in(user) } it 'rate limits logged in searches' do + SiteSetting.rate_limit_search_user = 3 RateLimiter.enable RateLimiter.clear_all! @@ -274,13 +294,32 @@ describe SearchController do end context 'rate limited' do + it 'rate limits anon searches per user' do + SiteSetting.rate_limit_search_anon_user = 2 + RateLimiter.enable + RateLimiter.clear_all! + + 2.times do + get "/search.json", params: { + q: 'bantha' + } + + expect(response.status).to eq(200) + json = response.parsed_body + expect(json["grouped_search_result"]["error"]).to eq(nil) + end + + get "/search.json", params: { + q: 'bantha' + } + expect(response.status).to eq(200) + json = response.parsed_body + expect(json["grouped_search_result"]["error"]).to eq(I18n.t("rate_limiter.slow_down")) - before do - SiteSetting.rate_limit_search_user = 3 - SiteSetting.rate_limit_search_anon = 2 end - it 'rate limits searches' do + it 'rate limits anon searches globally' do + SiteSetting.rate_limit_search_anon_global = 2 RateLimiter.enable RateLimiter.clear_all! @@ -307,6 +346,7 @@ describe SearchController do before { sign_in(user) } it 'rate limits searches' do + SiteSetting.rate_limit_search_user = 3 RateLimiter.enable RateLimiter.clear_all!