pipeline_message_spec.rb 4.5 KB
Newer Older
1 2
require 'spec_helper'

F
Felipe Artur 已提交
3 4
describe ChatMessage::PipelineMessage do
  subject { described_class.new(args) }
5

T
Tiago Botelho 已提交
6
  let(:user) { { name: 'hacker' } }
7 8
  let(:args) do
    {
9 10 11 12 13 14 15 16
      object_attributes: {
        id: 123,
        sha: '97de212e80737a608d939f648d959671fb0a0142',
        tag: false,
        ref: 'develop',
        status: status,
        duration: duration
      },
T
Tiago Botelho 已提交
17 18 19 20
      project: {
        path_with_namespace: 'project_name',
        web_url: 'http://example.gitlab.com'
      },
L
Lin Jen-Shin 已提交
21
      user: user
22 23 24
    }
  end

T
Tiago Botelho 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
  context 'without markdown' do
    context 'pipeline succeeded' do
      let(:status) { 'success' }
      let(:color) { 'good' }
      let(:duration) { 10 }
      let(:message) { build_message('passed') }

      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
38

T
Tiago Botelho 已提交
39 40 41 42 43
    context 'pipeline failed' do
      let(:status) { 'failed' }
      let(:color) { 'danger' }
      let(:duration) { 10 }
      let(:message) { build_message }
44

T
Tiago Botelho 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
      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
61 62
    end

T
Tiago Botelho 已提交
63 64 65 66 67 68 69
    def build_message(status_text = status, name = user[:name])
      "<http://example.gitlab.com|project_name>:" \
        " Pipeline <http://example.gitlab.com/pipelines/123|#123>" \
        " of <http://example.gitlab.com/commits/develop|develop> branch" \
        " by #{name} #{status_text} in #{duration} #{'second'.pluralize(duration)}"
    end
  end
70

T
Tiago Botelho 已提交
71 72 73
  context 'with markdown' do
    before do
      args[:markdown] = true
74
    end
L
Lin Jen-Shin 已提交
75

T
Tiago Botelho 已提交
76 77 78 79 80
    context 'pipeline succeeded' do
      let(:status) { 'success' }
      let(:color) { 'good' }
      let(:duration) { 10 }
      let(:message) { build_markdown_message('passed') }
L
Lin Jen-Shin 已提交
81

T
Tiago Botelho 已提交
82 83 84 85 86 87 88 89 90
      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 [develop](http://example.gitlab.com/commits/develop) branch by hacker passed',
          subtitle: 'in [project_name](http://example.gitlab.com)',
          text: 'in 10 seconds',
          image: ''
        })
L
Lin Jen-Shin 已提交
91 92 93
      end
    end

T
Tiago Botelho 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    context 'pipeline failed' do
      let(:status) { 'failed' }
      let(:color) { 'danger' }
      let(:duration) { 10 }
      let(:message) { build_markdown_message }

      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 [develop](http://example.gitlab.com/commits/develop) branch by hacker failed',
          subtitle: 'in [project_name](http://example.gitlab.com)',
          text: 'in 10 seconds',
          image: ''
        })
      end
110

T
Tiago Botelho 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      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 [develop](http://example.gitlab.com/commits/develop) branch by API failed',
            subtitle: 'in [project_name](http://example.gitlab.com)',
            text: 'in 10 seconds',
            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 [develop](http://example.gitlab.com/commits/develop)" \
        " branch by #{name} #{status_text} in #{duration} #{'second'.pluralize(duration)}"
    end
134
  end
135
end