Tests for rate limiter and supporting methods
parent
ef25b66a28
commit
04691ce944
|
@ -37,7 +37,7 @@ module Api
|
|||
else
|
||||
Device
|
||||
.delay
|
||||
.connection_warning(username) if is_guest
|
||||
.connection_warning(username) if !is_guest
|
||||
Rollbar.error(WARNING % username)
|
||||
raise RateLimit, username
|
||||
end
|
||||
|
|
|
@ -15,6 +15,28 @@ describe Api::RmqUtilsController do
|
|||
password: token }
|
||||
end
|
||||
|
||||
it "limits users to 10 connections per 10 minutes" do
|
||||
empty_mail_bag
|
||||
u = credentials.fetch(:username)
|
||||
p = credentials.fetch(:password)
|
||||
Rails
|
||||
.cache
|
||||
.redis
|
||||
.set("mqtt_limiter:" + u.split("_").last, 0)
|
||||
|
||||
10.times do
|
||||
post :user_action, params: { username: u, password: p }
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include("allow")
|
||||
end
|
||||
|
||||
run_jobs_now do
|
||||
post :user_action, params: { username: u, password: p }
|
||||
expect(response.status).to eq(403)
|
||||
expect(response.body).not_to include("allow")
|
||||
end
|
||||
end
|
||||
|
||||
it "reports people trying to use ADMIN_PASSWORD on non-local servers" do
|
||||
k = "ADMIN_PASSWORD"
|
||||
old_pw = ENV[k]
|
||||
|
|
|
@ -20,6 +20,11 @@ describe LogService do
|
|||
FakeDeliveryInfo.new("bot.device_#{device_id}.logs")
|
||||
end
|
||||
|
||||
it "Sends errors to rollbar" do
|
||||
expect(Rollbar).to receive(:error)
|
||||
LogService.new().deliver("") # Will raise NoMethodError
|
||||
end
|
||||
|
||||
it "has a log_channel" do
|
||||
calls = Transport.current.log_channel.calls[:bind]
|
||||
expect(calls).to include(["amq.topic", { routing_key: "bot.*.logs" }])
|
||||
|
|
|
@ -2,7 +2,7 @@ require "spec_helper"
|
|||
|
||||
describe Device do
|
||||
let(:device) { FactoryBot.create(:device, users: [FactoryBot.create(:user)]) }
|
||||
let(:user) { device.users.first }
|
||||
let(:user) { device.users.first }
|
||||
|
||||
it "creates a token" do
|
||||
jwt = device.create_token
|
||||
|
@ -17,11 +17,11 @@ describe Device do
|
|||
end
|
||||
|
||||
it "destroys dependent devices" do
|
||||
bot_id = device.id
|
||||
bot_id = device.id
|
||||
user_id = user.id
|
||||
user.destroy
|
||||
user_results = User.where(id: user_id).first
|
||||
bot_results = Device.where(id: bot_id).first
|
||||
bot_results = Device.where(id: bot_id).first
|
||||
expect(bot_results).to be_nil
|
||||
expect(user_results).to be_nil
|
||||
end
|
||||
|
@ -35,17 +35,17 @@ describe Device do
|
|||
|
||||
it "sends specific users toast messages" do
|
||||
Transport.current.clear!
|
||||
hello = "Hello!"
|
||||
log = device.tell(hello)
|
||||
hello = "Hello!"
|
||||
log = device.tell(hello)
|
||||
json, info = Transport.current.connection.calls[:publish].last
|
||||
json = JSON.parse(json)
|
||||
json = JSON.parse(json)
|
||||
expect(info[:routing_key]).to eq("bot.device_#{device.id}.logs")
|
||||
expect(log.message).to eq(hello)
|
||||
expect(json["message"]).to eq(hello)
|
||||
end
|
||||
|
||||
it "allows for caching" do
|
||||
id = device.id
|
||||
id = device.id
|
||||
cache_key = Device::CACHE_KEY % id
|
||||
Rails.cache.delete(cache_key)
|
||||
expect(Rails.cache.exist?(cache_key)).to be false
|
||||
|
@ -57,10 +57,10 @@ describe Device do
|
|||
end
|
||||
|
||||
it "refreshes the cache" do
|
||||
id = device.id
|
||||
id = device.id
|
||||
cache_key = Device::CACHE_KEY % id
|
||||
Rails.cache.delete(cache_key)
|
||||
b4 = device.name
|
||||
b4 = device.name
|
||||
expect(Device.cached_find(id).name).to eq(b4)
|
||||
device.name = "blah"
|
||||
expect(Device.cached_find(id).name).to eq(b4)
|
||||
|
@ -73,8 +73,8 @@ describe Device do
|
|||
device.update_attributes!(throttled_until: nil)
|
||||
expect(device.throttled_until).to be(nil)
|
||||
five_minutes = ThrottlePolicy::TimePeriod.new(5.minutes, Time.now + 1.minute)
|
||||
rule = ThrottlePolicy::Rule.new(five_minutes, 500)
|
||||
violation = ThrottlePolicy::Violation.new(rule)
|
||||
rule = ThrottlePolicy::Rule.new(five_minutes, 500)
|
||||
violation = ThrottlePolicy::Violation.new(rule)
|
||||
device.maybe_throttle(violation)
|
||||
expect(device.throttled_until).to eq(violation.ends_at)
|
||||
end
|
||||
|
@ -98,7 +98,7 @@ describe Device do
|
|||
π = FactoryBot.create(:log, device: π€, channels: ["fatal_email"])
|
||||
π = FactoryBot.create(:log, device: π€, channels: ["toast"])
|
||||
results = π€.unsent_routine_emails
|
||||
expect(results).to include(π§)
|
||||
expect(results).to include(π§)
|
||||
expect(results).to_not include(π)
|
||||
expect(results).to_not include(π)
|
||||
end
|
||||
|
|
Loadingβ¦
Reference in New Issue