summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/chat/responder/mattermost_spec.rb
blob: ca5f83d760ae06fdcc049303b8d98e1b2472dfdb (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Chat::Responder::Mattermost do
  let(:chat_name) { create(:chat_name, chat_id: 'U123') }

  let(:pipeline) do
    pipeline = create(:ci_pipeline)

    pipeline.create_chat_data!(
      response_url: 'http://example.com',
      chat_name_id: chat_name.id
    )

    pipeline
  end

  let(:build) { create(:ci_build, pipeline: pipeline) }
  let(:responder) { described_class.new(build) }

  describe '#send_response' do
    it 'sends a response back to Slack' do
      expect(Gitlab::HTTP).to receive(:post).with(
        'http://example.com',
        { headers: { 'Content-Type': 'application/json' }, body: 'hello'.to_json }
      )

      responder.send_response('hello')
    end
  end

  describe '#success' do
    it 'returns the output for a successful build' do
      expect(responder)
        .to receive(:send_response)
        .with(
          hash_including(
            response_type: :in_channel,
            attachments: array_including(
              a_hash_including(
                text: /#{pipeline.chat_data.chat_name.user.name}.*completed successfully/,
                fields: array_including(
                  a_hash_including(value: /##{build.id}/),
                  a_hash_including(value: build.name),
                  a_hash_including(value: "```shell\nscript output\n```")
                )
              )
            )
          )
        )

      responder.success('script output')
    end

    it 'limits the output to a fixed size' do
      expect(responder)
        .to receive(:send_response)
        .with(
          hash_including(
            response_type: :in_channel,
            attachments: array_including(
              a_hash_including(
                fields: array_including(
                  a_hash_including(value: /The output is too large/)
                )
              )
            )
          )
        )

      responder.success('a' * 4000)
    end

    it 'does not send a response if the output is empty' do
      expect(responder).not_to receive(:send_response)

      responder.success('')
    end
  end

  describe '#failure' do
    it 'returns the output for a failed build' do
      expect(responder)
        .to receive(:send_response)
        .with(
          hash_including(
            response_type: :in_channel,
            attachments: array_including(
              a_hash_including(
                text: /#{pipeline.chat_data.chat_name.user.name}.*failed/,
                fields: array_including(
                  a_hash_including(value: /##{build.id}/),
                  a_hash_including(value: build.name)
                )
              )
            )
          )
        )

      responder.failure
    end
  end

  describe '#scheduled_output' do
    it 'returns the output for a scheduled build' do
      output = responder.scheduled_output

      expect(output).to match(
        hash_including(
          response_type: :ephemeral,
          text: /##{build.id}/
        )
      )
    end
  end
end