2016-05-26 07:16:43 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Banzai::ReferenceParser::CommitParser do
|
2016-05-26 07:16:43 -04:00
|
|
|
include ReferenceParserHelpers
|
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2016-05-26 07:16:43 -04:00
|
|
|
let(:user) { create(:user) }
|
2018-04-03 09:45:17 -04:00
|
|
|
subject { described_class.new(Banzai::RenderContext.new(project, user)) }
|
2016-05-26 07:16:43 -04:00
|
|
|
let(:link) { empty_html_link }
|
|
|
|
|
2016-11-01 16:18:51 -04:00
|
|
|
describe '#nodes_visible_to_user' do
|
|
|
|
context 'when the link has a data-issue attribute' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
link['data-commit'] = 123
|
|
|
|
end
|
2016-11-01 16:18:51 -04:00
|
|
|
|
|
|
|
it_behaves_like "referenced feature visibility", "repository"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
describe '#referenced_by' do
|
|
|
|
context 'when the link has a data-project attribute' do
|
|
|
|
before do
|
|
|
|
link['data-project'] = project.id.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the link has a data-commit attribute' do
|
|
|
|
before do
|
|
|
|
link['data-commit'] = '123'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an Array of commits' do
|
|
|
|
commit = double(:commit)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:valid_repo?)
|
|
|
|
.and_return(true)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject).to receive(:find_commits)
|
|
|
|
.with(project, ['123'])
|
|
|
|
.and_return([commit])
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
expect(subject.referenced_by([link])).to eq([commit])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty Array when the commit could not be found' do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:valid_repo?)
|
|
|
|
.and_return(true)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject).to receive(:find_commits)
|
|
|
|
.with(project, ['123'])
|
|
|
|
.and_return([])
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
expect(subject.referenced_by([link])).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips projects without valid repositories' do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:valid_repo?)
|
|
|
|
.and_return(false)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
expect(subject.referenced_by([link])).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the link does not have a data-commit attribute' do
|
|
|
|
it 'returns an empty Array' do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:valid_repo?)
|
|
|
|
.and_return(true)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
expect(subject.referenced_by([link])).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the link does not have a data-project attribute' do
|
|
|
|
it 'returns an empty Array' do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:valid_repo?)
|
|
|
|
.and_return(true)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
expect(subject.referenced_by([link])).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#commit_ids_per_project' do
|
|
|
|
before do
|
|
|
|
link['data-project'] = project.id.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing commit IDs per project' do
|
|
|
|
link['data-commit'] = '123'
|
|
|
|
|
|
|
|
hash = subject.commit_ids_per_project([link])
|
|
|
|
|
|
|
|
expect(hash).to be_an_instance_of(Hash)
|
|
|
|
|
|
|
|
expect(hash[project.id].to_a).to eq(['123'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add a project when the data-commit attribute is empty' do
|
|
|
|
hash = subject.commit_ids_per_project([link])
|
|
|
|
|
|
|
|
expect(hash).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#find_commits' do
|
|
|
|
it 'returns an Array of commit objects' do
|
|
|
|
commit = double(:commit)
|
|
|
|
|
|
|
|
expect(project).to receive(:commit).with('123').and_return(commit)
|
|
|
|
expect(project).to receive(:valid_repo?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject.find_commits(project, %w{123})).to eq([commit])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips commit IDs for which no commit could be found' do
|
|
|
|
expect(project).to receive(:commit).with('123').and_return(nil)
|
|
|
|
expect(project).to receive(:valid_repo?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject.find_commits(project, %w{123})).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2018-09-30 13:54:21 -04:00
|
|
|
|
|
|
|
context 'when checking commits on another projects' do
|
|
|
|
let(:control_links) do
|
|
|
|
[commit_link]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:actual_links) do
|
|
|
|
control_links + [commit_link, commit_link]
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_link
|
|
|
|
project = create(:project, :repository, :public)
|
|
|
|
|
|
|
|
Nokogiri::HTML.fragment(%Q{<a data-commit="#{project.commit.id}" data-project="#{project.id}"></a>}).children[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'no project N+1 queries'
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|