summaryrefslogtreecommitdiff
path: root/spec/models/project_services/hipchat_service_spec.rb
blob: 82a4cde752b6688cad43f6d7f0025f0756800a02 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe HipchatService do
  describe "Associations" do
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
  end

  describe 'Validations' do
    context 'when service is active' do
      before do
        subject.active = true
      end

      it { is_expected.to validate_presence_of(:token) }
    end

    context 'when service is inactive' do
      before do
        subject.active = false
      end

      it { is_expected.not_to validate_presence_of(:token) }
    end
  end

  describe "Execute" do
    let(:hipchat) { described_class.new }
    let(:user)    { create(:user) }
    let(:project) { create(:project, :repository) }
    let(:api_url) { 'https://hipchat.example.com/v2/room/123456/notification?auth_token=verySecret' }
    let(:project_name) { project.full_name.gsub(/\s/, '') }
    let(:token) { 'verySecret' }
    let(:server_url) { 'https://hipchat.example.com'}
    let(:push_sample_data) do
      Gitlab::DataBuilder::Push.build_sample(project, user)
    end

    before do
      allow(hipchat).to receive_messages(
        project_id: project.id,
        project: project,
        room: 123456,
        server: server_url,
        token: token
      )
      WebMock.stub_request(:post, api_url)
    end

    it 'does nothing' do
      expect { hipchat.execute(push_sample_data) }.not_to raise_error
    end

    describe "#message_options" do
      it "is set to the defaults" do
        expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'yellow' })
      end

      it "sets notify to true" do
        allow(hipchat).to receive(:notify).and_return('1')

        expect(hipchat.__send__(:message_options)).to eq({ notify: true, color: 'yellow' })
      end

      it "sets the color" do
        allow(hipchat).to receive(:color).and_return('red')

        expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'red' })
      end

      context 'with a successful build' do
        it 'uses the green color' do
          data = { object_kind: 'pipeline',
                   object_attributes: { status: 'success' } }

          expect(hipchat.__send__(:message_options, data)).to eq({ notify: false, color: 'green' })
        end
      end

      context 'with a failed build' do
        it 'uses the red color' do
          data = { object_kind: 'pipeline',
                   object_attributes: { status: 'failed' } }

          expect(hipchat.__send__(:message_options, data)).to eq({ notify: false, color: 'red' })
        end
      end
    end
  end
end