docbrown/spec/requests/follows_create_spec.rb
Daniel Uber d145149de7
Clear the cached settings before testing the rate limiter (#13669)
* Override the Settings::RateLimit.follow_count_daily

An interaction was occurring when the data update script test to move
these settings was run prior to the rate limit test - where the rate
limiter was created with the value 23 for the follow count, but the
settings module was reporting 500.

Stub out the database setting to an explicit value - and allow the
user follow count to be one more (so that we trigger the rate limit
and respond with a 429).

* Clear the settings cache before reading the values

This appears to correct the modification made (and reverted?) in the
data update test without requiring more changes to the context (which
remains as it was).
2021-05-05 14:34:22 -05:00

41 lines
1.1 KiB
Ruby

require "rails_helper"
RSpec.describe "Follows #create", type: :request do
let(:user) { create(:user) }
let(:user_2) { create(:user) }
let(:headers) { { "Content-Type": "application/json", Accept: "application/json" } }
let(:follow_payload) do
{
followable_type: "User",
followable_id: user_2.id,
verb: "follow"
}.to_json
end
before do
sign_in user
Settings::RateLimit.clear_cache
end
context "when rate limit has been hit" do
before do
rate_limit_checker = RateLimitChecker.new(user)
allow(rate_limit_checker)
.to receive(:user_today_follow_count)
.and_return(Settings::RateLimit.follow_count_daily + 1)
allow(RateLimitChecker)
.to receive(:new)
.and_return(rate_limit_checker)
end
it "returns an error for too many follows in a day" do
post "/follows", headers: headers, params: follow_payload
json_response = JSON.parse(response.body)
expect(response).to have_http_status(:too_many_requests)
expect(json_response["error"]).to eq("Daily account follow limit reached!")
end
end
end