summaryrefslogtreecommitdiff
path: root/spec/models/project_services/chat_message/note_message_spec.rb
blob: da700a08e574806c1be85ad48d04537be857c392 (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
require 'spec_helper'

describe ChatMessage::NoteMessage, models: true do
  let(:color) { '#345' }

  before do
    @args = {
        user: {
            name: 'Test User',
            username: 'test.user',
            avatar_url: 'http://fakeavatar'
        },
        project_name: 'project_name',
        project_url: 'http://somewhere.com',
        repository: {
            name: 'project_name',
            url: 'http://somewhere.com',
        },
        object_attributes: {
            id: 10,
            note: 'comment on a commit',
            url: 'http://url.com',
            noteable_type: 'Commit'
        }
    }
  end

  context 'commit notes' do
    before do
      @args[:object_attributes][:note] = 'comment on a commit'
      @args[:object_attributes][:noteable_type] = 'Commit'
      @args[:commit] = {
          id: '5f163b2b95e6f53cbd428f5f0b103702a52b9a23',
          message: "Added a commit message\ndetails\n123\n"
      }
    end

    it 'returns a message regarding notes on commits' do
      message = described_class.new(@args)
      expect(message.pretext).to eq("test.user <http://url.com|commented on " \
      "commit 5f163b2b> in <http://somewhere.com|project_name>: " \
      "*Added a commit message*")
      expected_attachments = [
          {
              text: "comment on a commit",
              color: color,
          }
      ]
      expect(message.attachments).to eq(expected_attachments)
    end
  end

  context 'merge request notes' do
    before do
      @args[:object_attributes][:note] = 'comment on a merge request'
      @args[:object_attributes][:noteable_type] = 'MergeRequest'
      @args[:merge_request] = {
          id: 1,
          iid: 30,
          title: "merge request title\ndetails\n"
      }
    end

    it 'returns a message regarding notes on a merge request' do
      message = described_class.new(@args)
      expect(message.pretext).to eq("test.user <http://url.com|commented on " \
      "merge request !30> in <http://somewhere.com|project_name>: " \
      "*merge request title*")
      expected_attachments = [
          {
              text: "comment on a merge request",
              color: color,
          }
      ]
      expect(message.attachments).to eq(expected_attachments)
    end
  end

  context 'issue notes' do
    before do
      @args[:object_attributes][:note] = 'comment on an issue'
      @args[:object_attributes][:noteable_type] = 'Issue'
      @args[:issue] = {
          id: 1,
          iid: 20,
          title: "issue title\ndetails\n"
      }
    end

    it 'returns a message regarding notes on an issue' do
      message = described_class.new(@args)
      expect(message.pretext).to eq(
        "test.user <http://url.com|commented on " \
        "issue #20> in <http://somewhere.com|project_name>: " \
        "*issue title*")
      expected_attachments = [
          {
              text: "comment on an issue",
              color: color,
          }
      ]
      expect(message.attachments).to eq(expected_attachments)
    end
  end

  context 'project snippet notes' do
    before do
      @args[:object_attributes][:note] = 'comment on a snippet'
      @args[:object_attributes][:noteable_type] = 'Snippet'
      @args[:snippet] = {
          id: 5,
          title: "snippet title\ndetails\n"
      }
    end

    it 'returns a message regarding notes on a project snippet' do
      message = described_class.new(@args)
      expect(message.pretext).to eq("test.user <http://url.com|commented on " \
      "snippet #5> in <http://somewhere.com|project_name>: " \
      "*snippet title*")
      expected_attachments = [
          {
              text: "comment on a snippet",
              color: color,
          }
      ]
      expect(message.attachments).to eq(expected_attachments)
    end
  end
end