2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe WebHookLog do
|
2017-09-10 17:25:29 +05:30
|
|
|
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) }
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
describe '.recent' do
|
|
|
|
let(:hook) { create(:project_hook) }
|
|
|
|
|
|
|
|
it 'does not return web hook logs that are too old' do
|
2022-05-07 20:08:51 +05:30
|
|
|
create(:web_hook_log, web_hook: hook, created_at: 10.days.ago)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
describe '#save' do
|
2022-07-29 17:44:30 +05:30
|
|
|
context 'with basic auth credentials' do
|
|
|
|
let(:web_hook_log) { build(:web_hook_log, url: 'http://test:123@example.com') }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-07-29 17:44:30 +05:30
|
|
|
subject { web_hook_log.save! }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-07-29 17:44:30 +05:30
|
|
|
it { is_expected.to eq(true) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
it 'obfuscates the basic auth credentials' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(web_hook_log.url).to eq('http://*****:*****@example.com')
|
|
|
|
end
|
|
|
|
end
|
2022-07-29 17:44:30 +05:30
|
|
|
|
2022-10-02 17:18:49 +05:30
|
|
|
context "with users' emails" do
|
2022-07-29 17:44:30 +05:30
|
|
|
let(:author) { create(:user) }
|
2022-10-02 17:18:49 +05:30
|
|
|
let(:user) { create(:user) }
|
2022-07-29 17:44:30 +05:30
|
|
|
let(:web_hook_log) { create(:web_hook_log, request_data: data) }
|
|
|
|
let(:data) do
|
|
|
|
{
|
2022-10-02 17:18:49 +05:30
|
|
|
user: {
|
|
|
|
name: user.name,
|
|
|
|
email: user.email
|
|
|
|
},
|
|
|
|
commits: [
|
|
|
|
{
|
|
|
|
user: {
|
|
|
|
name: author.name,
|
|
|
|
email: author.email
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
user: {
|
|
|
|
name: user.name,
|
|
|
|
email: user.email
|
|
|
|
}
|
2022-07-29 17:44:30 +05:30
|
|
|
}
|
2022-10-02 17:18:49 +05:30
|
|
|
]
|
2022-07-29 17:44:30 +05:30
|
|
|
}.deep_stringify_keys
|
|
|
|
end
|
|
|
|
|
2022-10-02 17:18:49 +05:30
|
|
|
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]')
|
|
|
|
}
|
|
|
|
]
|
2022-07-29 17:44:30 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
describe '.delete_batch_for' do
|
|
|
|
let(:hook) { create(:project_hook) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create_list(:web_hook_log, 3, web_hook: hook)
|
|
|
|
create_list(:web_hook_log, 3)
|
|
|
|
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
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#success?' do
|
|
|
|
let(:web_hook_log) { build(:web_hook_log, response_status: status) }
|
|
|
|
|
|
|
|
describe '2xx' do
|
|
|
|
let(:status) { '200' }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(web_hook_log.success?).to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'not 2xx' do
|
|
|
|
let(:status) { '500' }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(web_hook_log.success?).to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'internal erorr' do
|
|
|
|
let(:status) { 'internal error' }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it { expect(web_hook_log.success?).to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
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
|
2023-01-10 11:22:00 +05:30
|
|
|
|
|
|
|
describe '#request_headers' do
|
|
|
|
let(:hook) { build(:project_hook, :token) }
|
|
|
|
let(:web_hook_log) { build(:web_hook_log, request_headers: request_headers) }
|
|
|
|
let(:expected_headers) { { 'X-Gitlab-Token' => _('[REDACTED]') } }
|
|
|
|
|
|
|
|
context 'with redacted headers token' do
|
|
|
|
let(:request_headers) { { 'X-Gitlab-Token' => _('[REDACTED]') } }
|
|
|
|
|
|
|
|
it { expect(web_hook_log.request_headers).to eq(expected_headers) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with exposed headers token' do
|
|
|
|
let(:request_headers) { { 'X-Gitlab-Token' => hook.token } }
|
|
|
|
|
|
|
|
it { expect(web_hook_log.request_headers).to eq(expected_headers) }
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|