File: matrix_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 (98 lines) | stat: -rw-r--r-- 3,066 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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Integrations::Matrix, feature_category: :integrations do
  it_behaves_like "chat integration", "Matrix", http_method: :put do
    let(:payload) do
      {
        body: be_present,
        msgtype: 'm.text',
        format: 'org.matrix.custom.html',
        formatted_body: be_present
      }
    end
  end

  describe 'validations' do
    context 'when integration is active' do
      before do
        subject.activate!
      end

      it { is_expected.to validate_presence_of(:token) }
      it { is_expected.to validate_presence_of(:room) }
      it { is_expected.to validate_presence_of(:webhook) }
    end

    context 'when integration is inactive' do
      before do
        subject.deactivate!
      end

      it { is_expected.not_to validate_presence_of(:token) }
      it { is_expected.not_to validate_presence_of(:room) }
      it { is_expected.not_to validate_presence_of(:webhook) }
    end
  end

  describe 'before_validation :set_webhook' do
    let(:integration) { build_stubbed(:matrix_integration) }

    it 'sets webhook value' do
      expect(integration).to be_valid
      expect(integration.webhook).to start_with('https://matrix.org/_matrix/client/v3/rooms/!qPKKM111FFKKsfoCVy:matrix')
    end

    context 'with custom hostname' do
      before do
        integration.hostname = 'https://gitlab.example.com'
      end

      it 'sets webhook value with custom hostname' do
        expect(integration).to be_valid
        expect(integration.webhook).to start_with("https://gitlab.example.com/_matrix/client/v3/rooms/")
      end
    end
  end

  describe '#notify' do
    let(:message) { instance_double(Integrations::ChatMessage::PushMessage, summary: '_Test message') }
    let(:header) { { 'Content-Type' => 'application/json' } }
    let(:response) { instance_double(HTTParty::Response, success?: true) }
    let(:body) do
      {
        body: '_Test message',
        msgtype: 'm.text',
        format: 'org.matrix.custom.html',
        formatted_body: Banzai.render_and_post_process('_Test message', context)
      }.compact_blank
    end

    before do
      allow(Gitlab::HTTP).to receive(:put).and_return(response)
    end

    context 'with project-level integration' do
      let(:subject) { create(:matrix_integration) }
      let(:context) { { project: subject.project, no_sourcepos: true } }

      it 'sends PUT request with `project` context' do
        expect(Gitlab::HTTP).to receive(:put).with(anything, headers: header, body: Gitlab::Json.dump(body))

        subject.send(:notify, message, {})
      end
    end

    context 'without project-level integration' do
      let(:subject) { create(:matrix_integration, :instance) }
      let(:context) { { skip_project_check: true, no_sourcepos: true } }

      it 'sends PUT request with `skip_project_check` context' do
        expect(Gitlab::HTTP).to receive(:put).with(anything, headers: header, body: Gitlab::Json.dump(body))

        subject.send(:notify, message, {})
      end
    end
  end
end