summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/concerns/issuable_shared_examples.rb
blob: 3a40708899709de4962affd39232a86a610d386e (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
# frozen_string_literal: true

RSpec.shared_examples 'matches_cross_reference_regex? fails fast' do
  it 'fails fast for long strings' do
    # took well under 1 second in CI https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/3267#note_172823
    expect do
      Timeout.timeout(6.seconds) { mentionable.matches_cross_reference_regex? }
    end.not_to raise_error
  end
end

RSpec.shared_examples 'validates description length with custom validation' do
  let(:issuable) { build(:issue, description: 'x' * (::Issuable::DESCRIPTION_LENGTH_MAX + 1)) }
  let(:context) { :update }

  subject { issuable.validate(context) }

  context 'when Issuable is a new record' do
    it 'validates the maximum description length' do
      subject
      expect(issuable.errors[:description]).to eq(["is too long (maximum is #{::Issuable::DESCRIPTION_LENGTH_MAX} characters)"])
    end

    context 'on create' do
      let(:context) { :create }

      it 'does not validate the maximum description length' do
        allow(issuable).to receive(:description_max_length_for_new_records_is_valid).and_call_original

        subject

        expect(issuable).not_to have_received(:description_max_length_for_new_records_is_valid)
      end
    end
  end

  context 'when Issuable is an existing record' do
    before do
      allow(issuable).to receive(:expire_etag_cache) # to skip the expire_etag_cache callback

      issuable.save!(validate: false)
    end

    it 'does not validate the maximum description length' do
      subject
      expect(issuable.errors).not_to have_key(:description)
    end
  end
end

RSpec.shared_examples 'truncates the description to its allowed maximum length on import' do
  before do
    allow(issuable).to receive(:importing?).and_return(true)
  end

  let(:issuable) { build(:issue, description: 'x' * (::Issuable::DESCRIPTION_LENGTH_MAX + 1)) }

  subject { issuable.validate(:create) }

  it 'truncates the description to its allowed maximum length' do
    subject

    expect(issuable.description).to eq('x' * ::Issuable::DESCRIPTION_LENGTH_MAX)
    expect(issuable.errors[:description]).to be_empty
  end
end