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

trust_score_spec.rb « abuse « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 755309ac699d1464a01704480286f0f479b23a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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