File: discord_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (151 lines) | stat: -rw-r--r-- 4,570 bytes parent folder | download
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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Integrations::Discord, feature_category: :integrations do
  it_behaves_like "chat integration", "Discord notifications", supports_deployments: true do
    let(:client) { Discordrb::Webhooks::Client }
    let(:client_arguments) { { url: webhook_url } }
    let(:payload) do
      {
        embeds: [
          include(
            author: include(name: be_present),
            description: be_present,
            color: be_present,
            timestamp: be_present
          )
        ]
      }
    end

    it_behaves_like 'supports group mentions', :discord_integration
  end

  describe 'validations' do
    let_it_be(:project) { create(:project) }

    subject(:discord_integration) { integration }

    describe 'only allows one channel on events' do
      context 'when given more than one channel' do
        let(:integration) { build(:discord_integration, project: project, note_channel: 'webhook1,webhook2') }

        it { is_expected.not_to be_valid }
      end

      context 'when given one channel' do
        let(:integration) { build(:discord_integration, project: project, note_channel: 'webhook1') }

        it { is_expected.to be_valid }
      end
    end
  end

  describe '#execute' do
    include StubRequests

    let_it_be(:project) { create(:project, :repository) }

    let(:user) { build_stubbed(:user) }
    let(:webhook_url) { "https://example.gitlab.com/" }
    let(:sample_data) do
      Gitlab::DataBuilder::Push.build_sample(project, user)
    end

    subject(:discord_integration) { described_class.new }

    before do
      allow(discord_integration).to receive_messages(
        project: project,
        project_id: project.id,
        webhook: webhook_url
      )

      WebMock.stub_request(:post, webhook_url)
    end

    it 'uses the right embed parameters' do
      builder = Discordrb::Webhooks::Builder.new

      allow_next_instance_of(Discordrb::Webhooks::Client) do |client|
        allow(client).to receive(:execute).and_yield(builder)
      end

      freeze_time do
        discord_integration.execute(sample_data)

        expect(builder.to_json_hash[:embeds].first).to include(
          description: start_with("#{user.name} pushed to branch [master](http://localhost/#{project.namespace.path}/#{project.path}/-/commits/master) of"),
          author: hash_including(
            icon_url: start_with('https://www.gravatar.com/avatar/'),
            name: user.name
          ),
          color: 3359829,
          timestamp: Time.now.utc.iso8601
        )
      end
    end

    context 'when description references attachments' do
      let(:builder) { Discordrb::Webhooks::Builder.new }
      let(:attachments) { ": foo\n - bar" }

      before do
        allow_next_instance_of(Discordrb::Webhooks::Client) do |client|
          allow(client).to receive(:execute).and_yield(builder)
        end

        allow_next_instance_of(Integrations::ChatMessage::PushMessage) do |message|
          allow(message).to receive(:pretext).and_return('pretext')
          allow(message).to receive(:attachments).and_return(attachments)
        end
      end

      it 'updates attachment format' do
        freeze_time do
          discord_integration.execute(sample_data)

          expect(builder.to_json_hash[:embeds].first)
            .to include(description: "pretext\n foo - bar\n")
        end
      end

      context 'when description is large' do
        let(:attachments) { "#{': -' * 20_000}: foo\n - bar" }

        it 'updates attachment format' do
          Timeout.timeout(1) do
            freeze_time do
              discord_integration.execute(sample_data)

              expect(builder.to_json_hash[:embeds].first)
                .to include(description: "pretext\n#{' -:' * 20_000} foo - bar\n")
            end
          end
        end
      end
    end

    context 'DNS rebind to local address' do
      before do
        stub_dns(webhook_url, ip_address: '192.168.2.120')
      end

      it 'does not allow DNS rebinding' do
        expect { discord_integration.execute(sample_data) }.to raise_error(ArgumentError, /is blocked/)
      end
    end

    context 'when the Discord request fails' do
      before do
        WebMock.stub_request(:post, webhook_url).to_return(status: 400)
      end

      it 'logs an error and returns false' do
        expect(discord_integration).to receive(:log_error).with('400 Bad Request')
        expect(discord_integration.execute(sample_data)).to be(false)
      end
    end
  end
end