2020-03-04 07:07:52 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Lograge::CustomOptions do
|
2020-03-04 07:07:52 -05:00
|
|
|
describe '.call' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'controller' => 'ApplicationController',
|
|
|
|
'action' => 'show',
|
|
|
|
'format' => 'html',
|
|
|
|
'a' => 'b'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-01 14:08:07 -04:00
|
|
|
let(:event_payload) do
|
|
|
|
{
|
|
|
|
params: params,
|
|
|
|
user_id: 'test',
|
|
|
|
cf_ray: SecureRandom.hex,
|
|
|
|
cf_request_id: SecureRandom.hex,
|
|
|
|
metadata: { 'meta.user' => 'jane.doe' }
|
|
|
|
}
|
2020-03-04 07:07:52 -05:00
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2020-06-01 14:08:07 -04:00
|
|
|
let(:event) { ActiveSupport::Notifications::Event.new('test', 1, 2, 'transaction_id', event_payload) }
|
2020-03-04 07:07:52 -05:00
|
|
|
|
|
|
|
subject { described_class.call(event) }
|
|
|
|
|
|
|
|
it 'ignores some parameters' do
|
|
|
|
param_keys = subject[:params].map { |param| param[:key] }
|
|
|
|
|
|
|
|
expect(param_keys).not_to include(*described_class::IGNORE_PARAMS)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'formats the parameters' do
|
|
|
|
expect(subject[:params]).to eq([{ key: 'a', value: 'b' }])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds the current time' do
|
|
|
|
travel_to(5.days.ago) do
|
|
|
|
expected_time = Time.now.utc.iso8601(3)
|
|
|
|
|
|
|
|
expect(subject[:time]).to eq(expected_time)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds the user id' do
|
|
|
|
expect(subject[:user_id]).to eq('test')
|
|
|
|
end
|
2020-05-11 20:10:11 -04:00
|
|
|
|
|
|
|
it 'adds Cloudflare headers' do
|
|
|
|
expect(subject[:cf_ray]).to eq(event.payload[:cf_ray])
|
|
|
|
expect(subject[:cf_request_id]).to eq(event.payload[:cf_request_id])
|
|
|
|
end
|
2020-05-15 14:07:52 -04:00
|
|
|
|
|
|
|
it 'adds the metadata' do
|
|
|
|
expect(subject['meta.user']).to eq('jane.doe')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metadata is missing' do
|
2020-06-01 14:08:07 -04:00
|
|
|
let(:event_payload) { { params: {} } }
|
2020-05-15 14:07:52 -04:00
|
|
|
|
|
|
|
it 'does not break' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2020-06-01 14:08:07 -04:00
|
|
|
|
2020-07-08 02:09:13 -04:00
|
|
|
context 'when correlation_id is overridden' do
|
2020-06-01 14:08:07 -04:00
|
|
|
let(:correlation_id_key) { Labkit::Correlation::CorrelationId::LOG_KEY }
|
|
|
|
|
|
|
|
before do
|
|
|
|
event_payload[correlation_id_key] = '123456'
|
|
|
|
end
|
|
|
|
|
2020-07-08 02:09:13 -04:00
|
|
|
it 'sets the overridden value' do
|
2020-06-01 14:08:07 -04:00
|
|
|
expect(subject[correlation_id_key]).to eq('123456')
|
|
|
|
end
|
|
|
|
end
|
2020-03-04 07:07:52 -05:00
|
|
|
end
|
|
|
|
end
|