2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-30 23:11:17 +08:00
|
|
|
describe WebhooksController do
|
2020-05-23 12:56:13 +08:00
|
|
|
before { Discourse.redis.flushdb }
|
2016-05-30 23:11:17 +08:00
|
|
|
|
2016-06-02 03:48:06 +08:00
|
|
|
let(:email) { "em@il.com" }
|
2016-06-09 06:33:13 +08:00
|
|
|
let(:message_id) { "12345@il.com" }
|
2016-06-02 03:48:06 +08:00
|
|
|
|
|
|
|
context "mailgun" do
|
2019-02-01 00:52:33 +08:00
|
|
|
|
|
|
|
let(:token) { "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de" }
|
|
|
|
let(:timestamp) { Time.now.to_i }
|
|
|
|
let(:data) { "#{timestamp}#{token}" }
|
|
|
|
let(:signature) { OpenSSL::HMAC.hexdigest("SHA256", SiteSetting.mailgun_api_key, data) }
|
|
|
|
|
|
|
|
before do
|
2016-06-07 07:55:24 +08:00
|
|
|
SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493"
|
2019-02-01 00:52:33 +08:00
|
|
|
end
|
2016-05-30 23:11:17 +08:00
|
|
|
|
2019-02-01 00:52:33 +08:00
|
|
|
it "works (deprecated)" do
|
2016-06-02 03:48:06 +08:00
|
|
|
user = Fabricate(:user, email: email)
|
2017-02-06 02:06:35 +08:00
|
|
|
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
|
2016-05-30 23:11:17 +08:00
|
|
|
|
2018-06-05 13:17:33 +08:00
|
|
|
post "/webhooks/mailgun.json", params: {
|
|
|
|
"token" => token,
|
|
|
|
"timestamp" => timestamp,
|
2017-08-31 12:06:56 +08:00
|
|
|
"event" => "dropped",
|
|
|
|
"recipient" => email,
|
2019-02-01 00:52:33 +08:00
|
|
|
"Message-Id" => "<#{message_id}>",
|
2022-02-15 12:17:26 +08:00
|
|
|
"signature" => signature,
|
|
|
|
"error" => "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
|
|
|
|
"code" => "5.1.1"
|
2018-06-05 13:17:33 +08:00
|
|
|
}
|
2016-05-30 23:11:17 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-05-30 23:11:17 +08:00
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq("5.1.1")
|
2019-02-01 00:52:33 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
|
|
|
|
end
|
|
|
|
|
|
|
|
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" => {
|
|
|
|
"token" => token,
|
|
|
|
"timestamp" => timestamp,
|
|
|
|
"signature" => signature,
|
|
|
|
},
|
|
|
|
"event-data" => {
|
|
|
|
"event" => "failed",
|
|
|
|
"severity" => "temporary",
|
|
|
|
"recipient" => email,
|
|
|
|
"message" => {
|
|
|
|
"headers" => {
|
|
|
|
"message-id" => message_id,
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:17:26 +08:00
|
|
|
},
|
|
|
|
"delivery-status" => {
|
|
|
|
"message" => "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
|
|
|
|
"code" => "5.1.1",
|
|
|
|
"description" => ""
|
2019-02-01 00:52:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq("5.1.1")
|
2019-02-01 00:52:33 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
|
2016-05-30 23:11:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 03:48:06 +08:00
|
|
|
context "sendgrid" do
|
|
|
|
it "works" do
|
|
|
|
user = Fabricate(:user, email: email)
|
2017-02-06 02:06:35 +08:00
|
|
|
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
|
2016-06-02 03:48:06 +08:00
|
|
|
|
2018-06-05 13:17:33 +08:00
|
|
|
post "/webhooks/sendgrid.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
"_json" => [
|
|
|
|
{
|
2018-12-04 17:48:16 +08:00
|
|
|
"email" => email,
|
2017-08-31 12:06:56 +08:00
|
|
|
"smtp-id" => "<12345@il.com>",
|
2018-12-04 17:48:16 +08:00
|
|
|
"event" => "bounce",
|
|
|
|
"status" => "5.0.0"
|
2017-08-31 12:06:56 +08:00
|
|
|
}
|
|
|
|
]
|
2018-06-05 13:17:33 +08:00
|
|
|
}
|
2016-06-02 03:48:06 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-06-02 03:48:06 +08:00
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq("5.0.0")
|
2019-02-01 00:52:33 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
|
2016-06-02 03:48:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-07 01:47:45 +08:00
|
|
|
context "mailjet" do
|
|
|
|
it "works" do
|
|
|
|
user = Fabricate(:user, email: email)
|
2017-02-06 02:06:35 +08:00
|
|
|
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
|
2016-06-07 01:47:45 +08:00
|
|
|
|
2018-06-05 13:17:33 +08:00
|
|
|
post "/webhooks/mailjet.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
"event" => "bounce",
|
2018-12-04 17:48:16 +08:00
|
|
|
"email" => email,
|
2017-08-31 12:06:56 +08:00
|
|
|
"hard_bounce" => true,
|
2018-12-04 17:48:16 +08:00
|
|
|
"CustomID" => message_id
|
2018-06-05 13:17:33 +08:00
|
|
|
}
|
2016-06-07 01:47:45 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-06-07 01:47:45 +08:00
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq(nil) # mailjet doesn't give us this
|
2019-02-01 00:52:33 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
|
2016-06-07 01:47:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-13 18:31:01 +08:00
|
|
|
context "mandrill" do
|
|
|
|
it "works" do
|
|
|
|
user = Fabricate(:user, email: email)
|
2017-02-06 02:06:35 +08:00
|
|
|
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
|
2016-06-13 18:31:01 +08:00
|
|
|
|
2018-06-05 13:17:33 +08:00
|
|
|
post "/webhooks/mandrill.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
mandrill_events: [{
|
|
|
|
"event" => "hard_bounce",
|
|
|
|
"msg" => {
|
|
|
|
"email" => email,
|
2022-02-15 12:17:26 +08:00
|
|
|
"diag" => "5.1.1",
|
|
|
|
"bounce_description": "smtp; 550-5.1.1 The email account that you tried to reach does not exist.",
|
2017-08-31 12:06:56 +08:00
|
|
|
"metadata" => {
|
|
|
|
"message_id" => message_id
|
|
|
|
}
|
2016-06-13 18:31:01 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
}]
|
2018-06-05 13:17:33 +08:00
|
|
|
}
|
2016-06-13 18:31:01 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-06-13 18:31:01 +08:00
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq("5.1.1")
|
2019-02-01 00:52:33 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
|
2016-06-13 18:31:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-11 23:09:07 +08:00
|
|
|
context "postmark" do
|
|
|
|
it "works" 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: {
|
2020-02-12 00:21:03 +08:00
|
|
|
"Type" => "HardBounce",
|
|
|
|
"MessageID" => message_id,
|
|
|
|
"Email" => email
|
|
|
|
}
|
2020-02-11 23:09:07 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq(nil) # postmark doesn't give us this
|
2020-02-11 23:09:07 +08:00
|
|
|
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: {
|
2020-02-12 00:21:03 +08:00
|
|
|
"Type" => "SoftBounce",
|
|
|
|
"MessageID" => message_id,
|
|
|
|
"Email" => email
|
|
|
|
}
|
2020-02-11 23:09:07 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2022-02-15 12:17:26 +08:00
|
|
|
expect(email_log.bounce_error_code).to eq(nil) # postmark doesn't give us this
|
2020-02-11 23:09:07 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-27 13:13:34 +08:00
|
|
|
context "sparkpost" do
|
|
|
|
it "works" do
|
|
|
|
user = Fabricate(:user, email: email)
|
2017-02-06 02:06:35 +08:00
|
|
|
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
|
2016-09-27 13:13:34 +08:00
|
|
|
|
2018-06-05 13:17:33 +08:00
|
|
|
post "/webhooks/sparkpost.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
"_json" => [{
|
|
|
|
"msys" => {
|
|
|
|
"message_event" => {
|
|
|
|
"bounce_class" => 10,
|
2022-02-15 12:17:26 +08:00
|
|
|
"error_code" => "554",
|
2017-08-31 12:06:56 +08:00
|
|
|
"rcpt_to" => email,
|
|
|
|
"rcpt_meta" => {
|
|
|
|
"message_id" => message_id
|
|
|
|
}
|
2016-10-28 01:35:50 +08:00
|
|
|
}
|
2016-09-27 13:13:34 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
}]
|
2018-06-05 13:17:33 +08:00
|
|
|
}
|
2016-09-27 13:13:34 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-09-27 13:13:34 +08:00
|
|
|
|
|
|
|
email_log.reload
|
|
|
|
expect(email_log.bounced).to eq(true)
|
2019-02-01 00:52:33 +08:00
|
|
|
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.hard_bounce_score)
|
2016-09-27 13:13:34 +08:00
|
|
|
end
|
|
|
|
end
|
2016-05-30 23:11:17 +08:00
|
|
|
end
|