summaryrefslogtreecommitdiff
path: root/spec/models/integrations/chat_message/push_message_spec.rb
blob: 8d2d0f9f9a80b52239317761c35ac45bb415dd71 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::ChatMessage::PushMessage do
  subject { described_class.new(args) }

  let(:args) do
    {
      after: 'after',
      before: 'before',
      project_name: 'project_name',
      ref: 'refs/heads/master',
      user_name: 'test.user',
      user_avatar: 'http://someavatar.com',
      project_url: 'http://url.com'
    }
  end

  let(:color) { '#345' }

  it_behaves_like Integrations::ChatMessage

  context 'push' do
    before do
      args[:commits] = [
        { message: 'message1', title: 'message1', url: 'http://url1.com', id: 'abcdefghijkl', author: { name: 'author1' } },
        {
          message: 'message2' + ' w' * 100 + "\nsecondline",
          title: 'message2 w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w ...',
          url: 'http://url2.com',
          id: '123456789012',
          author: { name: 'author2' }
        }
      ]
    end

    context 'without markdown' do
      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq(
          'test.user pushed to branch <http://url.com/commits/master|master> of '\
            '<http://url.com|project_name> (<http://url.com/compare/before...after|Compare changes>)')
        expect(subject.attachments).to eq([{
          text: "<http://url1.com|abcdefgh>: message1 - author1\n\n"\
            "<http://url2.com|12345678>: message2 w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w ... - author2",
          color: color
        }])
      end
    end

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

      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq(
          'test.user pushed to branch [master](http://url.com/commits/master) of [project_name](http://url.com) ([Compare changes](http://url.com/compare/before...after))')
        expect(subject.attachments).to eq(
          "[abcdefgh](http://url1.com): message1 - author1\n\n[12345678](http://url2.com): message2 w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w ... - author2")
        expect(subject.activity).to eq(
          title: 'test.user pushed to branch [master](http://url.com/commits/master)',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/before...after)',
          image: 'http://someavatar.com'
        )
      end
    end
  end

  context 'tag push' do
    let(:args) do
      {
        after: 'after',
        before: Gitlab::Git::BLANK_SHA,
        project_name: 'project_name',
        ref: 'refs/tags/new_tag',
        user_name: 'test.user',
        user_avatar: 'http://someavatar.com',
        project_url: 'http://url.com'
      }
    end

    context 'without markdown' do
      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq('test.user pushed new tag ' \
          '<http://url.com/-/tags/new_tag|new_tag> to ' \
          '<http://url.com|project_name>')
        expect(subject.attachments).to be_empty
      end
    end

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

      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq(
          'test.user pushed new tag [new_tag](http://url.com/-/tags/new_tag) to [project_name](http://url.com)')
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq(
          title: 'test.user pushed new tag [new_tag](http://url.com/-/tags/new_tag)',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/0000000000000000000000000000000000000000...after)',
          image: 'http://someavatar.com'
        )
      end
    end
  end

  context 'removed tag' do
    let(:args) do
      {
        after: Gitlab::Git::BLANK_SHA,
        before: 'before',
        project_name: 'project_name',
        ref: 'refs/tags/new_tag',
        user_name: 'test.user',
        user_avatar: 'http://someavatar.com',
        project_url: 'http://url.com'
      }
    end

    context 'without markdown' do
      it 'returns a message regarding removal of tags' do
        expect(subject.pretext).to eq('test.user removed tag ' \
          'new_tag from ' \
          '<http://url.com|project_name>')
        expect(subject.attachments).to be_empty
      end
    end

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

      it 'returns a message regarding removal of tags' do
        expect(subject.pretext).to eq(
          'test.user removed tag new_tag from [project_name](http://url.com)')
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq(
          title: 'test.user removed tag new_tag',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/before...0000000000000000000000000000000000000000)',
          image: 'http://someavatar.com'
        )
      end
    end
  end

  context 'new branch' do
    before do
      args[:before] = Gitlab::Git::BLANK_SHA
    end

    context 'without markdown' do
      it 'returns a message regarding a new branch' do
        expect(subject.pretext).to eq(
          'test.user pushed new branch <http://url.com/commits/master|master> to '\
            '<http://url.com|project_name>')
        expect(subject.attachments).to be_empty
      end
    end

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

      it 'returns a message regarding a new branch' do
        expect(subject.pretext).to eq(
          'test.user pushed new branch [master](http://url.com/commits/master) to [project_name](http://url.com)')
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq(
          title: 'test.user pushed new branch [master](http://url.com/commits/master)',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/0000000000000000000000000000000000000000...after)',
          image: 'http://someavatar.com'
        )
      end
    end
  end

  context 'removed branch' do
    before do
      args[:after] = Gitlab::Git::BLANK_SHA
    end

    context 'without markdown' do
      it 'returns a message regarding a removed branch' do
        expect(subject.pretext).to eq(
          'test.user removed branch master from <http://url.com|project_name>')
        expect(subject.attachments).to be_empty
      end
    end

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

      it 'returns a message regarding a removed branch' do
        expect(subject.pretext).to eq(
          'test.user removed branch master from [project_name](http://url.com)')
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq(
          title: 'test.user removed branch master',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/before...0000000000000000000000000000000000000000)',
          image: 'http://someavatar.com'
        )
      end
    end
  end
end