debian-mirror-gitlab/spec/lib/gitlab/alert_management/fingerprint_spec.rb

77 lines
2 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::AlertManagement::Fingerprint do
2020-06-23 00:09:42 +05:30
using RSpec::Parameterized::TableSyntax
let_it_be(:alert) { create(:alert_management_alert) }
describe '.generate' do
subject { described_class.generate(data) }
context 'when data is an array' do
let(:data) { [1, 'fingerprint', 'given'] }
it 'returns the hashed fingerprint' do
expected_fingerprint = Digest::SHA1.hexdigest(data.flatten.map!(&:to_s).join)
expect(subject).to eq(expected_fingerprint)
end
2020-07-28 23:09:34 +05:30
context 'with a variety of data' do
where(:data) do
[
111,
'fingerprint',
:fingerprint,
true
]
end
with_them do
it 'performs like a hashed fingerprint' do
expect(subject).to eq(Digest::SHA1.hexdigest(data.to_s))
end
end
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
context 'when data is a hash' do
let(:data) { { test: true } }
shared_examples 'fingerprinted Hash' do
2020-06-23 00:09:42 +05:30
it 'performs like a hashed fingerprint' do
2020-07-28 23:09:34 +05:30
flattened_hash = Gitlab::Utils::SafeInlineHash.merge_keys!(data).sort.to_s
expect(subject).to eq(Digest::SHA1.hexdigest(flattened_hash))
end
end
it_behaves_like 'fingerprinted Hash'
context 'hashes with different order' do
it 'calculates the same result' do
data = { test: true, another_test: 1 }
data_hash = described_class.generate(data)
reverse_data = { another_test: 1, test: true }
reverse_data_hash = described_class.generate(reverse_data)
expect(data_hash).to eq(reverse_data_hash)
end
end
context 'hash is too large' do
before do
expect_next_instance_of(Gitlab::Utils::SafeInlineHash) do |obj|
expect(obj).to receive(:valid?).and_return(false)
end
end
it 'raises an error' do
expect { subject }.to raise_error(ArgumentError)
2020-06-23 00:09:42 +05:30
end
end
end
end
end