summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/search/abuse_validators/no_abusive_term_length_validator_spec.rb
blob: 67409d9b628a52100ca01bdf06854e4c82b4bbf1 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Search::AbuseValidators::NoAbusiveTermLengthValidator do
  subject do
    described_class.new({ attributes: { foo: :bar }, maximum: limit, maximum_for_url: url_limit })
  end

  let(:limit) { 100 }
  let(:url_limit) { limit * 2 }
  let(:instance) { double(:instance) }
  let(:attribute) { :search }
  let(:validation_msg) { 'abusive term length detected' }
  let(:validate) { subject.validate_each(instance, attribute, search) }

  context 'when a term is over the limit' do
    let(:search) { "this search is too lo#{'n' * limit}g" }

    it 'adds a validation error' do
      expect(instance).to receive_message_chain(:errors, :add).with(attribute, validation_msg)
      validate
    end
  end

  context 'when all terms are under the limit' do
    let(:search) { "what is love? baby don't hurt me" }

    it 'does NOT add any validation errors' do
      expect(instance).not_to receive(:errors)
      validate
    end
  end

  context 'when a URL is detected in a search term' do
    let(:double_limit) { limit * 2 }
    let(:terms) do
      [
        'http://' + 'x' * (double_limit - 12) + '.com',
        'https://' + 'x' * (double_limit - 13) + '.com',
        'sftp://' + 'x' * (double_limit - 12) + '.com',
        'ftp://' + 'x' * (double_limit - 11) + '.com',
        'http://' + 'x' * (double_limit - 8) # no tld is OK
      ]
    end

    context 'when under twice the limit' do
      let(:search) { terms.join(' ') }

      it 'does NOT add any validation errors' do
        search.split.each do |term|
          expect(term.length).to be < url_limit
        end

        expect(instance).not_to receive(:errors)
        validate
      end
    end

    context 'when over twice the limit' do
      let(:search) do
        terms.map { |t| t + 'xxxxxxxx' }.join(' ')
      end

      it 'adds a validation error' do
        expect(instance).to receive_message_chain(:errors, :add).with(attribute, validation_msg)
        validate
      end
    end
  end
end