discourse/spec/integration/rate_limiting_spec.rb
Sam Saffron 4ea21fa2d0 DEV: use #frozen_string_literal: true on all spec
This change both speeds up specs (less strings to allocate) and helps catch
cases where methods in Discourse are mutating inputs.

Overall we will be migrating everything to use #frozen_string_literal: true
it will take a while, but this is the first and safest move in this direction
2019-04-30 10:27:42 +10:00

58 lines
1.3 KiB
Ruby

# encoding: UTF-8
# frozen_string_literal: true
require 'rails_helper'
describe 'rate limiter integration' do
before do
RateLimiter.enable
RateLimiter.clear_all!
end
after do
RateLimiter.disable
end
it "will clear the token cookie if invalid" do
name = Auth::DefaultCurrentUserProvider::TOKEN_COOKIE
# we try 11 times because the rate limit is 10
11.times {
cookies[name] = SecureRandom.hex
get '/categories.json'
expect(response.cookies.has_key?(name)).to eq(true)
expect(response.cookies[name]).to be_nil
}
end
it 'can cleanly limit requests and sets a Retry-After header' do
freeze_time
#request.set_header("action_dispatch.show_exceptions", true)
admin = Fabricate(:admin)
api_key = Fabricate(:api_key, key: SecureRandom.hex, user: admin)
global_setting :max_admin_api_reqs_per_key_per_minute, 1
get '/admin/api/keys.json', params: {
api_key: api_key.key,
api_username: admin.username
}
expect(response.status).to eq(200)
get '/admin/api/keys.json', params: {
api_key: api_key.key,
api_username: admin.username
}
expect(response.status).to eq(429)
data = JSON.parse(response.body)
expect(response.headers['Retry-After']).to eq(60)
expect(data["extras"]["wait_seconds"]).to eq(60)
end
end