REFACTOR: webhooks controller specs to requests

This commit is contained in:
OsamaSayegh 2018-06-05 08:17:33 +03:00 committed by Guo Xiang Tan
parent 4782f6cd01
commit 5ecaa55e50
1 changed files with 18 additions and 25 deletions

View File

@ -7,22 +7,25 @@ describe WebhooksController do
let(:message_id) { "12345@il.com" } let(:message_id) { "12345@il.com" }
context "mailgun" do context "mailgun" do
it "works" do it "works" do
SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493" SiteSetting.mailgun_api_key = "key-8221462f0c915af3f6f2e2df7aa5a493"
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email) email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
WebhooksController.any_instance.expects(:mailgun_verify).returns(true) token = "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de"
timestamp = Time.now.to_i
data = "#{timestamp}#{token}"
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, SiteSetting.mailgun_api_key, data)
post :mailgun, params: { post "/webhooks/mailgun.json", params: {
"token" => "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de", "token" => token,
"timestamp" => Time.now.to_i, "timestamp" => timestamp,
"event" => "dropped", "event" => "dropped",
"recipient" => email, "recipient" => email,
"Message-Id" => "<12345@il.com>" "Message-Id" => "<12345@il.com>",
}, format: :json "signature" => signature
}
expect(response).to be_successful expect(response).to be_successful
@ -30,16 +33,14 @@ describe WebhooksController do
expect(email_log.bounced).to eq(true) expect(email_log.bounced).to eq(true)
expect(email_log.user.user_stat.bounce_score).to eq(2) expect(email_log.user.user_stat.bounce_score).to eq(2)
end end
end end
context "sendgrid" do context "sendgrid" do
it "works" do it "works" do
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email) email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
post :sendgrid, params: { post "/webhooks/sendgrid.json", params: {
"_json" => [ "_json" => [
{ {
"email" => email, "email" => email,
@ -48,7 +49,7 @@ describe WebhooksController do
"status" => "5.0.0" "status" => "5.0.0"
} }
] ]
}, format: :json }
expect(response).to be_successful expect(response).to be_successful
@ -56,21 +57,19 @@ describe WebhooksController do
expect(email_log.bounced).to eq(true) expect(email_log.bounced).to eq(true)
expect(email_log.user.user_stat.bounce_score).to eq(2) expect(email_log.user.user_stat.bounce_score).to eq(2)
end end
end end
context "mailjet" do context "mailjet" do
it "works" do it "works" do
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email) email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
post :mailjet, params: { post "/webhooks/mailjet.json", params: {
"event" => "bounce", "event" => "bounce",
"email" => email, "email" => email,
"hard_bounce" => true, "hard_bounce" => true,
"CustomID" => message_id "CustomID" => message_id
}, format: :json }
expect(response).to be_successful expect(response).to be_successful
@ -78,16 +77,14 @@ describe WebhooksController do
expect(email_log.bounced).to eq(true) expect(email_log.bounced).to eq(true)
expect(email_log.user.user_stat.bounce_score).to eq(2) expect(email_log.user.user_stat.bounce_score).to eq(2)
end end
end end
context "mandrill" do context "mandrill" do
it "works" do it "works" do
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email) email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
post :mandrill, params: { post "/webhooks/mandrill.json", params: {
mandrill_events: [{ mandrill_events: [{
"event" => "hard_bounce", "event" => "hard_bounce",
"msg" => { "msg" => {
@ -97,7 +94,7 @@ describe WebhooksController do
} }
} }
}] }]
}, format: :json }
expect(response).to be_successful expect(response).to be_successful
@ -105,16 +102,14 @@ describe WebhooksController do
expect(email_log.bounced).to eq(true) expect(email_log.bounced).to eq(true)
expect(email_log.user.user_stat.bounce_score).to eq(2) expect(email_log.user.user_stat.bounce_score).to eq(2)
end end
end end
context "sparkpost" do context "sparkpost" do
it "works" do it "works" do
user = Fabricate(:user, email: email) user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email) email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
post :sparkpost, params: { post "/webhooks/sparkpost.json", params: {
"_json" => [{ "_json" => [{
"msys" => { "msys" => {
"message_event" => { "message_event" => {
@ -126,7 +121,7 @@ describe WebhooksController do
} }
} }
}] }]
}, format: :json }
expect(response).to be_successful expect(response).to be_successful
@ -134,7 +129,5 @@ describe WebhooksController do
expect(email_log.bounced).to eq(true) expect(email_log.bounced).to eq(true)
expect(email_log.user.user_stat.bounce_score).to eq(2) expect(email_log.user.user_stat.bounce_score).to eq(2)
end end
end end
end end