A platform for community discussion. Free, open, simple.
Revert "DEV: Replace MissingMailgunApiKey check with comprehensive Em… (#38038)
David Taylorfd54f2f493f3
diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rbindex 60ba6178dfd..763c31d07aa 100644--- a/app/controllers/webhooks_controller.rb+++ b/app/controllers/webhooks_controller.rb@@ -22,7 +22,9 @@ class WebhooksController < ActionController::Baseif SiteSetting.sendgrid_verification_key.present?return signature_failure if !valid_sendgrid_signature?else- warn_unauthenticated_webhook("SendGrid", "sendgrid_verification_key")+ Rails.logger.warn(+ "Received a Sendgrid webhook, but no verification key has been configured. This is unsafe behaviour and will be disallowed in the future.",+ )endevents = params["_json"] || [params]@@ -53,7 +55,9 @@ class WebhooksController < ActionController::Baseif SiteSetting.mailjet_webhook_token.present?return signature_failure if !valid_mailjet_token?else- warn_unauthenticated_webhook("Mailjet", "mailjet_webhook_token")+ Rails.logger.warn(+ "Received a Mailjet webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",+ )endevents = params["_json"] || [params]@@ -73,11 +77,7 @@ class WebhooksController < ActionController::Baseenddef mailpace- if SiteSetting.mailpace_verification_key.present?- return signature_failure if !valid_mailpace_signature?- else- warn_unauthenticated_webhook("Mailpace", "mailpace_verification_key")- end+ # see https://docs.mailpace.com/guide/webhooks#email-eventsmessage_id = Email::MessageIdService.message_id_clean(params["payload"]["message_id"])to_address = params["payload"]["to"]@@ -97,7 +97,9 @@ class WebhooksController < ActionController::Baseif SiteSetting.mandrill_authentication_key.present?return signature_failure if !valid_mandrill_signature?else- warn_unauthenticated_webhook("Mandrill", "mandrill_authentication_key")+ Rails.logger.warn(+ "Received a Mandrill webhook, but no authentication key has been configured. This is unsafe behaviour and will be disallowed in the future.",+ )endJSON@@ -128,13 +130,18 @@ class WebhooksController < ActionController::Baseif SiteSetting.postmark_webhook_token.present?return signature_failure if !valid_postmark_token?else- warn_unauthenticated_webhook("Postmark", "postmark_webhook_token")+ Rails.logger.warn(+ "Received a Postmark webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",+ )end+ # see https://postmarkapp.com/developer/webhooks/bounce-webhook#bounce-webhook-data+ # and https://postmarkapp.com/developer/api/bounce-api#bounce-types+message_id = params["MessageID"]to_address = params["Email"]-- case params["Type"]+ type = params["Type"]+ case typewhen "HardBounce", "SpamNotification", "SpamComplaint"process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)when "SoftBounce"@@ -148,7 +155,9 @@ class WebhooksController < ActionController::Baseif SiteSetting.sparkpost_webhook_token.present?return signature_failure if !valid_sparkpost_token?else- warn_unauthenticated_webhook("SparkPost", "sparkpost_webhook_token")+ Rails.logger.warn(+ "Received a Sparkpost webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",+ )endevents = params["_json"] || [params]@@ -163,6 +172,7 @@ class WebhooksController < ActionController::Basebounce_class = bounce_class.to_i+ # bounce class definitions: https://support.sparkpost.com/customer/portal/articles/1929896if bounce_class < 80if bounce_class == 10 || bounce_class == 25 || bounce_class == 30process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)@@ -191,14 +201,6 @@ class WebhooksController < ActionController::Baseprivate- def warn_unauthenticated_webhook(provider, setting)- Discourse.deprecate(- "Received a #{provider} webhook, but the `#{setting}` setting is not configured. Unauthenticated webhooks is unsafe behavior and will be rejected in a future version.",- since: "2026.2",- drop_from: "2026.5",- )- end-def signature_failurerender body: nil, status: :not_acceptableend@@ -208,16 +210,22 @@ class WebhooksController < ActionController::Baseenddef valid_mailgun_signature?(token, timestamp, signature)+ # token is a random 50 characters stringreturn false if token.blank? || token.size != 50+ # prevent replay attackskey = "mailgun_token_#{token}"return false unless Discourse.redis.setnx(key, 1)Discourse.redis.expire(key, 10.minutes)+ # ensure timestamp isn't too far from current timereturn false if (Time.at(timestamp.to_i) - Time.now).abs > 12.hours.to_i- digest = OpenSSL::HMAC.hexdigest("SHA256", SiteSetting.mailgun_api_key, "#{timestamp}#{token}")- ActiveSupport::SecurityUtils.secure_compare(signature, digest)+ # check the signature+ ActiveSupport::SecurityUtils.secure_compare(+ signature,+ OpenSSL::HMAC.hexdigest("SHA256", SiteSetting.mailgun_api_key, "#{timestamp}#{token}"),+ )enddef handle_mailgun_legacy(params)@@ -309,8 +317,12 @@ class WebhooksController < ActionController::Basepayload += valueend- digest = OpenSSL::HMAC.digest("sha1", SiteSetting.mandrill_authentication_key, payload)- ActiveSupport::SecurityUtils.secure_compare(signature, Base64.strict_encode64(digest))+ payload_signature =+ OpenSSL::HMAC.digest("sha1", SiteSetting.mandrill_authentication_key, payload)+ ActiveSupport::SecurityUtils.secure_compare(+ signature,+ Base64.strict_encode64(payload_signature),+ )enddef valid_postmark_token?@@ -318,27 +330,6 @@ class WebhooksController < ActionController::BaseActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.postmark_webhook_token)end- def valid_mailpace_signature?- signature = request.headers["X-MailPace-Signature"]- return false if signature.blank?-- request.body.rewind- payload = request.body.read-- begin- decoded_key = Base64.strict_decode64(SiteSetting.mailpace_verification_key)- public_key = OpenSSL::PKey.read(decoded_key)- rescue StandardError- Rails.logger.error("Invalid Mailpace webhook verification key")- return false- end-- decoded_signature = Base64.strict_decode64(signature)- public_key.verify(nil, decoded_signature, payload)- rescue StandardError- false- end-def valid_sparkpost_token?return false if params[:t].blank?ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.sparkpost_webhook_token)diff --git a/app/models/problem_check.rb b/app/models/problem_check.rbindex dd2e4633ad9..4e9c42908bd 100644--- a/app/models/problem_check.rb+++ b/app/models/problem_check.rb@@ -77,7 +77,7 @@ class ProblemCheckProblemCheck::GroupEmailCredentials,ProblemCheck::HostNames,ProblemCheck::ImageMagick,- ProblemCheck::EmailBounceHandling,+ ProblemCheck::MissingMailgunApiKey,ProblemCheck::OutOfDateThemes,ProblemCheck::PollPop3Timeout,ProblemCheck::PollPop3AuthError,diff --git a/app/services/problem_check/email_bounce_handling.rb b/app/services/problem_check/email_bounce_handling.rbdeleted file mode 100644index 14cf68ca9f3..00000000000--- a/app/services/problem_check/email_bounce_handling.rb+++ /dev/null@@ -1,39 +0,0 @@-# frozen_string_literal: true--class ProblemCheck::EmailBounceHandling < ProblemCheck- self.priority = "low"-- PROVIDERS = [- { address: "smtp.mailgun.org", setting: :mailgun_api_key, name: "Mailgun" },- { address: "smtp.sendgrid.net", setting: :sendgrid_verification_key, name: "SendGrid" },- { address: /\.mailjet\.com\z/, setting: :mailjet_webhook_token, name: "Mailjet" },- { address: "smtp.mandrillapp.com", setting: :mandrill_authentication_key, name: "Mandrill" },- { address: "smtp.postmarkapp.com", setting: :postmark_webhook_token, name: "Postmark" },- { address: "smtp.sparkpostmail.com", setting: :sparkpost_webhook_token, name: "SparkPost" },- { address: "smtp.mailpace.com", setting: :mailpace_verification_key, name: "Mailpace" },- { address: /email-smtp\..+\.amazonaws\.com\z/, name: "AWS SES" },- ].freeze-- def call- if SiteSetting.reply_by_email_enabled && Email::Sender.bounceable_reply_address?- return no_problem- end-- smtp_address = ActionMailer::Base.smtp_settings[:address].to_s-- if provider = PROVIDERS.find { |p| p[:address] === smtp_address }- return no_problem if provider[:setting].blank?- return no_problem if SiteSetting.public_send(provider[:setting]).present?-- problem(- override_key: "dashboard.problem.email_bounce_handling.webhook_key_missing",- override_data: {- provider: provider[:name],- setting: provider[:setting],- },- )- else- problem(override_key: "dashboard.problem.email_bounce_handling.no_bounce_handling")- end- end-enddiff --git a/app/services/problem_check/missing_mailgun_api_key.rb b/app/services/problem_check/missing_mailgun_api_key.rbnew file mode 100644index 00000000000..4e399faefb1--- /dev/null+++ b/app/services/problem_check/missing_mailgun_api_key.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++class ProblemCheck::MissingMailgunApiKey < ProblemCheck+ self.priority = "low"++ def call+ return no_problem if !SiteSetting.reply_by_email_enabled+ return no_problem if ActionMailer::Base.smtp_settings[:address] != "smtp.mailgun.org"+ return no_problem if SiteSetting.mailgun_api_key.present?++ problem+ end+enddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex f4637221361..15bf5e6c880 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1710,9 +1710,7 @@ en:email_polling_errored_recently:one: "Email polling has generated an error in the past 24 hours. Look at <a href='%{base_path}/logs' target='_blank'>the logs</a> for more details."other: "Email polling has generated %{count} errors in the past 24 hours. Look at <a href='%{base_path}/logs' target='_blank'>the logs</a> for more details."- email_bounce_handling:- webhook_key_missing: "The server sends emails via %{provider} but the <code>%{setting}</code> setting is not configured. Without it, bounce webhooks will be rejected and bounced emails won't be tracked. Configure it in <a href='%{base_path}/admin/site_settings'>Site Settings</a>."- no_bounce_handling: "No bounce handling detected. Configure reply by email with VERP support, or set up bounce webhooks with a supported email provider. Without bounce handling, you won't be notified of email delivery failures."+ missing_mailgun_api_key: "The server is configured to send emails via Mailgun but you haven't provided an API key used to verify the webhook messages."qq_mail_smtp: "Your SMTP server (%{smtp_address}) is known to cause duplicate emails with Discourse. QQ Mail does not return proper acknowledgments, causing Discourse to retry sends that already succeeded. <a href='https://github.com/discourse/discourse/blob/main/docs/INSTALL-email.md' target='_blank'>See recommended alternatives</a>."bad_favicon_url: "The favicon is failing to load. Check your favicon setting in <a href='%{base_path}/admin/site_settings'>Site Settings</a>."poll_pop3_timeout: "Connection to the POP3 server is timing out. Incoming email could not be retrieved. Please check your <a href='%{base_path}/admin/site_settings/category/email'>POP3 settings</a> and service provider."@@ -2345,7 +2343,6 @@ en:mandrill_authentication_key: "Mandrill authentication key used to verify webhook messages."postmark_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/postmark?t=supersecret"sparkpost_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/sparkpost?t=supersecret"- mailpace_verification_key: "Verification key used to verify MailPace webhook messages."soft_bounce_score: "Bounce score added to the user when a temporary bounce happens."hard_bounce_score: "Bounce score added to the user when a permanent bounce happens."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex c210b0058cc..25f0ac0594e 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2019,9 +2019,6 @@ email:sparkpost_webhook_token:default: ""secret: true- mailpace_verification_key:- default: ""- secret: truebounce_score_threshold:client: truedefault: 4diff --git a/db/migrate/20260224152846_remove_stale_missing_mailgun_api_key_problem_check_tracker.rb b/db/migrate/20260224152846_remove_stale_missing_mailgun_api_key_problem_check_tracker.rbdeleted file mode 100644index 5e988a26dd7..00000000000--- a/db/migrate/20260224152846_remove_stale_missing_mailgun_api_key_problem_check_tracker.rb+++ /dev/null@@ -1,11 +0,0 @@-# frozen_string_literal: true--class RemoveStaleMissingMailgunApiKeyProblemCheckTracker < ActiveRecord::Migration[8.0]- def up- execute "DELETE FROM problem_check_trackers WHERE identifier = 'missing_mailgun_api_key'"- end-- def down- # no-op: can't restore deleted tracker rows- end-enddiff --git a/spec/requests/webhooks_controller_spec.rb b/spec/requests/webhooks_controller_spec.rbindex f03b606f9a4..bd4a52ec0ee 100644--- a/spec/requests/webhooks_controller_spec.rb+++ b/spec/requests/webhooks_controller_spec.rb@@ -5,19 +5,6 @@ RSpec.describe WebhooksController dofab!(:email) { "[email protected]" }fab!(:message_id) { "[email protected]" }- fab!(:user) { Fabricate(:user, email:) }- fab!(:email_log) { Fabricate(:email_log, user:, message_id:, to_address: email) }-- def expect_bounce(score:, error_code: nil)- email_log.reload- expect(email_log.bounced).to eq(true)- expect(email_log.bounce_error_code).to eq(error_code)- expect(email_log.user.user_stat.bounce_score).to eq(score)- end-- def expect_no_bounce- expect(email_log.reload.bounced).to eq(false)- enddescribe "#mailgun" dolet(:token) { "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de" }@@ -27,28 +14,15 @@ RSpec.describe WebhooksController dobefore doSiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493"- ActionController::Base.allow_forgery_protection = true+ ActionController::Base.allow_forgery_protection = true # Ensure the endpoint works, even with CSRF protection generally enabledendafter { ActionController::Base.allow_forgery_protection = false }- it "returns 406 when API key is missing" do- SiteSetting.mailgun_api_key = ""-- post "/webhooks/mailgun.json",- params: {- "token" => token,- "timestamp" => timestamp,- "event" => "dropped",- "recipient" => email,- "Message-Id" => "<#{message_id}>",- "signature" => signature,- }-- expect(response.status).to eq(406)- end+ it "works (deprecated)" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes legacy dropped events as hard bounces" dopost "/webhooks/mailgun.json",params: {"token" => token,@@ -62,27 +36,17 @@ RSpec.describe WebhooksController do}expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")- end- it "processes legacy transient bounces as soft bounces" do- post "/webhooks/mailgun.json",- params: {- "token" => token,- "timestamp" => timestamp,- "event" => "bounced",- "recipient" => email,- "Message-Id" => "<#{message_id}>",- "signature" => signature,- "error" => "smtp; 4.7.1 Temporary failure",- "code" => "4.7.1",- }-- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.7.1")+ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq("5.1.1")+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)end- it "processes new format temporary failures as soft bounces" do+ it "works (new)" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)+post "/webhooks/mailgun.json",params: {"signature" => {@@ -101,75 +65,58 @@ RSpec.describe WebhooksController do},},"delivery-status" => {- "code" => "4.7.1",+ "message" =>+ "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",+ "code" => "5.1.1",+ "description" => "",},}expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.7.1")++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq("5.1.1")+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)end- it "processes new format permanent failures as hard bounces" do- post "/webhooks/mailgun.json",- params: {- "signature" => {+ context "when readonly mode is enabled" do+ before { Discourse.enable_readonly_mode }++ it "returns 503" do+ user = Fabricate(:user, email:)+ email_log = Fabricate(:email_log, user:, message_id:, to_address: email)++ post "/webhooks/mailgun.json",+ params: {"token" => token,"timestamp" => timestamp,- "signature" => signature,- },- "event-data" => {- "event" => "failed",- "severity" => "permanent",+ "event" => "dropped","recipient" => email,- "message" => {- "headers" => {- "message-id" => message_id,- },- },- },- "delivery-status" => {+ "Message-Id" => "<#{message_id}>",+ "signature" => signature,+ "error" =>+ "smtp; 550-5.1.1 The email account that you tried to reach does not exist.","code" => "5.1.1",- },- }-- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")- end-- it "returns 503 in readonly mode" do- Discourse.enable_readonly_mode-- post "/webhooks/mailgun.json",- params: {- "token" => token,- "timestamp" => timestamp,- "event" => "dropped",- "recipient" => email,- "Message-Id" => "<#{message_id}>",- "signature" => signature,- "code" => "5.1.1",- }+ }- expect(response.status).to eq(503)- expect_no_bounce+ expect(response.status).to eq(503)+ expect(email_log.reload.bounced).to eq(false)+ endendenddescribe "#sendgrid" do- before do- SiteSetting.sendgrid_verification_key = "key"- WebhooksController.any_instance.stubs(:valid_sendgrid_signature?).returns(true)- end-- it "processes webhooks with a deprecation warning when verification key is missing" do- SiteSetting.sendgrid_verification_key = ""+ fab!(:user) { Fabricate(:user, email:) }+ fab!(:email_log) { Fabricate(:email_log, user:, message_id: message_id, to_address: email) }+ it "works" dopost "/webhooks/sendgrid.json",params: {"_json" => [{"email" => email,- "smtp-id" => "<#{message_id}>",+ "smtp-id" => "<[email protected]>","event" => "bounce","status" => "5.0.0",},@@ -177,396 +124,508 @@ RSpec.describe WebhooksController do}expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.0.0")- end- it "returns 406 when signature is invalid" do- WebhooksController.any_instance.stubs(:valid_sendgrid_signature?).returns(false)+ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq("5.0.0")+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)+ end+ it "sets the bounce error code to 5.1.2 when payload's `event` is `bounce`, `type` is `blocked` and `status` is blank" dopost "/webhooks/sendgrid.json",params: {"_json" => [{"email" => email,- "smtp-id" => "<#{message_id}>",+ "smtp-id" => "<[email protected]>","event" => "bounce",- "status" => "5.0.0",+ "type" => "blocked",},],}- expect(response.status).to eq(406)+ expect(response.status).to eq(200)++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq("5.1.2")end- it "processes hard bounces" do+ it "verifies signatures" do+ SiteSetting.sendgrid_verification_key =+ "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="+post "/webhooks/sendgrid.json",- params: {- "_json" => [- {- "email" => email,- "smtp-id" => "<#{message_id}>",- "event" => "bounce",- "status" => "5.0.0",- },- ],- }+ headers: {+ "X-Twilio-Email-Event-Webhook-Signature" =>+ "MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=",+ "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112502",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.0.0")end- it "processes soft bounces with transient failure status" do+ it "returns error if signature verification fails" do+ SiteSetting.sendgrid_verification_key =+ "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="+post "/webhooks/sendgrid.json",- params: {- "_json" => [- {- "email" => email,- "smtp-id" => "<#{message_id}>",- "event" => "bounce",- "status" => "4.0.0",- },- ],- }+ headers: {+ "X-Twilio-Email-Event-Webhook-Signature" =>+ "MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0=",+ "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112502",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.0.0")+ expect(response.status).to eq(406)end- it "processes dropped events as hard bounces" do+ it "returns error if signature is invalid" do+ SiteSetting.sendgrid_verification_key = "foo"+post "/webhooks/sendgrid.json",- params: {- "_json" => [- {- "email" => email,- "smtp-id" => "<#{message_id}>",- "event" => "dropped",- "status" => "5.0.0",- },- ],- }+ headers: {+ "X-Twilio-Email-Event-Webhook-Signature" =>+ "MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0=",+ "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112502",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.0.0")+ expect(response.status).to eq(406)end- it "defaults to error code 5.1.2 for blocked bounces without status" do+ it "returns error if signature header is missing" do+ SiteSetting.sendgrid_verification_key =+ "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="+post "/webhooks/sendgrid.json",- params: {- "_json" => [- {- "email" => email,- "smtp-id" => "<#{message_id}>",- "event" => "bounce",- "type" => "blocked",- },- ],- }+ headers: {+ "X-Twilio-Email-Event-Webhook-Timestamp" => "1600112492",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.2")+ expect(response.status).to eq(406)+ end++ it "returns error if timestamp header is missing" do+ SiteSetting.sendgrid_verification_key =+ "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=="++ post "/webhooks/sendgrid.json",+ headers: {+ "X-Twilio-Email-Event-Webhook-Signature" =>+ "MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=",+ },+ params:+ "[{\"email\":\"[email protected]\",\"event\":\"dropped\",\"reason\":\"Bounced Address\",\"sg_event_id\":\"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA\",\"sg_message_id\":\"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0\",\"smtp-id\":\"<[email protected]>\",\"timestamp\":1600112492}]\r\n"++ expect(response.status).to eq(406)endenddescribe "#mailjet" do- let(:bounce_params) do- { "event" => "bounce", "email" => email, "hard_bounce" => true, "CustomID" => message_id }- end+ it "works" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes webhooks with a deprecation warning when webhook token is missing" do- post "/webhooks/mailjet.json", params: bounce_params+ post "/webhooks/mailjet.json",+ params: {+ "event" => "bounce",+ "email" => email,+ "hard_bounce" => true,+ "CustomID" => message_id,+ }expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq(nil) # mailjet doesn't give us this+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)end- context "with a valid webhook token" do- before { SiteSetting.mailjet_webhook_token = "foo" }+ it "verifies signatures" do+ SiteSetting.mailjet_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes hard bounces" do- post "/webhooks/mailjet.json?t=foo", params: bounce_params+ post "/webhooks/mailjet.json?t=foo",+ params: {+ "event" => "bounce",+ "email" => email,+ "hard_bounce" => true,+ "CustomID" => message_id,+ }- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)- end+ expect(response.status).to eq(200)+ expect(email_log.reload.bounced).to eq(true)+ end- it "processes soft bounces" do- post "/webhooks/mailjet.json?t=foo",- params: [- {- "event" => "bounce",- "email" => email,- "hard_bounce" => false,- "CustomID" => message_id,- },- ].to_json,- headers: {- "CONTENT_TYPE" => "application/json",- }+ it "returns error if signature verification fails" do+ SiteSetting.mailjet_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score)- end+ post "/webhooks/mailjet.json?t=bar",+ params: {+ "event" => "bounce",+ "email" => email,+ "hard_bounce" => true,+ "CustomID" => message_id,+ }- it "rejects wrong token" do- post "/webhooks/mailjet.json?t=bar", params: bounce_params+ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)+ end- expect(response.status).to eq(406)- expect_no_bounce- end+ it "returns error if token param is missing" do+ SiteSetting.mailjet_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "rejects missing token param" do- post "/webhooks/mailjet.json", params: bounce_params+ post "/webhooks/mailjet.json",+ params: {+ "event" => "bounce",+ "email" => email,+ "hard_bounce" => true,+ "CustomID" => message_id,+ }- expect(response.status).to eq(406)- expect_no_bounce- end+ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)endenddescribe "#mailpace" do- let(:bounce_params) do- {- event: "email.bounced",- payload: {- status: "bounced",- to: email,- message_id: "<#{message_id}>",- },- }- end+ it "works" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes webhooks with a deprecation warning when verification key is missing" do- post "/webhooks/mailpace.json", params: bounce_params+ post "/webhooks/mailpace.json",+ params: {+ event: "email.bounced",+ payload: {+ status: "bounced",+ to: email,+ message_id: "<#{message_id}>",+ },+ }expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)- end- it "returns 406 when signature is invalid" do- SiteSetting.mailpace_verification_key = "key"- WebhooksController.any_instance.stubs(:valid_mailpace_signature?).returns(false)-- post "/webhooks/mailpace.json", params: bounce_params- expect(response.status).to eq(406)+ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq(nil) # mailpace doesn't give us this+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)end- context "with a valid verification key" do- before do- SiteSetting.mailpace_verification_key = "key"- WebhooksController.any_instance.stubs(:valid_mailpace_signature?).returns(true)- end+ it "soft bounces" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes hard bounces" do- post "/webhooks/mailpace.json", params: bounce_params-- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)- end+ post "/webhooks/mailpace.json",+ params: {+ event: "email.deferred",+ payload: {+ status: "deferred",+ to: email,+ message_id: "<#{message_id}>",+ },+ }- it "processes soft bounces" do- post "/webhooks/mailpace.json",- params: {- event: "email.deferred",- payload: {- status: "deferred",- to: email,- message_id: "<#{message_id}>",- },- }+ expect(response.status).to eq(200)- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score)- end+ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq(nil) # mailpace doesn't give us this+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)endenddescribe "#mandrill" do- def mandrill_events_json(event: "hard_bounce", diag: "5.1.1")- [- {- "event" => event,- "msg" => {- "email" => email,- "diag" => diag,- "bounce_description" =>- "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",- "metadata" => {- "message_id" => message_id,- },- },- },- ].to_json+ let(:payload) do+ "mandrill_events=%5B%7B%22event%22%3A%22hard_bounce%22%2C%22msg%22%3A%7B%22email%22%3A%22em%40il.com%22%2C%22diag%22%3A%225.1.1%22%2C%22bounce_description%22%3A%22smtp%3B+550-5.1.1+The+email+account+that+you+tried+to+reach+does+not+exist.%22%2C%22metadata%22%3A%7B%22message_id%22%3A%2212345%40il.com%22%7D%7D%7D%5D"+ end++ it "works" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)++ post "/webhooks/mandrill.json",+ params: {+ mandrill_events: [+ {+ "event" => "hard_bounce",+ "msg" => {+ "email" => email,+ "diag" => "5.1.1",+ :"bounce_description" =>+ "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",+ "metadata" => {+ "message_id" => message_id,+ },+ },+ },+ ].to_json,+ }++ expect(response.status).to eq(200)++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq("5.1.1")+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)end- it "processes webhooks with a deprecation warning when authentication key is missing" do- post "/webhooks/mandrill.json", params: { mandrill_events: mandrill_events_json }+ it "verifies signatures" do+ SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"++ post "/webhooks/mandrill.json",+ headers: {+ "X-Mandrill-Signature" => "Q5pCb903EjEqRZ99gZrlYKOfvIU=",+ },+ params: payloadexpect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")end- it "returns 406 when signature is invalid" do- SiteSetting.mandrill_authentication_key = "key"- WebhooksController.any_instance.stubs(:valid_mandrill_signature?).returns(false)+ it "returns error if signature verification fails" do+ SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"- post "/webhooks/mandrill.json", params: { mandrill_events: mandrill_events_json }+ post "/webhooks/mandrill.json", headers: { "X-Mandrill-Signature" => "foo" }, params: payloadexpect(response.status).to eq(406)end- context "with a valid authentication key" do- before do- SiteSetting.mandrill_authentication_key = "key"- WebhooksController.any_instance.stubs(:valid_mandrill_signature?).returns(true)- end+ it "returns error if signature is invalid" do+ SiteSetting.mandrill_authentication_key = "foo"- it "processes hard bounces" do- post "/webhooks/mandrill.json", params: { mandrill_events: mandrill_events_json }+ post "/webhooks/mandrill.json",+ headers: {+ "X-Mandrill-Signature" => "Q5pCb903EjEqRZ99gZrlYKOfvIU=",+ },+ params: payload- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")- end+ expect(response.status).to eq(406)+ end- it "processes soft bounces" do- post "/webhooks/mandrill.json",- params: {- mandrill_events: mandrill_events_json(event: "soft_bounce", diag: "4.7.1"),- }+ it "returns error if signature header is missing" do+ SiteSetting.mandrill_authentication_key = "wr_JeJNO9OI65RFDrvk3Zw"- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score, error_code: "4.7.1")- end+ post "/webhooks/mandrill.json", params: payload++ expect(response.status).to eq(406)endenddescribe "#mandrill_head" do- it "returns 200" do+ it "works" dohead "/webhooks/mandrill.json"+expect(response.status).to eq(200)endenddescribe "#postmark" do- let(:bounce_params) { { "Type" => "HardBounce", "MessageID" => message_id, "Email" => email } }+ it "works" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes webhooks with a deprecation warning when webhook token is missing" do- post "/webhooks/postmark.json", params: bounce_params+ post "/webhooks/postmark.json",+ params: {+ "Type" => "HardBounce",+ "MessageID" => message_id,+ "Email" => email,+ }+ expect(response.status).to eq(200)+ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq(nil) # postmark doesn't give us this+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)+ end++ it "soft bounces" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)++ post "/webhooks/postmark.json",+ params: {+ "Type" => "SoftBounce",+ "MessageID" => message_id,+ "Email" => email,+ }expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.bounce_error_code).to eq(nil) # postmark doesn't give us this+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)end- context "with a valid webhook token" do- before { SiteSetting.postmark_webhook_token = "foo" }+ it "verifies signatures" do+ SiteSetting.postmark_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes hard bounces" do- post "/webhooks/postmark.json?t=foo", params: bounce_params+ post "/webhooks/postmark.json?t=foo",+ params: {+ "Type" => "HardBounce",+ "MessageID" => message_id,+ "Email" => email,+ }- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)- end+ expect(response.status).to eq(200)+ expect(email_log.reload.bounced).to eq(true)+ end- it "processes soft bounces" do- post "/webhooks/postmark.json?t=foo",- params: {- "Type" => "SoftBounce",- "MessageID" => message_id,- "Email" => email,- }+ it "returns error if signature verification fails" do+ SiteSetting.postmark_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score)- end+ post "/webhooks/postmark.json?t=bar",+ params: {+ "Type" => "HardBounce",+ "MessageID" => message_id,+ "Email" => email,+ }- it "rejects wrong token" do- post "/webhooks/postmark.json?t=bar", params: bounce_params+ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)+ end- expect(response.status).to eq(406)- expect_no_bounce- end+ it "returns error if token param is missing" do+ SiteSetting.postmark_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "rejects missing token param" do- post "/webhooks/postmark.json", params: bounce_params+ post "/webhooks/postmark.json",+ params: {+ "Type" => "HardBounce",+ "MessageID" => message_id,+ "Email" => email,+ }- expect(response.status).to eq(406)- expect_no_bounce- end+ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)endenddescribe "#sparkpost" do- let(:bounce_params) do- {- "_json" => [- {- "msys" => {- "message_event" => {- "bounce_class" => 10,- "error_code" => "554",- "rcpt_to" => email,- "rcpt_meta" => {- "message_id" => message_id,- },- },- },- },- ],- }- end+ it "works" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes webhooks with a deprecation warning when webhook token is missing" do- post "/webhooks/sparkpost.json", params: bounce_params+ post "/webhooks/sparkpost.json",+ params: {+ "_json" => [+ {+ "msys" => {+ "message_event" => {+ "bounce_class" => 10,+ "error_code" => "554",+ "rcpt_to" => email,+ "rcpt_meta" => {+ "message_id" => message_id,+ },+ },+ },+ },+ ],+ }expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)end- context "with a valid webhook token" do- before { SiteSetting.sparkpost_webhook_token = "foo" }+ it "verifies signatures" do+ SiteSetting.sparkpost_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "processes hard bounces" do- post "/webhooks/sparkpost.json?t=foo", params: bounce_params+ post "/webhooks/sparkpost.json?t=foo",+ params: {+ "_json" => [+ {+ "msys" => {+ "message_event" => {+ "bounce_class" => 10,+ "error_code" => "554",+ "rcpt_to" => email,+ "rcpt_meta" => {+ "message_id" => message_id,+ },+ },+ },+ },+ ],+ }- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score)- end+ expect(response.status).to eq(200)+ expect(email_log.reload.bounced).to eq(true)+ end- it "processes soft bounces" do- post "/webhooks/sparkpost.json?t=foo",- params: {- "_json" => [- {- "msys" => {- "message_event" => {- "bounce_class" => 20,- "error_code" => "450",- "rcpt_to" => email,- "rcpt_meta" => {- "message_id" => message_id,- },+ it "returns error if signature verification fails" do+ SiteSetting.sparkpost_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)++ post "/webhooks/sparkpost.json?t=bar",+ params: {+ "_json" => [+ {+ "msys" => {+ "message_event" => {+ "bounce_class" => 10,+ "error_code" => "554",+ "rcpt_to" => email,+ "rcpt_meta" => {+ "message_id" => message_id,},},},- ],- }-- expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.soft_bounce_score)- end+ },+ ],+ }- it "rejects wrong token" do- post "/webhooks/sparkpost.json?t=bar", params: bounce_params+ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)+ end- expect(response.status).to eq(406)- expect_no_bounce- end+ it "returns error if token param is missing" do+ SiteSetting.sparkpost_webhook_token = "foo"+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)- it "rejects missing token param" do- post "/webhooks/sparkpost.json", params: bounce_params+ post "/webhooks/sparkpost.json",+ params: {+ "_json" => [+ {+ "msys" => {+ "message_event" => {+ "bounce_class" => 10,+ "error_code" => "554",+ "rcpt_to" => email,+ "rcpt_meta" => {+ "message_id" => message_id,+ },+ },+ },+ },+ ],+ }- expect(response.status).to eq(406)- expect_no_bounce- end+ expect(response.status).to eq(406)+ expect(email_log.reload.bounced).to eq(false)endend@@ -576,10 +635,10 @@ RSpec.describe WebhooksController do"Type" => "Notification","Message" => {"notificationType" => "Bounce",- "bounce" => {+ :"bounce" => {"bounceType" => "Permanent","reportingMTA" => "dns; email.example.com",- "bouncedRecipients" => [+ :"bouncedRecipients" => [{"emailAddress" => email,"status" => "5.1.1",@@ -592,7 +651,7 @@ RSpec.describe WebhooksController do"feedbackId" => "00000138111222aa-33322211-cccc-cccc-cccc-ddddaaaa068a-000000","remoteMtaIp" => "127.0.2.0",},- "mail" => {+ :"mail" => {"timestamp" => "2016-01-27T14:59:38.237Z","source" => "[email protected]","sourceArn" => "arn:aws:ses:us-east-1:888888888888:identity/example.com",@@ -631,14 +690,19 @@ RSpec.describe WebhooksController dobefore { Jobs.run_immediately! }- it "processes bounce notifications" do+ it "works" do+ user = Fabricate(:user, email: email)+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)+require "aws-sdk-sns"Aws::SNS::MessageVerifier.any_instance.stubs(:authentic?).with(payload).returns(true)post "/webhooks/aws.json", headers: { "RAW_POST_DATA" => payload }-expect(response.status).to eq(200)- expect_bounce(score: SiteSetting.hard_bounce_score, error_code: "5.1.1")++ email_log.reload+ expect(email_log.bounced).to eq(true)+ expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)endendenddiff --git a/spec/services/problem_check/email_bounce_handling_spec.rb b/spec/services/problem_check/email_bounce_handling_spec.rbdeleted file mode 100644index 4d99fad3bb4..00000000000--- a/spec/services/problem_check/email_bounce_handling_spec.rb+++ /dev/null@@ -1,150 +0,0 @@-# frozen_string_literal: true--RSpec.describe ProblemCheck::EmailBounceHandling do- subject(:check) { described_class.new }-- before do- SiteSetting.stubs(reply_by_email_enabled: reply_by_email_enabled)- Email::Sender.stubs(:bounceable_reply_address?).returns(bounceable_reply_address)- ActionMailer::Base.stubs(smtp_settings: { address: smtp_address })- end-- context "when reply by email is enabled with VERP" do- let(:reply_by_email_enabled) { true }- let(:bounceable_reply_address) { true }- let(:smtp_address) { "smtp.mailgun.org" }-- it { expect(check).to be_chill_about_it }- end-- context "when reply by email is enabled without VERP" do- let(:reply_by_email_enabled) { true }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.mailgun.org" }-- before { SiteSetting.stubs(mailgun_api_key: "") }-- it { expect(check).to have_a_problem }- end-- context "when reply by email is disabled with VERP address configured" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { true }- let(:smtp_address) { "smtp.mailgun.org" }-- before { SiteSetting.stubs(mailgun_api_key: "") }-- it { expect(check).to have_a_problem }- end-- context "when using Mailgun without an API key" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.mailgun.org" }-- before { SiteSetting.stubs(mailgun_api_key: "") }-- it do- expect(check).to have_a_problem.with_priority("low").with_message(- I18n.t(- "dashboard.problem.email_bounce_handling.webhook_key_missing",- provider: "Mailgun",- setting: "mailgun_api_key",- base_path: Discourse.base_path,- ),- )- end- end-- context "when using Mailgun with an API key" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.mailgun.org" }-- before { SiteSetting.stubs(mailgun_api_key: "key-123") }-- it { expect(check).to be_chill_about_it }- end-- context "when using SendGrid without a verification key" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.sendgrid.net" }-- before { SiteSetting.stubs(sendgrid_verification_key: "") }-- it do- expect(check).to have_a_problem.with_priority("low").with_message(- I18n.t(- "dashboard.problem.email_bounce_handling.webhook_key_missing",- provider: "SendGrid",- setting: "sendgrid_verification_key",- base_path: Discourse.base_path,- ),- )- end- end-- context "when using SendGrid with a verification key" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.sendgrid.net" }-- before { SiteSetting.stubs(sendgrid_verification_key: "key-123") }-- it { expect(check).to be_chill_about_it }- end-- context "when using Mailjet without a webhook token" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "in-v3.mailjet.com" }-- before { SiteSetting.stubs(mailjet_webhook_token: "") }-- it { expect(check).to have_a_problem.with_priority("low") }- end-- context "when using Mailpace without a verification key" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.mailpace.com" }-- before { SiteSetting.stubs(mailpace_verification_key: "") }-- it { expect(check).to have_a_problem.with_priority("low") }- end-- context "when using Mailpace with a verification key" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.mailpace.com" }-- before { SiteSetting.stubs(mailpace_verification_key: "key-123") }-- it { expect(check).to be_chill_about_it }- end-- context "when using AWS SES" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "email-smtp.us-east-1.amazonaws.com" }-- it { expect(check).to be_chill_about_it }- end-- context "when using an unknown provider" do- let(:reply_by_email_enabled) { false }- let(:bounceable_reply_address) { false }- let(:smtp_address) { "smtp.unknown-provider.com" }-- it do- expect(check).to have_a_problem.with_priority("low").with_message(- I18n.t(- "dashboard.problem.email_bounce_handling.no_bounce_handling",- base_path: Discourse.base_path,- ),- )- end- end-enddiff --git a/spec/services/problem_check/missing_mailgun_api_key_spec.rb b/spec/services/problem_check/missing_mailgun_api_key_spec.rbnew file mode 100644index 00000000000..e52625fc323--- /dev/null+++ b/spec/services/problem_check/missing_mailgun_api_key_spec.rb@@ -0,0 +1,41 @@+# frozen_string_literal: true++RSpec.describe ProblemCheck::MissingMailgunApiKey do+ subject(:check) { described_class.new }++ describe ".call" do+ before do+ SiteSetting.stubs(reply_by_email_enabled: replies_enabled)+ ActionMailer::Base.stubs(smtp_settings: { address: mailgun_address })+ SiteSetting.stubs(mailgun_api_key: api_key)+ end++ context "when replies are disabled" do+ let(:replies_enabled) { false }+ let(:mailgun_address) { anything }+ let(:api_key) { anything }++ it { expect(check).to be_chill_about_it }+ end++ context "when not using Mailgun for replies" do+ let(:replies_enabled) { false }+ let(:mailgun_address) { nil }+ let(:api_key) { anything }++ it { expect(check).to be_chill_about_it }+ end++ context "when using Mailgun without an API key" do+ let(:replies_enabled) { true }+ let(:mailgun_address) { "smtp.mailgun.org" }+ let(:api_key) { nil }++ it do+ expect(check).to have_a_problem.with_priority("low").with_message(+ "The server is configured to send emails via Mailgun but you haven't provided an API key used to verify the webhook messages.",+ )+ end+ end+ end+end