Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/abuse/trust_score_spec.rb')
-rw-r--r--spec/models/abuse/trust_score_spec.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/spec/models/abuse/trust_score_spec.rb b/spec/models/abuse/trust_score_spec.rb
new file mode 100644
index 00000000000..755309ac699
--- /dev/null
+++ b/spec/models/abuse/trust_score_spec.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Abuse::TrustScore, feature_category: :instance_resiliency do
+ let_it_be(:user) { create(:user) }
+
+ let(:correlation_id) { nil }
+
+ let(:abuse_trust_score) do
+ create(:abuse_trust_score, user: user, correlation_id_value: correlation_id)
+ end
+
+ describe 'associations' do
+ it { is_expected.to belong_to(:user) }
+ end
+
+ describe 'validations' do
+ it { is_expected.to validate_presence_of(:user) }
+ it { is_expected.to validate_presence_of(:score) }
+ it { is_expected.to validate_presence_of(:source) }
+ end
+
+ describe 'create' do
+ subject { abuse_trust_score }
+
+ before do
+ allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('123abc')
+ stub_const('Abuse::TrustScore::MAX_EVENTS', 2)
+ end
+
+ context 'if correlation ID is nil' do
+ it 'adds the correlation id' do
+ expect(subject.correlation_id_value).to eq('123abc')
+ end
+ end
+
+ context 'if correlation ID is set' do
+ let(:correlation_id) { 'already-set' }
+
+ it 'does not change the correlation id' do
+ expect(subject.correlation_id_value).to eq('already-set')
+ end
+ end
+
+ context 'if max events is exceeded' do
+ it 'removes the oldest events' do
+ first = create(:abuse_trust_score, user: user)
+ create(:abuse_trust_score, user: user)
+ create(:abuse_trust_score, user: user)
+
+ expect(user.abuse_trust_scores.count).to eq(2)
+ expect(described_class.find_by_id(first.id)).to eq(nil)
+ end
+ end
+ end
+end