2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-12 18:27:42 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-06-15 14:09:57 -04:00
|
|
|
RSpec.describe Gitlab::Checks::ChangesAccess do
|
2021-07-29 08:08:55 -04:00
|
|
|
include_context 'changes access checks context'
|
|
|
|
|
|
|
|
subject { changes_access }
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
describe '#validate!' do
|
2021-07-29 08:08:55 -04:00
|
|
|
shared_examples '#validate!' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:lfs_enabled?).and_return(true)
|
|
|
|
end
|
2016-08-12 18:27:42 -04:00
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
context 'without failed checks' do
|
|
|
|
it "doesn't raise an error" do
|
|
|
|
expect { subject.validate! }.not_to raise_error
|
|
|
|
end
|
2021-06-15 14:09:57 -04:00
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
it 'calls lfs checks' do
|
|
|
|
expect_next_instance_of(Gitlab::Checks::LfsCheck) do |instance|
|
|
|
|
expect(instance).to receive(:validate!)
|
|
|
|
end
|
2016-08-12 18:27:42 -04:00
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
subject.validate!
|
|
|
|
end
|
2016-08-12 18:27:42 -04:00
|
|
|
end
|
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
context 'when time limit was reached' do
|
|
|
|
it 'raises a TimeoutError' do
|
|
|
|
logger = Gitlab::Checks::TimedLogger.new(start_time: timeout.ago, timeout: timeout)
|
|
|
|
access = described_class.new(changes,
|
|
|
|
project: project,
|
|
|
|
user_access: user_access,
|
|
|
|
protocol: protocol,
|
|
|
|
logger: logger)
|
|
|
|
|
|
|
|
expect { access.validate! }.to raise_error(Gitlab::Checks::TimedLogger::TimeoutError)
|
2019-12-06 19:07:51 -05:00
|
|
|
end
|
2021-07-29 08:08:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with batched commits enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(changes_batch_commits: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#validate!'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with batched commits disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(changes_batch_commits: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#validate!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#commits' do
|
|
|
|
it 'calls #new_commits' do
|
2021-08-06 17:10:11 -04:00
|
|
|
expect(project.repository).to receive(:new_commits).and_call_original
|
2021-07-29 08:08:55 -04:00
|
|
|
|
|
|
|
expect(subject.commits).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when changes contain empty revisions' do
|
|
|
|
let(:changes) { [{ newrev: newrev }, { newrev: '' }, { newrev: Gitlab::Git::BLANK_SHA }] }
|
|
|
|
let(:expected_commit) { instance_double(Commit) }
|
|
|
|
|
|
|
|
it 'returns only commits with non empty revisions' do
|
2021-08-04 23:10:19 -04:00
|
|
|
expect(project.repository).to receive(:new_commits).with([newrev], { allow_quarantine: true }) { [expected_commit] }
|
2021-07-29 08:08:55 -04:00
|
|
|
expect(subject.commits).to eq([expected_commit])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-31 12:57:29 -04:00
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
describe '#commits_for' do
|
|
|
|
let(:new_commits) { [] }
|
|
|
|
let(:expected_commits) { [] }
|
|
|
|
|
|
|
|
shared_examples 'a listing of new commits' do
|
|
|
|
it 'returns expected commits' do
|
|
|
|
expect(subject).to receive(:commits).and_return(new_commits)
|
|
|
|
|
|
|
|
expect(subject.commits_for(newrev)).to eq(expected_commits)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no commits' do
|
|
|
|
it_behaves_like 'a listing of new commits'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with unrelated commits' do
|
|
|
|
let(:new_commits) { [create_commit('1234', %w[1111 2222])] }
|
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with single related commit' do
|
|
|
|
let(:new_commits) { [create_commit(newrev, %w[1111 2222])] }
|
|
|
|
let(:expected_commits) { new_commits }
|
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with single related and unrelated commit' do
|
|
|
|
let(:new_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111 2222]),
|
|
|
|
create_commit('abcd', %w[1111 2222])
|
|
|
|
]
|
2017-03-31 12:57:29 -04:00
|
|
|
end
|
2021-07-29 08:08:55 -04:00
|
|
|
|
|
|
|
let(:expected_commits) do
|
|
|
|
[create_commit(newrev, %w[1111 2222])]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
2016-08-12 18:27:42 -04:00
|
|
|
end
|
2017-08-24 21:30:12 -04:00
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
context 'with multiple related commits' do
|
|
|
|
let(:new_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111]),
|
|
|
|
create_commit('1111', %w[2222]),
|
|
|
|
create_commit('abcd', [])
|
|
|
|
]
|
|
|
|
end
|
2018-02-07 08:00:53 -05:00
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
let(:expected_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111]),
|
|
|
|
create_commit('1111', %w[2222])
|
|
|
|
]
|
2018-02-07 08:00:53 -05:00
|
|
|
end
|
2021-07-29 08:08:55 -04:00
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
2018-02-07 08:00:53 -05:00
|
|
|
end
|
2021-07-29 08:08:55 -04:00
|
|
|
|
|
|
|
context 'with merge commits' do
|
|
|
|
let(:new_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111 2222 3333]),
|
|
|
|
create_commit('1111', []),
|
|
|
|
create_commit('3333', %w[4444]),
|
|
|
|
create_commit('4444', [])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111 2222 3333]),
|
|
|
|
create_commit('1111', []),
|
|
|
|
create_commit('3333', %w[4444]),
|
|
|
|
create_commit('4444', [])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
|
|
|
end
|
2021-08-12 11:09:58 -04:00
|
|
|
|
|
|
|
context 'with criss-cross merges' do
|
|
|
|
let(:new_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[a1 b1]),
|
|
|
|
create_commit('a1', %w[a2 b2]),
|
|
|
|
create_commit('a2', %w[a3 b3]),
|
|
|
|
create_commit('a3', %w[c]),
|
|
|
|
create_commit('b1', %w[b2 a2]),
|
|
|
|
create_commit('b2', %w[b3 a3]),
|
|
|
|
create_commit('b3', %w[c]),
|
|
|
|
create_commit('c', [])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[a1 b1]),
|
|
|
|
create_commit('a1', %w[a2 b2]),
|
|
|
|
create_commit('b1', %w[b2 a2]),
|
|
|
|
create_commit('a2', %w[a3 b3]),
|
|
|
|
create_commit('b2', %w[b3 a3]),
|
|
|
|
create_commit('a3', %w[c]),
|
|
|
|
create_commit('b3', %w[c]),
|
|
|
|
create_commit('c', [])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
|
|
|
end
|
2021-07-29 08:08:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_commit(id, parent_ids)
|
|
|
|
Gitlab::Git::Commit.new(project.repository, {
|
|
|
|
id: id,
|
|
|
|
parent_ids: parent_ids
|
|
|
|
})
|
2016-08-12 18:27:42 -04:00
|
|
|
end
|
|
|
|
end
|