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

web_hook_log_spec.rb « hooks « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fafca144cae269a63ab2fa1e8dabb7d69d86c8de (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WebHookLog do
  it { is_expected.to belong_to(:web_hook) }

  it { is_expected.to serialize(:request_headers).as(Hash) }
  it { is_expected.to serialize(:request_data).as(Hash) }
  it { is_expected.to serialize(:response_headers).as(Hash) }

  it { is_expected.to validate_presence_of(:web_hook) }

  describe '.recent' do
    let(:hook) { build(:project_hook) }

    it 'does not return web hook logs that are too old' do
      create(:web_hook_log, web_hook: hook, created_at: 10.days.ago)

      expect(described_class.recent.size).to be_zero
    end

    it 'returns the web hook logs in descending order' do
      hook1 = create(:web_hook_log, web_hook: hook, created_at: 2.hours.ago)
      hook2 = create(:web_hook_log, web_hook: hook, created_at: 1.hour.ago)
      hooks = described_class.recent.to_a

      expect(hooks).to eq([hook2, hook1])
    end
  end

  describe '#save' do
    let(:hook) { build(:project_hook) }

    context 'with basic auth credentials' do
      let(:web_hook_log) { build(:web_hook_log, web_hook: hook, url: 'http://test:123@example.com') }

      subject { web_hook_log.save! }

      it { is_expected.to eq(true) }

      it 'obfuscates the basic auth credentials' do
        subject

        expect(web_hook_log.url).to eq('http://*****:*****@example.com')
      end
    end

    context "with users' emails" do
      let(:author) { build(:user) }
      let(:user) { build(:user) }
      let(:web_hook_log) { create(:web_hook_log, web_hook: hook, request_data: data) }
      let(:data) do
        {
          user: {
            name: user.name,
            email: user.email
          },
          commits: [
            {
              user: {
                name: author.name,
                email: author.email
              }
            },
            {
              user: {
                name: user.name,
                email: user.email
              }
            }
          ]
        }.deep_stringify_keys
      end

      it "redacts users' emails" do
        expect(web_hook_log.request_data['user']).to match a_hash_including(
          'name' => user.name,
          'email' => _('[REDACTED]')
        )
        expect(web_hook_log.request_data['commits'].pluck('user')).to match_array(
          [
            {
              'name' => author.name,
              'email' => _('[REDACTED]')
            },
            {
              'name' => user.name,
              'email' => _('[REDACTED]')
            }
          ]
        )
      end
    end
  end

  describe '.delete_batch_for' do
    let_it_be(:hook) { build(:project_hook) }
    let_it_be(:hook2) { build(:project_hook) }

    before_all do
      create_list(:web_hook_log, 3, web_hook: hook)
      create_list(:web_hook_log, 3, web_hook: hook2)
    end

    subject { described_class.delete_batch_for(hook, batch_size: batch_size) }

    shared_examples 'deletes batch of web hook logs' do
      it { is_expected.to be(batch_size <= 3) }

      it 'deletes min(batch_size, total) records' do
        deleted = [batch_size, 3].min

        expect { subject }.to change(described_class, :count).by(-deleted)
      end
    end

    context 'when the batch size is less than one' do
      let(:batch_size) { 0 }

      it 'raises an argument error' do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context 'when the batch size is smaller than the total' do
      let(:batch_size) { 2 }

      include_examples 'deletes batch of web hook logs'
    end

    context 'when the batch size is equal to the total' do
      let(:batch_size) { 3 }

      include_examples 'deletes batch of web hook logs'
    end

    context 'when the batch size is greater than the total' do
      let(:batch_size) { 1000 }

      include_examples 'deletes batch of web hook logs'
    end

    it 'does not loop forever' do
      batches = 0
      batches += 1 while described_class.delete_batch_for(hook, batch_size: 1)

      expect(hook.web_hook_logs).to be_none
      expect(described_class.count).to eq 3
      expect(batches).to eq 3 # true three times, stops at first false
    end
  end

  describe '#success?' do
    let(:web_hook_log) { build(:web_hook_log, response_status: status) }

    describe '2xx' do
      let(:status) { '200' }

      it { expect(web_hook_log.success?).to be_truthy }
    end

    describe 'not 2xx' do
      let(:status) { '500' }

      it { expect(web_hook_log.success?).to be_falsey }
    end

    describe 'internal erorr' do
      let(:status) { 'internal error' }

      it { expect(web_hook_log.success?).to be_falsey }
    end
  end

  describe '#internal_error?' do
    let(:web_hook_log) { build_stubbed(:web_hook_log, response_status: status) }

    context 'when response status is not an internal error' do
      let(:status) { '200' }

      it { expect(web_hook_log.internal_error?).to be_falsey }
    end

    context 'when response status is an internal error' do
      let(:status) { 'internal error' }

      it { expect(web_hook_log.internal_error?).to be_truthy }
    end
  end
end