Create tests to PingAdmins (#4169) [ci skip]
This commit is contained in:
parent
5da6c4e1c7
commit
4402cc212c
4 changed files with 54 additions and 3 deletions
4
Envfile
4
Envfile
|
|
@ -130,8 +130,8 @@ variable :RECAPTCHA_SECRET, :String, default: "Optional"
|
||||||
variable :RECAPTCHA_SITE, :String, default: "Optional"
|
variable :RECAPTCHA_SITE, :String, default: "Optional"
|
||||||
|
|
||||||
# Slack for customer alerts
|
# Slack for customer alerts
|
||||||
variable :SLACK_CHANNEL, :String, default: "Optional"
|
variable :SLACK_CHANNEL, :String, default: ""
|
||||||
variable :SLACK_WEBHOOK_URL, :String, default: "Optional"
|
variable :SLACK_WEBHOOK_URL, :String, default: ""
|
||||||
|
|
||||||
# Stripe for payment system
|
# Stripe for payment system
|
||||||
variable :STRIPE_PUBLISHABLE_KEY, :String, default: "Optional"
|
variable :STRIPE_PUBLISHABLE_KEY, :String, default: "Optional"
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class PingAdmins
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
return unless user && Rails.env.production?
|
return unless user
|
||||||
|
|
||||||
SlackBot.ping(
|
SlackBot.ping(
|
||||||
"Rate limit exceeded (#{action}). https://dev.to#{user.path}",
|
"Rate limit exceeded (#{action}). https://dev.to#{user.path}",
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ def create_normal_notifier
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_test_channel_notifier
|
def create_test_channel_notifier
|
||||||
|
return create_stubbed_notifier if ApplicationConfig["SLACK_WEBHOOK_URL"].blank?
|
||||||
Slack::Notifier.new(
|
Slack::Notifier.new(
|
||||||
ApplicationConfig["SLACK_WEBHOOK_URL"],
|
ApplicationConfig["SLACK_WEBHOOK_URL"],
|
||||||
channel: "#test",
|
channel: "#test",
|
||||||
|
|
|
||||||
50
spec/services/ping_admins_spec.rb
Normal file
50
spec/services/ping_admins_spec.rb
Normal 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
|
||||||
Loading…
Add table
Reference in a new issue