2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-04 20:31:55 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'socket'
|
|
|
|
require 'json'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe IrkerService do
|
2015-02-04 20:31:55 +00:00
|
|
|
describe 'Associations' do
|
2015-05-21 21:49:06 +00:00
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
it { is_expected.to have_one :service_hook }
|
2015-02-04 20:31:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Validations' do
|
2016-04-21 15:13:14 +00:00
|
|
|
context 'when service is active' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
2016-04-21 15:13:14 +00:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:recipients) }
|
2015-02-04 20:31:55 +00:00
|
|
|
end
|
|
|
|
|
2016-04-21 15:13:14 +00:00
|
|
|
context 'when service is inactive' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
subject.active = false
|
|
|
|
end
|
2016-04-21 15:13:14 +00:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:recipients) }
|
2015-02-04 20:31:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Execute' do
|
2017-07-25 17:09:00 +00:00
|
|
|
let(:irker) { described_class.new }
|
2015-02-04 20:31:55 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-01-26 22:44:58 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-08-04 15:44:27 +00:00
|
|
|
let(:sample_data) do
|
2016-08-12 08:09:29 +00:00
|
|
|
Gitlab::DataBuilder::Push.build_sample(project, user)
|
2016-08-04 15:44:27 +00:00
|
|
|
end
|
2015-02-04 20:31:55 +00:00
|
|
|
|
2015-07-03 06:17:10 +00:00
|
|
|
let(:recipients) { '#commits irc://test.net/#test ftp://bad' }
|
2015-02-04 20:31:55 +00:00
|
|
|
let(:colorize_messages) { '1' }
|
|
|
|
|
|
|
|
before do
|
2016-08-15 21:26:40 +00:00
|
|
|
@irker_server = TCPServer.new 'localhost', 0
|
|
|
|
|
2015-05-21 21:49:06 +00:00
|
|
|
allow(irker).to receive_messages(
|
2016-08-15 21:33:36 +00:00
|
|
|
active: true,
|
|
|
|
project: project,
|
|
|
|
project_id: project.id,
|
|
|
|
service_hook: true,
|
|
|
|
server_host: @irker_server.addr[2],
|
|
|
|
server_port: @irker_server.addr[1],
|
|
|
|
default_irc_uri: 'irc://chat.freenode.net/',
|
|
|
|
recipients: recipients,
|
|
|
|
colorize_messages: colorize_messages)
|
2015-07-03 06:17:10 +00:00
|
|
|
|
2015-02-04 20:31:55 +00:00
|
|
|
irker.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@irker_server.close
|
|
|
|
end
|
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
it 'sends valid JSON messages to an Irker listener', :sidekiq_might_not_need_inline do
|
2015-02-04 20:31:55 +00:00
|
|
|
irker.execute(sample_data)
|
|
|
|
|
|
|
|
conn = @irker_server.accept
|
2020-03-27 18:07:48 +00:00
|
|
|
conn.each_line do |line|
|
2020-04-30 18:09:38 +00:00
|
|
|
msg = Gitlab::Json.parse(line.chomp("\n"))
|
2017-02-22 17:46:57 +00:00
|
|
|
expect(msg.keys).to match_array(%w(to privmsg))
|
2015-07-03 06:17:10 +00:00
|
|
|
expect(msg['to']).to match_array(["irc://chat.freenode.net/#commits",
|
|
|
|
"irc://test.net/#test"])
|
2015-02-04 20:31:55 +00:00
|
|
|
end
|
|
|
|
conn.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|