Create tests to PingAdmins (#4169) [ci skip]

This commit is contained in:
Allan Klaus 2019-10-03 12:18:53 -03:00 committed by Mac Siri
parent 5da6c4e1c7
commit 4402cc212c
4 changed files with 54 additions and 3 deletions

View file

@ -130,8 +130,8 @@ variable :RECAPTCHA_SECRET, :String, default: "Optional"
variable :RECAPTCHA_SITE, :String, default: "Optional"
# Slack for customer alerts
variable :SLACK_CHANNEL, :String, default: "Optional"
variable :SLACK_WEBHOOK_URL, :String, default: "Optional"
variable :SLACK_CHANNEL, :String, default: ""
variable :SLACK_WEBHOOK_URL, :String, default: ""
# Stripe for payment system
variable :STRIPE_PUBLISHABLE_KEY, :String, default: "Optional"

View file

@ -9,7 +9,7 @@ class PingAdmins
end
def call
return unless user && Rails.env.production?
return unless user
SlackBot.ping(
"Rate limit exceeded (#{action}). https://dev.to#{user.path}",

View file

@ -13,6 +13,7 @@ def create_normal_notifier
end
def create_test_channel_notifier
return create_stubbed_notifier if ApplicationConfig["SLACK_WEBHOOK_URL"].blank?
Slack::Notifier.new(
ApplicationConfig["SLACK_WEBHOOK_URL"],
channel: "#test",

View file

@ -0,0 +1,50 @@
require "rails_helper"
RSpec.describe PingAdmins do
let(:user) { build_stubbed(:user) }
describe "#call" do
subject(:ping_admin_call) { described_class.call(user) }
before { allow(SlackBot).to receive(:ping) }
context "when user isn't nil" do
let(:action) { "unknown" }
let(:message_expected) { "Rate limit exceeded (#{action}). https://dev.to#{user.path}" }
it "calls SlackBot.ping" do
ping_admin_call
expect(SlackBot).to have_received(:ping).with(message_expected,
channel: "abuse-reports",
username: "rate_limit",
icon_emoji: ":hand:")
end
end
context "when user is nil" do
let(:user) { nil }
it "doesnt call SlackBot.ping" do
expect(ping_admin_call).to be_nil
expect(SlackBot).not_to have_received(:ping)
end
end
context "when receive action" do
subject(:ping_admin_call) { described_class.call(user, action) }
let(:action) { "any-action" }
let(:message_expected) { "Rate limit exceeded (#{action}). https://dev.to#{user.path}" }
it "calls SlackBot.ping" do
ping_admin_call
expect(SlackBot).to have_received(:ping).with(message_expected,
channel: "abuse-reports",
username: "rate_limit",
icon_emoji: ":hand:")
end
end
end
end