Welcome to mirror list, hosted at ThFree Co, Russian Federation.

squash_tm_spec.rb « integrations « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f12e37dae6d86b0d040d5fe44e97ff909cc5163c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::SquashTm, feature_category: :integrations do
  it_behaves_like Integrations::HasWebHook do
    let_it_be(:project) { create(:project) }

    let(:integration) { build(:squash_tm_integration, project: project) }
    let(:hook_url) { "#{integration.url}?token={token}" }
  end

  it_behaves_like Integrations::ResetSecretFields do
    let(:integration) { subject }
  end

  describe 'Validations' do
    context 'when integration is active' do
      before do
        subject.active = true
      end

      it { is_expected.to validate_presence_of(:url) }
      it { is_expected.to allow_value('https://example.com').for(:url) }
      it { is_expected.not_to allow_value(nil).for(:url) }
      it { is_expected.not_to allow_value('').for(:url) }
      it { is_expected.not_to allow_value('foo').for(:url) }
      it { is_expected.not_to allow_value('example.com').for(:url) }

      it { is_expected.not_to validate_presence_of(:token) }
      it { is_expected.to validate_length_of(:token).is_at_most(255) }
      it { is_expected.to allow_value(nil).for(:token) }
      it { is_expected.to allow_value('foo').for(:token) }
    end

    context 'when integration is inactive' do
      before do
        subject.active = false
      end

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

  describe '#execute' do
    let(:integration) { build(:squash_tm_integration, project: build(:project)) }

    let(:squash_tm_hook_url) do
      "#{integration.url}?token=#{integration.token}"
    end

    let_it_be(:user) { create(:user) }
    let_it_be(:issue) { create(:issue) }
    let(:data) { issue.to_hook_data(user) }

    before do
      stub_request(:post, squash_tm_hook_url)
    end

    it 'calls Squash TM API' do
      integration.execute(data)

      expect(a_request(:post, squash_tm_hook_url)).to have_been_made.once
    end
  end

  describe '#test' do
    let(:integration) { build(:squash_tm_integration) }

    let(:squash_tm_hook_url) do
      "#{integration.url}?token=#{integration.token}"
    end

    subject(:result) { integration.test({}) }

    context 'when server is responding' do
      let(:body) { 'OK' }
      let(:status) { 200 }

      before do
        stub_request(:post, squash_tm_hook_url)
          .to_return(status: status, body: body)
      end

      it { is_expected.to eq(success: true, result: 'OK') }
    end

    context 'when server rejects the request' do
      let(:body) { 'Unauthorized' }
      let(:status) { 401 }

      before do
        stub_request(:post, squash_tm_hook_url)
          .to_return(status: status, body: body)
      end

      it { is_expected.to eq(success: false, result: body) }
    end

    context 'when test request executes with errors' do
      before do
        allow(integration).to receive(:execute_web_hook!)
          .with({}, "Test Configuration Hook")
          .and_raise(StandardError, 'error message')
      end

      it { is_expected.to eq(success: false, result: 'error message') }
    end
  end

  describe '.default_test_event' do
    subject { described_class.default_test_event }

    it { is_expected.to eq('issue') }
  end
end