2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-10-16 17:07:10 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Snippet do
|
2015-05-02 23:11:21 -04:00
|
|
|
describe 'modules' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(Gitlab::VisibilityLevel) }
|
|
|
|
it { is_expected.to include_module(Participable) }
|
|
|
|
it { is_expected.to include_module(Referable) }
|
|
|
|
it { is_expected.to include_module(Sortable) }
|
2016-06-03 05:44:04 -04:00
|
|
|
it { is_expected.to include_module(Awardable) }
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'associations' do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to(:author).class_name('User') }
|
2015-05-02 23:14:31 -04:00
|
|
|
it { is_expected.to belong_to(:project) }
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to have_many(:notes).dependent(:destroy) }
|
2016-06-03 05:44:04 -04:00
|
|
|
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
|
2011-10-16 17:07:10 -04:00
|
|
|
end
|
|
|
|
|
2015-05-02 23:14:31 -04:00
|
|
|
describe 'validation' do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:author) }
|
2012-08-29 11:36:02 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:title) }
|
2016-12-02 07:54:57 -05:00
|
|
|
it { is_expected.to validate_length_of(:title).is_at_most(255) }
|
2012-08-29 11:36:02 -04:00
|
|
|
|
2016-12-02 07:54:57 -05:00
|
|
|
it { is_expected.to validate_length_of(:file_name).is_at_most(255) }
|
2012-08-29 11:36:02 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:content) }
|
2015-05-02 23:14:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
|
2011-10-16 17:07:10 -04:00
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
|
|
|
|
describe '#to_reference' do
|
2016-11-02 19:49:13 -04:00
|
|
|
context 'when snippet belongs to a project' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { build(:project, name: 'sample-project') }
|
2016-11-02 19:49:13 -04:00
|
|
|
let(:snippet) { build(:snippet, id: 1, project: project) }
|
2015-05-02 23:11:21 -04:00
|
|
|
|
2016-11-02 19:49:13 -04:00
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(snippet.to_reference).to eq "$1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a cross-project reference' do
|
2017-08-02 15:55:11 -04:00
|
|
|
another_project = build(:project, name: 'another-project', namespace: project.namespace)
|
2016-11-02 19:49:13 -04:00
|
|
|
expect(snippet.to_reference(another_project)).to eq "sample-project$1"
|
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
|
2016-11-02 19:49:13 -04:00
|
|
|
context 'when snippet does not belong to a project' do
|
|
|
|
let(:snippet) { build(:snippet, id: 1, project: nil) }
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(snippet.to_reference).to eq "$1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'still returns shortest reference when project arg present' do
|
2017-08-02 15:55:11 -04:00
|
|
|
another_project = build(:project, name: 'another-project')
|
2016-11-02 19:49:13 -04:00
|
|
|
expect(snippet.to_reference(another_project)).to eq "$1"
|
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
end
|
2016-03-01 06:51:01 -05:00
|
|
|
|
2016-12-02 07:54:57 -05:00
|
|
|
describe '#file_name' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2016-12-02 07:54:57 -05:00
|
|
|
|
|
|
|
context 'file_name is nil' do
|
|
|
|
let(:snippet) { create(:snippet, project: project, file_name: nil) }
|
|
|
|
|
|
|
|
it 'returns an empty string' do
|
|
|
|
expect(snippet.file_name).to eq ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'file_name is not nil' do
|
|
|
|
let(:snippet) { create(:snippet, project: project, file_name: 'foo.txt') }
|
|
|
|
|
|
|
|
it 'returns the file_name' do
|
|
|
|
expect(snippet.file_name).to eq 'foo.txt'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
describe "#content_html_invalidated?" do
|
|
|
|
let(:snippet) { create(:snippet, content: "md", content_html: "html", file_name: "foo.md") }
|
|
|
|
it "invalidates the HTML cache of content when the filename changes" do
|
|
|
|
expect { snippet.file_name = "foo.rb" }.to change { snippet.content_html_invalidated? }.from(false).to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-01 06:51:01 -05:00
|
|
|
describe '.search' do
|
2017-11-24 06:24:24 -05:00
|
|
|
let(:snippet) { create(:snippet, title: 'test snippet') }
|
2016-03-01 06:51:01 -05:00
|
|
|
|
|
|
|
it 'returns snippets with a matching title' do
|
|
|
|
expect(described_class.search(snippet.title)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a partially matching title' do
|
|
|
|
expect(described_class.search(snippet.title[0..2])).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching title regardless of the casing' do
|
|
|
|
expect(described_class.search(snippet.title.upcase)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching file name' do
|
|
|
|
expect(described_class.search(snippet.file_name)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a partially matching file name' do
|
|
|
|
expect(described_class.search(snippet.file_name[0..2])).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching file name regardless of the casing' do
|
|
|
|
expect(described_class.search(snippet.file_name.upcase)).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 16:44:24 -04:00
|
|
|
describe '.search_code' do
|
2016-03-01 06:51:01 -05:00
|
|
|
let(:snippet) { create(:snippet, content: 'class Foo; end') }
|
|
|
|
|
|
|
|
it 'returns snippets with matching content' do
|
|
|
|
expect(described_class.search_code(snippet.content)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with partially matching content' do
|
|
|
|
expect(described_class.search_code('class')).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with matching content regardless of the casing' do
|
|
|
|
expect(described_class.search_code('FOO')).to eq([snippet])
|
|
|
|
end
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.with_optional_visibility' do
|
|
|
|
context 'when a visibility level is provided' do
|
|
|
|
it 'returns snippets with the given visibility' do
|
|
|
|
create(:snippet, :private)
|
|
|
|
|
|
|
|
snippet = create(:snippet, :public)
|
|
|
|
snippets = described_class
|
|
|
|
.with_optional_visibility(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a visibility level is not provided' do
|
|
|
|
it 'returns all snippets' do
|
|
|
|
snippet1 = create(:snippet, :public)
|
|
|
|
snippet2 = create(:snippet, :private)
|
|
|
|
snippets = described_class.with_optional_visibility
|
|
|
|
|
|
|
|
expect(snippets).to include(snippet1, snippet2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.only_global_snippets' do
|
|
|
|
it 'returns snippets not associated with any projects' do
|
|
|
|
create(:project_snippet)
|
|
|
|
|
|
|
|
snippet = create(:snippet)
|
|
|
|
snippets = described_class.only_global_snippets
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.only_include_projects_visible_to' do
|
|
|
|
let!(:project1) { create(:project, :public) }
|
|
|
|
let!(:project2) { create(:project, :internal) }
|
|
|
|
let!(:project3) { create(:project, :private) }
|
|
|
|
let!(:snippet1) { create(:project_snippet, project: project1) }
|
|
|
|
let!(:snippet2) { create(:project_snippet, project: project2) }
|
|
|
|
let!(:snippet3) { create(:project_snippet, project: project3) }
|
|
|
|
|
|
|
|
context 'when a user is provided' do
|
|
|
|
it 'returns snippets visible to the user' do
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
snippets = described_class.only_include_projects_visible_to(user)
|
|
|
|
|
|
|
|
expect(snippets).to include(snippet1, snippet2)
|
|
|
|
expect(snippets).not_to include(snippet3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is not provided' do
|
|
|
|
it 'returns snippets visible to anonymous users' do
|
|
|
|
snippets = described_class.only_include_projects_visible_to
|
|
|
|
|
|
|
|
expect(snippets).to include(snippet1)
|
|
|
|
expect(snippets).not_to include(snippet2, snippet3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'only_include_projects_with_snippets_enabled' do
|
|
|
|
context 'when the include_private option is enabled' do
|
|
|
|
it 'includes snippets for projects with snippets set to private' do
|
|
|
|
project = create(:project)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::PRIVATE)
|
|
|
|
|
|
|
|
snippet = create(:project_snippet, project: project)
|
|
|
|
|
|
|
|
snippets = described_class
|
|
|
|
.only_include_projects_with_snippets_enabled(include_private: true)
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the include_private option is not enabled' do
|
|
|
|
it 'does not include snippets for projects that have snippets set to private' do
|
|
|
|
project = create(:project)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::PRIVATE)
|
|
|
|
|
|
|
|
create(:project_snippet, project: project)
|
|
|
|
|
|
|
|
snippets = described_class.only_include_projects_with_snippets_enabled
|
|
|
|
|
|
|
|
expect(snippets).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes snippets for projects with snippets enabled' do
|
|
|
|
project = create(:project)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::ENABLED)
|
|
|
|
|
|
|
|
snippet = create(:project_snippet, project: project)
|
|
|
|
snippets = described_class.only_include_projects_with_snippets_enabled
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.only_include_authorized_projects' do
|
|
|
|
it 'only includes snippets for projects the user is authorized to see' do
|
|
|
|
user = create(:user)
|
|
|
|
project1 = create(:project, :private)
|
|
|
|
project2 = create(:project, :private)
|
|
|
|
|
|
|
|
project1.team.add_developer(user)
|
|
|
|
|
|
|
|
create(:project_snippet, project: project2)
|
|
|
|
|
|
|
|
snippet = create(:project_snippet, project: project1)
|
|
|
|
snippets = described_class.only_include_authorized_projects(user)
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_project_with_user' do
|
|
|
|
context 'when a user is provided' do
|
|
|
|
it 'returns an empty collection if the user can not view the snippets' do
|
|
|
|
project = create(:project, :private)
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::ENABLED)
|
|
|
|
|
|
|
|
create(:project_snippet, :public, project: project)
|
|
|
|
|
|
|
|
expect(described_class.for_project_with_user(project, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the snippets if the user is a member of the project' do
|
|
|
|
project = create(:project, :private)
|
|
|
|
user = create(:user)
|
|
|
|
snippet = create(:project_snippet, project: project)
|
|
|
|
|
|
|
|
project.team.add_developer(user)
|
|
|
|
|
|
|
|
snippets = described_class.for_project_with_user(project, user)
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns public snippets for a public project the user is not a member of' do
|
|
|
|
project = create(:project, :public)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::ENABLED)
|
|
|
|
|
|
|
|
user = create(:user)
|
|
|
|
snippet = create(:project_snippet, :public, project: project)
|
|
|
|
|
|
|
|
create(:project_snippet, :private, project: project)
|
|
|
|
|
|
|
|
snippets = described_class.for_project_with_user(project, user)
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is not provided' do
|
|
|
|
it 'returns an empty collection for a private project' do
|
|
|
|
project = create(:project, :private)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::ENABLED)
|
|
|
|
|
|
|
|
create(:project_snippet, :public, project: project)
|
|
|
|
|
|
|
|
expect(described_class.for_project_with_user(project)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns public snippets for a public project' do
|
|
|
|
project = create(:project, :public)
|
|
|
|
snippet = create(:project_snippet, :public, project: project)
|
|
|
|
|
|
|
|
project.project_feature
|
|
|
|
.update(snippets_access_level: ProjectFeature::PUBLIC)
|
|
|
|
|
|
|
|
create(:project_snippet, :private, project: project)
|
|
|
|
|
|
|
|
snippets = described_class.for_project_with_user(project)
|
|
|
|
|
|
|
|
expect(snippets).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.visible_to_or_authored_by' do
|
|
|
|
it 'returns snippets visible to the user' do
|
|
|
|
user = create(:user)
|
|
|
|
snippet1 = create(:snippet, :public)
|
|
|
|
snippet2 = create(:snippet, :private, author: user)
|
|
|
|
snippet3 = create(:snippet, :private)
|
|
|
|
|
|
|
|
snippets = described_class.visible_to_or_authored_by(user)
|
|
|
|
|
|
|
|
expect(snippets).to include(snippet1, snippet2)
|
|
|
|
expect(snippets).not_to include(snippet3)
|
|
|
|
end
|
2016-03-01 06:51:01 -05:00
|
|
|
end
|
2016-06-22 16:44:24 -04:00
|
|
|
|
2016-05-26 07:38:28 -04:00
|
|
|
describe '#participants' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2016-05-26 07:38:28 -04:00
|
|
|
let(:snippet) { create(:snippet, content: 'foo', project: project) }
|
|
|
|
|
|
|
|
let!(:note1) do
|
|
|
|
create(:note_on_project_snippet,
|
|
|
|
noteable: snippet,
|
|
|
|
project: project,
|
|
|
|
note: 'a')
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:note2) do
|
|
|
|
create(:note_on_project_snippet,
|
|
|
|
noteable: snippet,
|
|
|
|
project: project,
|
|
|
|
note: 'b')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the snippet author' do
|
|
|
|
expect(snippet.participants).to include(snippet.author)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the note authors' do
|
|
|
|
expect(snippet.participants).to include(note1.author, note2.author)
|
|
|
|
end
|
|
|
|
end
|
2017-03-20 22:37:29 -04:00
|
|
|
|
|
|
|
describe '#check_for_spam' do
|
|
|
|
let(:snippet) { create :snippet, visibility_level: visibility_level }
|
|
|
|
|
|
|
|
subject do
|
|
|
|
snippet.assign_attributes(title: title)
|
|
|
|
snippet.check_for_spam?
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when public and spammable attributes changed' do
|
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
let(:title) { 'woo' }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when private' do
|
|
|
|
let(:visibility_level) { Snippet::PRIVATE }
|
|
|
|
let(:title) { snippet.title }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when switching to public' do
|
|
|
|
snippet.save!
|
|
|
|
snippet.visibility_level = Snippet::PUBLIC
|
|
|
|
|
|
|
|
expect(snippet.check_for_spam?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when spammable attributes have not changed' do
|
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
let(:title) { snippet.title }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-13 12:47:28 -04:00
|
|
|
|
|
|
|
describe '#blob' do
|
|
|
|
let(:snippet) { create(:snippet) }
|
|
|
|
|
|
|
|
it 'returns a blob representing the snippet data' do
|
|
|
|
blob = snippet.blob
|
|
|
|
|
|
|
|
expect(blob).to be_a(Blob)
|
|
|
|
expect(blob.path).to eq(snippet.file_name)
|
|
|
|
expect(blob.data).to eq(snippet.content)
|
|
|
|
end
|
|
|
|
end
|
2018-12-11 01:28:06 -05:00
|
|
|
|
|
|
|
describe '#embeddable?' do
|
|
|
|
context 'project snippet' do
|
|
|
|
[
|
|
|
|
{ project: :public, snippet: :public, embeddable: true },
|
|
|
|
{ project: :internal, snippet: :public, embeddable: false },
|
|
|
|
{ project: :private, snippet: :public, embeddable: false },
|
|
|
|
{ project: :public, snippet: :internal, embeddable: false },
|
|
|
|
{ project: :internal, snippet: :internal, embeddable: false },
|
|
|
|
{ project: :private, snippet: :internal, embeddable: false },
|
|
|
|
{ project: :public, snippet: :private, embeddable: false },
|
|
|
|
{ project: :internal, snippet: :private, embeddable: false },
|
|
|
|
{ project: :private, snippet: :private, embeddable: false }
|
|
|
|
].each do |combination|
|
|
|
|
it 'only returns true when both project and snippet are public' do
|
|
|
|
project = create(:project, combination[:project])
|
|
|
|
snippet = create(:project_snippet, combination[:snippet], project: project)
|
|
|
|
|
|
|
|
expect(snippet.embeddable?).to eq(combination[:embeddable])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'personal snippet' do
|
|
|
|
[
|
|
|
|
{ snippet: :public, embeddable: true },
|
|
|
|
{ snippet: :internal, embeddable: false },
|
|
|
|
{ snippet: :private, embeddable: false }
|
|
|
|
].each do |combination|
|
|
|
|
it 'only returns true when snippet is public' do
|
|
|
|
snippet = create(:personal_snippet, combination[:snippet])
|
|
|
|
|
|
|
|
expect(snippet.embeddable?).to eq(combination[:embeddable])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-16 17:07:10 -04:00
|
|
|
end
|