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