summaryrefslogtreecommitdiff
path: root/spec/models/project_services/chat_message/pipeline_message_spec.rb
blob: 0ff204009998ace30d947ea36862a30094566715 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'spec_helper'

describe ChatMessage::PipelineMessage do
  subject { described_class.new(args) }

  let(:user) { { name: "The Hacker", username: 'hacker' } }
  let(:duration) { 7210 }
  let(:args) do
    {
      object_attributes: {
        id: 123,
        sha: '97de212e80737a608d939f648d959671fb0a0142',
        tag: false,
        ref: 'develop',
        status: status,
        duration: duration
      },
      project: {
        path_with_namespace: 'project_name',
        web_url: 'http://example.gitlab.com'
      },
      user: user
    }
  end
  let(:combined_name) { "The Hacker (hacker)" }

  context 'without markdown' do
    context 'pipeline succeeded' do
      let(:status) { 'success' }
      let(:color) { 'good' }
      let(:message) { build_message('passed', combined_name) }

      it 'returns a message with information about succeeded build' do
        expect(subject.pretext).to be_empty
        expect(subject.fallback).to eq(message)
        expect(subject.attachments).to eq([text: message, color: color])
      end
    end

    context 'pipeline failed' do
      let(:status) { 'failed' }
      let(:color) { 'danger' }
      let(:message) { build_message(status, combined_name) }

      it 'returns a message with information about failed build' do
        expect(subject.pretext).to be_empty
        expect(subject.fallback).to eq(message)
        expect(subject.attachments).to eq([text: message, color: color])
      end

      context 'when triggered by API therefore lacking user' do
        let(:user) { nil }
        let(:message) { build_message(status, 'API') }

        it 'returns a message stating it is by API' do
          expect(subject.pretext).to be_empty
          expect(subject.fallback).to eq(message)
          expect(subject.attachments).to eq([text: message, color: color])
        end
      end
    end

    def build_message(status_text = status, name = user[:name])
      "<http://example.gitlab.com|project_name>:" \
        " Pipeline <http://example.gitlab.com/pipelines/123|#123>" \
        " of branch <http://example.gitlab.com/commits/develop|develop>" \
        " by #{name} #{status_text} in 02:00:10"
    end
  end

  context 'with markdown' do
    before do
      args[:markdown] = true
    end

    context 'pipeline succeeded' do
      let(:status) { 'success' }
      let(:color) { 'good' }
      let(:message) { build_markdown_message('passed', combined_name) }

      it 'returns a message with information about succeeded build' do
        expect(subject.pretext).to be_empty
        expect(subject.attachments).to eq(message)
        expect(subject.activity).to eq({
          title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of branch [develop](http://example.gitlab.com/commits/develop) by The Hacker (hacker) passed',
          subtitle: 'in [project_name](http://example.gitlab.com)',
          text: 'in 02:00:10',
          image: ''
        })
      end
    end

    context 'pipeline failed' do
      let(:status) { 'failed' }
      let(:color) { 'danger' }
      let(:message) { build_markdown_message(status, combined_name) }

      it 'returns a message with information about failed build' do
        expect(subject.pretext).to be_empty
        expect(subject.attachments).to eq(message)
        expect(subject.activity).to eq({
          title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of branch [develop](http://example.gitlab.com/commits/develop) by The Hacker (hacker) failed',
          subtitle: 'in [project_name](http://example.gitlab.com)',
          text: 'in 02:00:10',
          image: ''
        })
      end

      context 'when triggered by API therefore lacking user' do
        let(:user) { nil }
        let(:message) { build_markdown_message(status, 'API') }

        it 'returns a message stating it is by API' do
          expect(subject.pretext).to be_empty
          expect(subject.attachments).to eq(message)
          expect(subject.activity).to eq({
            title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of branch [develop](http://example.gitlab.com/commits/develop) by API failed',
            subtitle: 'in [project_name](http://example.gitlab.com)',
            text: 'in 02:00:10',
            image: ''
          })
        end
      end
    end

    def build_markdown_message(status_text = status, name = user[:name])
      "[project_name](http://example.gitlab.com):" \
        " Pipeline [#123](http://example.gitlab.com/pipelines/123)" \
        " of branch [develop](http://example.gitlab.com/commits/develop)" \
        " by #{name} #{status_text} in 02:00:10"
    end
  end
end