summaryrefslogtreecommitdiff
path: root/spec/models/ci/project_services/hip_chat_message_spec.rb
blob: 6d257638359e2dd28b227f164eec955ef5f84380 (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
require 'spec_helper'

describe Ci::HipChatMessage do
  subject { Ci::HipChatMessage.new(build) }

  context "One build" do
    let(:commit) { FactoryGirl.create(:ci_commit_with_one_job) }

    let(:build) do
      commit.create_builds('master', false, nil)
      commit.builds.first
    end

    context 'when build succeeds' do
      it 'returns a successful message' do
        build.update(status: "success")

        expect( subject.status_color ).to eq 'green'
        expect( subject.notify? ).to be_falsey
        expect( subject.to_s ).to match(/Build '[^']+' #\d+/)
        expect( subject.to_s ).to match(/Successful in \d+ second\(s\)\./)
      end
    end

    context 'when build fails' do
      it 'returns a failure message' do
        build.update(status: "failed")

        expect( subject.status_color ).to eq 'red'
        expect( subject.notify? ).to be_truthy
        expect( subject.to_s ).to match(/Build '[^']+' #\d+/)
        expect( subject.to_s ).to match(/Failed in \d+ second\(s\)\./)
      end
    end
  end

  context "Several builds" do
    let(:commit) { FactoryGirl.create(:ci_commit_with_two_jobs) }

    let(:build) do
      commit.builds.first
    end

    context 'when all matrix builds succeed' do
      it 'returns a successful message' do
        commit.create_builds('master', false, nil)
        commit.builds.update_all(status: "success")
        commit.reload

        expect( subject.status_color ).to eq 'green'
        expect( subject.notify? ).to be_falsey
        expect( subject.to_s ).to match(/Commit #\d+/)
        expect( subject.to_s ).to match(/Successful in \d+ second\(s\)\./)
      end
    end

    context 'when at least one matrix build fails' do
      it 'returns a failure message' do
        commit.create_builds('master', false, nil)
        first_build = commit.builds.first
        second_build = commit.builds.last
        first_build.update(status: "success")
        second_build.update(status: "failed")

        expect( subject.status_color ).to eq 'red'
        expect( subject.notify? ).to be_truthy
        expect( subject.to_s ).to match(/Commit #\d+/)
        expect( subject.to_s ).to match(/Failed in \d+ second\(s\)\./)
      end
    end
  end
end