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

user_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: 6b2cfa6a50413e907625dbbd72b2fed191d64874 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Abuse::UserTrustScore, feature_category: :instance_resiliency do
  let_it_be(:user1) { create(:user) }
  let_it_be(:user2) { create(:user) }
  let(:user_1_scores) { described_class.new(user1) }
  let(:user_2_scores) { described_class.new(user2) }

  describe '#spammer?' do
    context 'when the user is a spammer' do
      before do
        allow(user_1_scores).to receive(:spam_score).and_return(0.9)
      end

      it 'classifies the user as a spammer' do
        expect(user_1_scores).to be_spammer
      end
    end

    context 'when the user is not a spammer' do
      before do
        allow(user_1_scores).to receive(:spam_score).and_return(0.1)
      end

      it 'does not classify the user as a spammer' do
        expect(user_1_scores).not_to be_spammer
      end
    end
  end

  describe '#spam_score' do
    context 'when the user is a spammer' do
      before do
        create(:abuse_trust_score, user: user1, score: 0.8)
        create(:abuse_trust_score, user: user1, score: 0.9)
      end

      it 'returns the expected score' do
        expect(user_1_scores.spam_score).to be_within(0.01).of(0.85)
      end
    end

    context 'when the user is not a spammer' do
      before do
        create(:abuse_trust_score, user: user1, score: 0.1)
        create(:abuse_trust_score, user: user1, score: 0.0)
      end

      it 'returns the expected score' do
        expect(user_1_scores.spam_score).to be_within(0.01).of(0.05)
      end
    end
  end

  describe '#telesign_score' do
    context 'when the user has a telesign risk score' do
      before do
        create(:abuse_trust_score, user: user1, score: 12.0, source: :telesign)
        create(:abuse_trust_score, user: user1, score: 24.0, source: :telesign)
      end

      it 'returns the latest score' do
        expect(user_1_scores.telesign_score).to be(24.0)
      end
    end

    context 'when the user does not have a telesign risk score' do
      it 'defaults to zero' do
        expect(user_2_scores.telesign_score).to be(0.0)
      end
    end
  end

  describe '#arkose_global_score' do
    context 'when the user has an arkose global risk score' do
      before do
        create(:abuse_trust_score, user: user1, score: 12.0, source: :arkose_global_score)
        create(:abuse_trust_score, user: user1, score: 24.0, source: :arkose_global_score)
      end

      it 'returns the latest score' do
        expect(user_1_scores.arkose_global_score).to be(24.0)
      end
    end

    context 'when the user does not have an arkose global risk score' do
      it 'defaults to zero' do
        expect(user_2_scores.arkose_global_score).to be(0.0)
      end
    end
  end

  describe '#arkose_custom_score' do
    context 'when the user has an arkose custom risk score' do
      before do
        create(:abuse_trust_score, user: user1, score: 12.0, source: :arkose_custom_score)
        create(:abuse_trust_score, user: user1, score: 24.0, source: :arkose_custom_score)
      end

      it 'returns the latest score' do
        expect(user_1_scores.arkose_custom_score).to be(24.0)
      end
    end

    context 'when the user does not have an arkose custom risk score' do
      it 'defaults to zero' do
        expect(user_2_scores.arkose_custom_score).to be(0.0)
      end
    end
  end

  describe '#remove_old_scores' do
    context 'if max events is exceeded' do
      before do
        stub_const('Abuse::UserTrustScore::MAX_EVENTS', 2)
      end

      it 'removes the oldest events' do
        first = create(:abuse_trust_score, user: user1)
        create(:abuse_trust_score, user: user1)
        create(:abuse_trust_score, user: user1)

        expect(user1.abuse_trust_scores.count).to eq(2)
        expect(Abuse::TrustScore.find_by_id(first.id)).to eq(nil)
      end
    end
  end
end