mirror of
https://github.com/discourse/discourse.git
synced 2025-06-11 08:43:45 +08:00
Add specs for RateLimiter::LimitExceeded#description
.
This commit is contained in:
@ -10,15 +10,15 @@ class RateLimiter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
time_left = ""
|
time_left =
|
||||||
if @available_in <= 3
|
if @available_in <= 3
|
||||||
time_left = I18n.t("rate_limiter.short_time")
|
I18n.t("rate_limiter.short_time")
|
||||||
elsif @available_in < 1.minute.to_i
|
elsif @available_in < 1.minute.to_i
|
||||||
time_left = I18n.t("rate_limiter.seconds", count: @available_in)
|
I18n.t("rate_limiter.seconds", count: @available_in)
|
||||||
elsif @available_in < 1.hour.to_i
|
elsif @available_in < 1.hour.to_i
|
||||||
time_left = I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
|
I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
|
||||||
else
|
else
|
||||||
time_left = I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
|
I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
|
||||||
end
|
end
|
||||||
|
|
||||||
if @type.present?
|
if @type.present?
|
||||||
|
20
spec/components/rate_limiter/limit_exceeded_spec.rb
Normal file
20
spec/components/rate_limiter/limit_exceeded_spec.rb
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe RateLimiter::LimitExceeded do
|
||||||
|
describe '#description' do
|
||||||
|
it 'should return the right description' do
|
||||||
|
[
|
||||||
|
[3, I18n.t("rate_limiter.short_time")],
|
||||||
|
[59, I18n.t("rate_limiter.seconds", count: 59)],
|
||||||
|
[3599, I18n.t("rate_limiter.minutes", count: 59)],
|
||||||
|
[7000, I18n.t("rate_limiter.hours", count: 1)]
|
||||||
|
].each do |available_in, time_left|
|
||||||
|
|
||||||
|
expect(described_class.new(available_in).description).to eq(I18n.t(
|
||||||
|
"rate_limiter.too_many_requests",
|
||||||
|
time_left: time_left
|
||||||
|
))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Reference in New Issue
Block a user