2021-06-17 20:54:30 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Activities::CreateActivityService do
|
|
|
|
let(:webhook) { create :webhook }
|
|
|
|
|
|
|
|
context 'when webhook is valid' do
|
|
|
|
it 'executes webhook' do
|
|
|
|
stub_request(:post, webhook.url).to_return(status: 200, body: "", headers: {})
|
|
|
|
|
|
|
|
expect(WebhookService.new(webhook, { payload: "payload" }).send_webhook.response.code).to eq("200")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when webhook gets non-success HTTP response' do
|
|
|
|
it 'logs error' do
|
|
|
|
stub_request(:post, webhook.url).to_return(status: 500, body: "", headers: {})
|
|
|
|
|
2021-07-22 19:43:36 +08:00
|
|
|
expect { WebhookService.new(webhook, { payload: "payload" }).send_webhook }.to(
|
|
|
|
raise_error(WebhookService::RequestFailureException)
|
|
|
|
)
|
|
|
|
|
2021-06-17 20:54:30 +08:00
|
|
|
expect(webhook.last_error).to eq("500: ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when webhook times out' do
|
|
|
|
it 'logs error' do
|
|
|
|
stub_request(:post, webhook.url).to_timeout
|
|
|
|
|
|
|
|
expect { WebhookService.new(webhook, { payload: "payload" }).send_webhook }.to raise_error(Net::OpenTimeout)
|
|
|
|
expect(webhook.last_error).to eq("execution expired")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when webhook url cannot be resolved' do
|
|
|
|
it 'logs error' do
|
|
|
|
stub_request(:post, webhook.url).to_raise(SocketError)
|
|
|
|
|
|
|
|
expect { WebhookService.new(webhook, { payload: "payload" }).send_webhook }.to raise_error(SocketError)
|
|
|
|
expect(webhook.last_error).to eq("Exception from WebMock")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|