push_message_spec.rb 5.3 KB
Newer Older
1
require 'spec_helper'
2

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

6
  let(:args) do
7 8 9 10 11
    {
      after: 'after',
      before: 'before',
      project_name: 'project_name',
      ref: 'refs/heads/master',
12
      user_name: 'test.user',
T
Tiago Botelho 已提交
13
      user_avatar: 'http://someavatar.com',
14
      project_url: 'http://url.com'
15
    }
16
  end
17

18 19
  let(:color) { '#345' }

20 21 22
  context 'push' do
    before do
      args[:commits] = [
23
        { message: 'message1', url: 'http://url1.com', id: 'abcdefghijkl', author: { name: 'author1' } },
24
        { message: 'message2', url: 'http://url2.com', id: '123456789012', author: { name: 'author2' } }
25 26 27
      ]
    end

T
Tiago Botelho 已提交
28 29 30
    context 'without markdown' do
      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq(
31
          'test.user pushed to branch <http://url.com/commits/master|master> of '\
T
Tiago Botelho 已提交
32 33 34 35
            '<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 - author2",
36
          color: color
T
Tiago Botelho 已提交
37 38 39 40 41 42 43 44 45 46 47
        }])
      end
    end

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

      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq(
48
          '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))')
T
Tiago Botelho 已提交
49 50 51 52 53 54 55 56 57
        expect(subject.attachments).to eq(
          "[abcdefgh](http://url1.com): message1 - author1\n\n[12345678](http://url2.com): message2 - author2")
        expect(subject.activity).to eq({
          title: 'test.user pushed to branch',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/before...after)',
          image: 'http://someavatar.com'
        })
      end
58 59 60
    end
  end

61
  context 'tag push' do
62
    let(:args) do
63
      {
64 65 66 67
        after: 'after',
        before: Gitlab::Git::BLANK_SHA,
        project_name: 'project_name',
        ref: 'refs/tags/new_tag',
68
        user_name: 'test.user',
T
Tiago Botelho 已提交
69
        user_avatar: 'http://someavatar.com',
70
        project_url: 'http://url.com'
71
      }
72
    end
73

T
Tiago Botelho 已提交
74 75 76
    context 'without markdown' do
      it 'returns a message regarding pushes' do
        expect(subject.pretext).to eq('test.user pushed new tag ' \
77
          '<http://url.com/commits/new_tag|new_tag> to ' \
T
Tiago Botelho 已提交
78 79 80 81 82 83 84 85 86 87 88 89
          '<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(
90
          'test.user pushed new tag [new_tag](http://url.com/commits/new_tag) to [project_name](http://url.com)')
T
Tiago Botelho 已提交
91 92 93 94 95 96 97 98
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq({
          title: 'test.user created tag',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/0000000000000000000000000000000000000000...after)',
          image: 'http://someavatar.com'
        })
      end
99 100 101
    end
  end

102 103
  context 'new branch' do
    before do
104
      args[:before] = Gitlab::Git::BLANK_SHA
105 106
    end

T
Tiago Botelho 已提交
107 108 109
    context 'without markdown' do
      it 'returns a message regarding a new branch' do
        expect(subject.pretext).to eq(
110
          'test.user pushed new branch <http://url.com/commits/master|master> to '\
T
Tiago Botelho 已提交
111 112 113 114 115 116 117 118 119 120 121 122
            '<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(
123
          'test.user pushed new branch [master](http://url.com/commits/master) to [project_name](http://url.com)')
T
Tiago Botelho 已提交
124 125 126 127 128 129 130 131
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq({
          title: 'test.user created branch',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/0000000000000000000000000000000000000000...after)',
          image: 'http://someavatar.com'
        })
      end
132 133 134 135 136
    end
  end

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

T
Tiago Botelho 已提交
140 141 142
    context 'without markdown' do
      it 'returns a message regarding a removed branch' do
        expect(subject.pretext).to eq(
143
          'test.user removed branch master from <http://url.com|project_name>')
T
Tiago Botelho 已提交
144 145 146 147 148 149 150 151 152 153 154
        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(
155
          'test.user removed branch master from [project_name](http://url.com)')
T
Tiago Botelho 已提交
156 157 158 159 160 161 162 163
        expect(subject.attachments).to be_empty
        expect(subject.activity).to eq({
          title: 'test.user removed branch',
          subtitle: 'in [project_name](http://url.com)',
          text: '[Compare changes](http://url.com/compare/before...0000000000000000000000000000000000000000)',
          image: 'http://someavatar.com'
        })
      end
164 165 166
    end
  end
end