2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Banzai::ReferenceParser::SnippetParser do
|
2016-05-26 07:16:43 -04:00
|
|
|
include ReferenceParserHelpers
|
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-06-08 12:56:39 -04:00
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-06-08 12:56:39 -04:00
|
|
|
let(:external_user) { create(:user, :external) }
|
|
|
|
let(:project_member) { create(:user) }
|
|
|
|
|
2018-04-03 09:45:17 -04:00
|
|
|
subject { described_class.new(Banzai::RenderContext.new(project, user)) }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
let(:link) { empty_html_link }
|
|
|
|
|
2017-06-08 12:56:39 -04:00
|
|
|
def visible_references(snippet_visibility, user = nil)
|
|
|
|
snippet = create(:project_snippet, snippet_visibility, project: project)
|
|
|
|
link['data-project'] = project.id.to_s
|
|
|
|
link['data-snippet'] = snippet.id.to_s
|
|
|
|
|
|
|
|
subject.nodes_visible_to_user(user, [link])
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_user(project_member, :developer)
|
|
|
|
end
|
|
|
|
|
2016-11-01 16:18:51 -04:00
|
|
|
describe '#nodes_visible_to_user' do
|
2017-06-08 12:56:39 -04:00
|
|
|
context 'when a project is public and the snippets feature is enabled for everyone' do
|
|
|
|
before do
|
|
|
|
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::ENABLED)
|
|
|
|
end
|
|
|
|
|
2020-07-21 08:09:30 -04:00
|
|
|
it 'avoids N+1 cached queries', :use_sql_query_cache do
|
|
|
|
# Run this once to establish a baseline
|
|
|
|
visible_references(:public)
|
|
|
|
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
|
|
|
|
subject.nodes_visible_to_user(user, [link])
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject.nodes_visible_to_user(user, Array.new(10, link)) }.not_to exceed_all_query_limit(control_count.count)
|
|
|
|
end
|
|
|
|
|
2017-06-08 12:56:39 -04:00
|
|
|
it 'creates a reference for guest for a public snippet' do
|
|
|
|
expect(visible_references(:public)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a regular user for a public snippet' do
|
|
|
|
expect(visible_references(:public, user)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a regular user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, user)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for an external user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, external_user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a private snippet' do
|
|
|
|
expect(visible_references(:private, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for a private snippet' do
|
|
|
|
expect(visible_references(:private, user)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project is public and the snippets feature is enabled for project team members' do
|
|
|
|
before do
|
|
|
|
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a public snippet' do
|
|
|
|
expect(visible_references(:public, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for guest for a public snippet' do
|
|
|
|
expect(visible_references(:public, nil)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for a public snippet' do
|
|
|
|
expect(visible_references(:public, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a private snippet' do
|
|
|
|
expect(visible_references(:private, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for a private snippet' do
|
|
|
|
expect(visible_references(:private, user)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project is internal and the snippets feature is enabled for everyone' do
|
|
|
|
before do
|
|
|
|
project.update_attribute(:visibility, Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::ENABLED)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for guest for a public snippet' do
|
|
|
|
expect(visible_references(:public)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for an external user for a public snippet' do
|
|
|
|
expect(visible_references(:public, external_user)).to be_empty
|
|
|
|
end
|
2016-11-01 16:18:51 -04:00
|
|
|
|
2017-06-08 12:56:39 -04:00
|
|
|
it 'creates a reference for a regular user for a public snippet' do
|
|
|
|
expect(visible_references(:public, user)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a regular user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, user)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for an external user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, external_user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a private snippet' do
|
|
|
|
expect(visible_references(:private, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for a private snippet' do
|
|
|
|
expect(visible_references(:private, user)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project is internal and the snippets feature is enabled for project team members' do
|
|
|
|
before do
|
|
|
|
project.update_attribute(:visibility, Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a public snippet' do
|
|
|
|
expect(visible_references(:public, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for guest for a public snippet' do
|
|
|
|
expect(visible_references(:public, nil)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create reference for a regular user for a public snippet' do
|
|
|
|
expect(visible_references(:public, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a private snippet' do
|
|
|
|
expect(visible_references(:private, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create reference for a regular user for a private snippet' do
|
|
|
|
expect(visible_references(:private, user)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project is private and the snippets feature is enabled for project team members' do
|
|
|
|
before do
|
|
|
|
project.update_attribute(:visibility, Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a public snippet' do
|
|
|
|
expect(visible_references(:public, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for guest for a public snippet' do
|
|
|
|
expect(visible_references(:public, nil)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for a public snippet' do
|
|
|
|
expect(visible_references(:public, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for an internal snippet' do
|
|
|
|
expect(visible_references(:internal, user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reference for a project member for a private snippet' do
|
|
|
|
expect(visible_references(:private, project_member)).to eq([link])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a reference for a regular user for a private snippet' do
|
|
|
|
expect(visible_references(:private, user)).to be_empty
|
|
|
|
end
|
2016-11-01 16:18:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
describe '#referenced_by' do
|
2017-06-08 12:56:39 -04:00
|
|
|
let(:snippet) { create(:snippet, project: project) }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
describe 'when the link has a data-snippet attribute' do
|
|
|
|
context 'using an existing snippet ID' do
|
|
|
|
it 'returns an Array of snippets' do
|
|
|
|
link['data-snippet'] = snippet.id.to_s
|
|
|
|
|
|
|
|
expect(subject.referenced_by([link])).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using a non-existing snippet ID' do
|
|
|
|
it 'returns an empty Array' do
|
|
|
|
link['data-snippet'] = ''
|
|
|
|
|
2017-06-08 12:56:39 -04:00
|
|
|
expect(subject.referenced_by([link])).to be_empty
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|