From 4402cc212cb3efea371f6bd6657cbaf3787992cb Mon Sep 17 00:00:00 2001 From: Allan Klaus Date: Thu, 3 Oct 2019 12:18:53 -0300 Subject: [PATCH] Create tests to PingAdmins (#4169) [ci skip] --- Envfile | 4 +-- app/services/ping_admins.rb | 2 +- config/initializers/slack_notifier.rb | 1 + spec/services/ping_admins_spec.rb | 50 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 spec/services/ping_admins_spec.rb diff --git a/Envfile b/Envfile index 640400ed8..7d15746ff 100644 --- a/Envfile +++ b/Envfile @@ -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" diff --git a/app/services/ping_admins.rb b/app/services/ping_admins.rb index 3e621e99e..2cc860e40 100644 --- a/app/services/ping_admins.rb +++ b/app/services/ping_admins.rb @@ -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}", diff --git a/config/initializers/slack_notifier.rb b/config/initializers/slack_notifier.rb index e51cbe725..d00b6a7b4 100644 --- a/config/initializers/slack_notifier.rb +++ b/config/initializers/slack_notifier.rb @@ -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", diff --git a/spec/services/ping_admins_spec.rb b/spec/services/ping_admins_spec.rb new file mode 100644 index 000000000..d94bcf12e --- /dev/null +++ b/spec/services/ping_admins_spec.rb @@ -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