2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe WebHookLog do
|
2017-04-27 06:08:57 -04:00
|
|
|
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-06-28 09:34:31 -04:00
|
|
|
describe '.recent' do
|
|
|
|
let(:hook) { create(:project_hook) }
|
|
|
|
|
|
|
|
it 'does not return web hook logs that are too old' do
|
|
|
|
create(:web_hook_log, web_hook: hook, created_at: 91.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
|
|
|
|
|
2019-12-17 10:08:15 -05:00
|
|
|
describe '#save' do
|
|
|
|
let(:web_hook_log) { build(:web_hook_log, url: url) }
|
|
|
|
let(:url) { 'http://example.com' }
|
|
|
|
|
|
|
|
subject { web_hook_log.save! }
|
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
|
|
|
|
context 'with basic auth credentials' do
|
|
|
|
let(:url) { 'http://test:123@example.com'}
|
|
|
|
|
|
|
|
it 'obfuscates the basic auth credentials' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(web_hook_log.url).to eq('http://*****:*****@example.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
describe '#success?' do
|
|
|
|
let(:web_hook_log) { build(:web_hook_log, response_status: status) }
|
|
|
|
|
|
|
|
describe '2xx' do
|
|
|
|
let(:status) { '200' }
|
2019-12-18 19:08:01 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it { expect(web_hook_log.success?).to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'not 2xx' do
|
|
|
|
let(:status) { '500' }
|
2019-12-18 19:08:01 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it { expect(web_hook_log.success?).to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'internal erorr' do
|
|
|
|
let(:status) { 'internal error' }
|
2019-12-18 19:08:01 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it { expect(web_hook_log.success?).to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
2020-01-27 04:08:32 -05:00
|
|
|
|
|
|
|
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
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|