diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-12 15:06:26 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-12 15:06:26 +0000 |
commit | 69944ffb68788d190e81ff7e33db5dcb6c903184 (patch) | |
tree | 4112a1285f186c140749e8410a8a2788b6812500 /spec/support/shared_examples/models/concerns | |
parent | 1b7381e998ff4b33ec8f633766030082e95f10c8 (diff) | |
download | gitlab-ce-69944ffb68788d190e81ff7e33db5dcb6c903184.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support/shared_examples/models/concerns')
-rw-r--r-- | spec/support/shared_examples/models/concerns/issuable_shared_examples.rb | 66 | ||||
-rw-r--r-- | spec/support/shared_examples/models/concerns/redactable_shared_examples.rb | 39 |
2 files changed, 105 insertions, 0 deletions
diff --git a/spec/support/shared_examples/models/concerns/issuable_shared_examples.rb b/spec/support/shared_examples/models/concerns/issuable_shared_examples.rb new file mode 100644 index 00000000000..4978a403324 --- /dev/null +++ b/spec/support/shared_examples/models/concerns/issuable_shared_examples.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +shared_examples_for '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 + +shared_examples_for '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 + +shared_examples_for '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 diff --git a/spec/support/shared_examples/models/concerns/redactable_shared_examples.rb b/spec/support/shared_examples/models/concerns/redactable_shared_examples.rb new file mode 100644 index 00000000000..c5c14901268 --- /dev/null +++ b/spec/support/shared_examples/models/concerns/redactable_shared_examples.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +shared_examples 'model with redactable field' do + it 'redacts unsubscribe token' do + model[field] = 'some text /sent_notifications/00000000000000000000000000000000/unsubscribe more text' + + model.save! + + expect(model[field]).to eq 'some text /sent_notifications/REDACTED/unsubscribe more text' + end + + it 'ignores not hexadecimal tokens' do + text = 'some text /sent_notifications/token/unsubscribe more text' + model[field] = text + + model.save! + + expect(model[field]).to eq text + end + + it 'ignores not matching texts' do + text = 'some text /sent_notifications/.*/unsubscribe more text' + model[field] = text + + model.save! + + expect(model[field]).to eq text + end + + it 'redacts the field when saving the model before creating markdown cache' do + model[field] = 'some text /sent_notifications/00000000000000000000000000000000/unsubscribe more text' + + model.save! + + expected = 'some text /sent_notifications/REDACTED/unsubscribe more text' + expect(model[field]).to eq expected + expect(model["#{field}_html"]).to eq "<p dir=\"auto\">#{expected}</p>" + end +end |