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

describe SlackMessage do
  subject { described_class.new(commit) }

  let(:project) { FactoryGirl.create :project }

  context "One build" do
    let(:commit) { FactoryGirl.create(:commit_with_one_job, project: project) }

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

    context 'when build succeeded' do
      let(:color) { 'good' }

      it 'returns a message with succeeded build' do
        build.update(status: "success")

        expect(subject.color).to eq color
        expect(subject.fallback).to include('Build')
        expect(subject.fallback).to include("\##{build.id}")
        expect(subject.fallback).to include('succeeded')
        expect(subject.attachments.first[:fields]).to be_empty
      end
    end

    context 'when build failed' do
      let(:color) { 'danger' }

      it 'returns a message with failed build' do
        build.update(status: "failed")

        expect(subject.color).to eq color
        expect(subject.fallback).to include('Build')
        expect(subject.fallback).to include("\##{build.id}")
        expect(subject.fallback).to include('failed')
        expect(subject.attachments.first[:fields]).to be_empty
      end
    end
  end

  context "Several builds" do
    let(:commit) { FactoryGirl.create(:commit_with_two_jobs, project: project) }

    context 'when all matrix builds succeeded' do
      let(:color) { 'good' }

      it 'returns a message with success' do
        commit.create_builds
        commit.builds.update_all(status: "success")
        commit.reload

        expect(subject.color).to eq color
        expect(subject.fallback).to include('Commit')
        expect(subject.fallback).to include("\##{commit.id}")
        expect(subject.fallback).to include('succeeded')
        expect(subject.attachments.first[:fields]).to be_empty
      end
    end

    context 'when one of matrix builds failed' do
      let(:color) { 'danger' }

      it 'returns a message with information about failed build' do
        commit.create_builds
        first_build = commit.builds.first
        second_build = commit.builds.last
        first_build.update(status: "success")
        second_build.update(status: "failed")
        
        expect(subject.color).to eq color
        expect(subject.fallback).to include('Commit')
        expect(subject.fallback).to include("\##{commit.id}")
        expect(subject.fallback).to include('failed')
        expect(subject.attachments.first[:fields].size).to eq 1
        expect(subject.attachments.first[:fields].first[:title]).to eq second_build.name
        expect(subject.attachments.first[:fields].first[:value]).to include("\##{second_build.id}")
      end
    end
  end
end