2018-11-04 21:19:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-04 20:10:27 -05:00
|
|
|
require "spec_helper"
|
|
|
|
|
2021-05-31 23:10:06 -04:00
|
|
|
RSpec.describe Integrations::Discord do
|
|
|
|
it_behaves_like "chat integration", "Discord notifications" do
|
2018-11-06 17:12:02 -05:00
|
|
|
let(:client) { Discordrb::Webhooks::Client }
|
|
|
|
let(:client_arguments) { { url: webhook_url } }
|
2021-02-23 07:10:56 -05:00
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
embeds: [
|
|
|
|
include(
|
|
|
|
author: include(name: be_present),
|
2021-07-11 20:08:32 -04:00
|
|
|
description: be_present,
|
|
|
|
color: be_present,
|
|
|
|
timestamp: be_present
|
2021-02-23 07:10:56 -05:00
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
2018-11-04 20:10:27 -05:00
|
|
|
end
|
2019-07-18 22:12:02 -04:00
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
include StubRequests
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:webhook_url) { "https://example.gitlab.com/" }
|
|
|
|
|
|
|
|
let(:sample_data) do
|
|
|
|
Gitlab::DataBuilder::Push.build_sample(project, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(subject).to receive_messages(
|
|
|
|
project: project,
|
|
|
|
project_id: project.id,
|
|
|
|
webhook: webhook_url
|
|
|
|
)
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, webhook_url)
|
|
|
|
end
|
|
|
|
|
2020-03-24 11:08:44 -04:00
|
|
|
it 'uses the right embed parameters' do
|
|
|
|
builder = Discordrb::Webhooks::Builder.new
|
|
|
|
|
|
|
|
allow_next_instance_of(Discordrb::Webhooks::Client) do |client|
|
|
|
|
allow(client).to receive(:execute).and_yield(builder)
|
|
|
|
end
|
|
|
|
|
2021-07-11 20:08:32 -04:00
|
|
|
freeze_time do
|
|
|
|
subject.execute(sample_data)
|
2020-03-24 11:08:44 -04:00
|
|
|
|
2021-07-11 20:08:32 -04:00
|
|
|
expect(builder.to_json_hash[:embeds].first).to include(
|
|
|
|
description: start_with("#{user.name} pushed to branch [master](http://localhost/#{project.namespace.path}/#{project.path}/commits/master) of"),
|
|
|
|
author: hash_including(
|
|
|
|
icon_url: start_with('https://www.gravatar.com/avatar/'),
|
|
|
|
name: user.name
|
|
|
|
),
|
|
|
|
color: 16543014,
|
|
|
|
timestamp: Time.now.utc.iso8601
|
2020-03-24 11:08:44 -04:00
|
|
|
)
|
2021-07-11 20:08:32 -04:00
|
|
|
end
|
2020-03-24 11:08:44 -04:00
|
|
|
end
|
|
|
|
|
2019-07-18 22:12:02 -04:00
|
|
|
context 'DNS rebind to local address' do
|
|
|
|
before do
|
|
|
|
stub_dns(webhook_url, ip_address: '192.168.2.120')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow DNS rebinding' do
|
|
|
|
expect { subject.execute(sample_data) }.to raise_error(ArgumentError, /is blocked/)
|
|
|
|
end
|
|
|
|
end
|
2021-03-10 10:09:11 -05:00
|
|
|
|
|
|
|
context 'when the Discord request fails' do
|
|
|
|
before do
|
|
|
|
WebMock.stub_request(:post, webhook_url).to_return(status: 400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an error and returns false' do
|
|
|
|
expect(subject).to receive(:log_error).with('400 Bad Request')
|
|
|
|
expect(subject.execute(sample_data)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
2019-07-18 22:12:02 -04:00
|
|
|
end
|
2018-11-04 20:10:27 -05:00
|
|
|
end
|