2020-09-30 08:09:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Packages::CreateEventService do
|
|
|
|
let(:scope) { 'container' }
|
|
|
|
let(:event_name) { 'push_package' }
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
scope: scope,
|
|
|
|
event_name: event_name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(nil, user, params).execute }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2020-11-17 07:09:15 -05:00
|
|
|
shared_examples 'db package event creation' do |originator_type, expected_scope|
|
|
|
|
before do
|
|
|
|
allow(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
|
|
|
|
end
|
|
|
|
|
2020-10-27 08:08:33 -04:00
|
|
|
context 'with feature flag disable' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(collect_package_events: false)
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
|
2020-11-20 16:09:12 -05:00
|
|
|
it 'does not create an event' do
|
2020-11-17 07:09:15 -05:00
|
|
|
expect { subject }.not_to change { Packages::Event.count }
|
2020-10-27 08:08:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with feature flag enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(collect_package_events: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the event' do
|
|
|
|
expect { subject }.to change { Packages::Event.count }.by(1)
|
|
|
|
|
|
|
|
expect(subject.originator_type).to eq(originator_type)
|
|
|
|
expect(subject.originator).to eq(user&.id)
|
|
|
|
expect(subject.event_scope).to eq(expected_scope)
|
|
|
|
expect(subject.event_type).to eq(event_name)
|
|
|
|
end
|
2020-11-20 16:09:12 -05:00
|
|
|
|
|
|
|
context 'on a read-only instance' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create an event' do
|
|
|
|
expect { subject }.not_to change { Packages::Event.count }
|
|
|
|
end
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
shared_examples 'redis package event creation' do |originator_type, expected_scope|
|
|
|
|
context 'with feature flag disable' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(collect_package_events_redis: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not track the event' do
|
|
|
|
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks the event' do
|
2020-12-08 04:09:41 -05:00
|
|
|
expect(::Gitlab::UsageDataCounters::GuestPackageEventCounter).not_to receive(:count)
|
2020-11-17 07:09:15 -05:00
|
|
|
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(user.id, Packages::Event.allowed_event_name(expected_scope, event_name, originator_type))
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-08 04:09:41 -05:00
|
|
|
shared_examples 'redis package guest event creation' do |originator_type, expected_scope|
|
|
|
|
context 'with feature flag disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(collect_package_events_redis: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not track the event' do
|
|
|
|
expect(::Gitlab::UsageDataCounters::GuestPackageEventCounter).not_to receive(:count)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks the event' do
|
|
|
|
expect(::Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
|
|
|
|
expect(::Gitlab::UsageDataCounters::GuestPackageEventCounter).to receive(:count).with(Packages::Event.allowed_event_name(expected_scope, event_name, originator_type))
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-30 08:09:53 -04:00
|
|
|
context 'with a user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
it_behaves_like 'db package event creation', 'user', 'container'
|
|
|
|
it_behaves_like 'redis package event creation', 'user', 'container'
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a deploy token' do
|
|
|
|
let(:user) { create(:deploy_token) }
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
it_behaves_like 'db package event creation', 'deploy_token', 'container'
|
|
|
|
it_behaves_like 'redis package event creation', 'deploy_token', 'container'
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no user' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
it_behaves_like 'db package event creation', 'guest', 'container'
|
2020-12-08 04:09:41 -05:00
|
|
|
it_behaves_like 'redis package guest event creation', 'guest', 'container'
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a package as scope' do
|
|
|
|
let(:scope) { create(:npm_package) }
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
context 'as guest' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it_behaves_like 'db package event creation', 'guest', 'npm'
|
2020-12-08 04:09:41 -05:00
|
|
|
it_behaves_like 'redis package guest event creation', 'guest', 'npm'
|
2020-11-17 07:09:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it_behaves_like 'db package event creation', 'user', 'npm'
|
|
|
|
it_behaves_like 'redis package event creation', 'user', 'npm'
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|