summaryrefslogtreecommitdiff
path: root/spec/models/snippet_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-20 09:07:57 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-20 09:07:57 +0000
commit7881eb30eaa8b01dbcfe87faa09927c75c7d6e45 (patch)
tree298bc8d2c62b2f2c29cb8ecbcf3de3eaaa6466d9 /spec/models/snippet_spec.rb
parent64b66e0cb6d1bfd27abf24e06653f00bddb60597 (diff)
downloadgitlab-ce-7881eb30eaa8b01dbcfe87faa09927c75c7d6e45.tar.gz
Add latest changes from gitlab-org/gitlab@12-6-stable-ee
Diffstat (limited to 'spec/models/snippet_spec.rb')
-rw-r--r--spec/models/snippet_spec.rb73
1 files changed, 73 insertions, 0 deletions
diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb
index e4cc8931840..9c549a6d56d 100644
--- a/spec/models/snippet_spec.rb
+++ b/spec/models/snippet_spec.rb
@@ -18,6 +18,7 @@ describe Snippet do
it { is_expected.to belong_to(:project) }
it { is_expected.to have_many(:notes).dependent(:destroy) }
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
+ it { is_expected.to have_many(:user_mentions).class_name("SnippetUserMention") }
end
describe 'validation' do
@@ -31,6 +32,62 @@ describe Snippet do
it { is_expected.to validate_presence_of(:content) }
it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
+
+ it do
+ allow(Gitlab::CurrentSettings).to receive(:snippet_size_limit).and_return(1)
+
+ is_expected
+ .to validate_length_of(:content)
+ .is_at_most(Gitlab::CurrentSettings.snippet_size_limit)
+ .with_message("is too long (2 Bytes). The maximum size is 1 Byte.")
+ end
+
+ context 'content validations' do
+ context 'with existing snippets' do
+ let(:snippet) { create(:personal_snippet, content: 'This is a valid content at the time of creation') }
+
+ before do
+ expect(snippet).to be_valid
+
+ stub_application_setting(snippet_size_limit: 2)
+ end
+
+ it 'does not raise a validation error if the content is not changed' do
+ snippet.title = 'new title'
+
+ expect(snippet).to be_valid
+ end
+
+ it 'raises and error if the content is changed and the size is bigger than limit' do
+ snippet.content = snippet.content + "test"
+
+ expect(snippet).not_to be_valid
+ end
+ end
+
+ context 'with new snippets' do
+ let(:limit) { 15 }
+
+ before do
+ stub_application_setting(snippet_size_limit: limit)
+ end
+
+ it 'is valid when content is smaller than the limit' do
+ snippet = build(:personal_snippet, content: 'Valid Content')
+
+ expect(snippet).to be_valid
+ end
+
+ it 'raises error when content is bigger than setting limit' do
+ snippet = build(:personal_snippet, content: 'This is an invalid content')
+
+ aggregate_failures do
+ expect(snippet).not_to be_valid
+ expect(snippet.errors[:content]).to include("is too long (#{snippet.content.size} Bytes). The maximum size is #{limit} Bytes.")
+ end
+ end
+ end
+ end
end
describe '#to_reference' do
@@ -451,4 +508,20 @@ describe Snippet do
expect(blob.data).to eq(snippet.content)
end
end
+
+ describe '#to_json' do
+ let(:snippet) { build(:snippet) }
+
+ it 'excludes secret_token from generated json' do
+ expect(JSON.parse(to_json).keys).not_to include("secret_token")
+ end
+
+ it 'does not override existing exclude option value' do
+ expect(JSON.parse(to_json(except: [:id])).keys).not_to include("secret_token", "id")
+ end
+
+ def to_json(params = {})
+ snippet.to_json(params)
+ end
+ end
end