summaryrefslogtreecommitdiff
path: root/spec/workers/irker_worker_spec.rb
blob: aa1f1d2fe1d7a2d2838d32c2e358ddf97c1a5c7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IrkerWorker, '#perform' do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }
  let_it_be(:push_data) { HashWithIndifferentAccess.new(Gitlab::DataBuilder::Push.build_sample(project, user)) }
  let_it_be(:channels) { ['irc://test.net/#test'] }

  let_it_be(:server_settings) do
    {
      server_host: 'localhost',
      server_port: 6659
    }
  end

  let_it_be(:arguments) do
    [
      project.id,
      channels,
      false,
      push_data,
      server_settings
    ]
  end

  let(:tcp_socket) { double('socket') }

  subject(:worker) { described_class.new }

  before do
    allow(TCPSocket).to receive(:new).and_return(tcp_socket)
    allow(tcp_socket).to receive(:puts).and_return(true)
    allow(tcp_socket).to receive(:close).and_return(true)
  end

  context 'connection fails' do
    before do
      allow(TCPSocket).to receive(:new).and_raise(Errno::ECONNREFUSED.new('test'))
    end

    it { expect(subject.perform(*arguments)).to be_falsey }
  end

  context 'connection successful' do
    it { expect(subject.perform(*arguments)).to be_truthy }

    context 'new branch' do
      it 'sends a correct message with branches url' do
        branches_url = Gitlab::Routing.url_helpers
          .project_branches_url(project)

        push_data['before'] = '0000000000000000000000000000000000000000'

        message = "has created a new branch master: #{branches_url}"

        expect(tcp_socket).to receive(:puts).with(wrap_message(message))

        subject.perform(*arguments)
      end
    end

    context 'deleted branch' do
      it 'sends a correct message' do
        push_data['after'] = '0000000000000000000000000000000000000000'

        message = "has deleted the branch master"

        expect(tcp_socket).to receive(:puts).with(wrap_message(message))

        subject.perform(*arguments)
      end
    end

    context 'new commits to existing branch' do
      it 'sends a correct message with a compare url' do
        compare_url = Gitlab::Routing.url_helpers
          .project_compare_url(project,
                               from: Commit.truncate_sha(push_data[:before]),
                               to: Commit.truncate_sha(push_data[:after]))

        message = "pushed #{push_data['total_commits_count']} " \
          "new commits to master: #{compare_url}"

        expect(tcp_socket).to receive(:puts).with(wrap_message(message))

        subject.perform(*arguments)
      end
    end
  end

  def wrap_message(text)
    message = "[#{project.path}] #{push_data['user_name']} #{text}"
    to_send = { to: channels, privmsg: message }

    Gitlab::Json.dump(to_send)
  end
end