This commit adds feature of sending alerts when the total consumption exceeds a user-defined threshold. We associate each alert to a user who created it and send the email to that user when the alert is triggered. An alternative would be allowing users to specify an email address to send the alert to, but that opens an attack vector where a user could spam another user with alerts. This vector can be mitigated by requiring the user to confirm the email address, but implementing a whole confirmation workflow would significantly increase the effort required to implement this feature.
20 lines
847 B
Ruby
20 lines
847 B
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "../model/spec_helper"
|
|
|
|
RSpec.describe Prog::CheckUsageAlerts do
|
|
subject(:cua) {
|
|
described_class.new(Strand.new(prog: "CheckUsageAlerts"))
|
|
}
|
|
|
|
describe "#wait" do
|
|
it "triggers alerts if usage is exceeded given threshold" do
|
|
exceeded = instance_double(UsageAlert, limit: 100, project: instance_double(Project, current_invoice: instance_double(Invoice, content: {"cost" => 1000})))
|
|
not_exceeded = instance_double(UsageAlert, limit: 100, project: instance_double(Project, current_invoice: instance_double(Invoice, content: {"cost" => 10})))
|
|
expect(UsageAlert).to receive(:where).and_return([exceeded, not_exceeded])
|
|
expect(exceeded).to receive(:trigger)
|
|
expect(not_exceeded).not_to receive(:trigger)
|
|
expect { cua.wait }.to nap(5 * 60)
|
|
end
|
|
end
|
|
end
|