2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Gitlab::Checks::ChangesAccess, feature_category: :source_code_management do
|
2021-10-27 15:23:28 +05:30
|
|
|
include_context 'changes access checks context'
|
|
|
|
|
|
|
|
subject { changes_access }
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
describe '#validate!' do
|
2021-11-11 11:23:49 +05:30
|
|
|
before do
|
|
|
|
allow(project).to receive(:lfs_enabled?).and_return(true)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'without failed checks' do
|
|
|
|
it "doesn't raise an error" do
|
|
|
|
expect { subject.validate! }.not_to raise_error
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it 'calls lfs checks' do
|
|
|
|
expect_next_instance_of(Gitlab::Checks::LfsCheck) do |instance|
|
|
|
|
expect(instance).to receive(:validate!)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
subject.validate!
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
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)
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect { access.validate! }.to raise_error(Gitlab::Checks::TimedLogger::TimeoutError)
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#commits' do
|
|
|
|
it 'calls #new_commits' do
|
|
|
|
expect(project.repository).to receive(:new_commits).and_call_original
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(subject.commits).to match_array([])
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'when change is for notes ref' do
|
|
|
|
let(:changes) do
|
|
|
|
[{ oldrev: oldrev, newrev: newrev, ref: 'refs/notes/commit' }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return any commits' do
|
|
|
|
expect(subject.commits).to match_array([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'when changes contain empty revisions' do
|
|
|
|
let(:expected_commit) { instance_double(Commit) }
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
shared_examples 'returns only commits with non empty revisions' do
|
|
|
|
specify do
|
2022-07-16 23:28:13 +05:30
|
|
|
expect(project.repository)
|
|
|
|
.to receive(:new_commits)
|
2022-07-23 23:45:48 +05:30
|
|
|
.with([newrev]) { [expected_commit] }
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(subject.commits).to match_array([expected_commit])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
context 'with oldrev' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:changes) { [{ oldrev: oldrev, newrev: newrev }, { newrev: '' }, { newrev: Gitlab::Git::BLANK_SHA }] }
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
it_behaves_like 'returns only commits with non empty revisions'
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'without oldrev' do
|
2022-07-16 23:28:13 +05:30
|
|
|
let(:changes) { [{ newrev: newrev }, { newrev: '' }, { newrev: Gitlab::Git::BLANK_SHA }] }
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it_behaves_like 'returns only commits with non empty revisions'
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#commits_for' do
|
|
|
|
let(:new_commits) { [] }
|
|
|
|
let(:expected_commits) { [] }
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:oldrev) { Gitlab::Git::BLANK_SHA }
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
shared_examples 'a listing of new commits' do
|
|
|
|
it 'returns expected commits' do
|
|
|
|
expect(subject).to receive(:commits).and_return(new_commits)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(subject.commits_for(oldrev, newrev)).to eq(expected_commits)
|
2021-10-27 15:23:28 +05:30
|
|
|
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 }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
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])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_commits) do
|
|
|
|
[create_commit(newrev, %w[1111 2222])]
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'with multiple related commits' do
|
|
|
|
let(:new_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111]),
|
|
|
|
create_commit('1111', %w[2222]),
|
|
|
|
create_commit('abcd', [])
|
|
|
|
]
|
|
|
|
end
|
2019-01-03 12:48:30 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
let(:expected_commits) do
|
|
|
|
[
|
|
|
|
create_commit(newrev, %w[1111]),
|
|
|
|
create_commit('1111', %w[2222])
|
|
|
|
]
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
context 'with over-push' do
|
|
|
|
let(:newrev) { '1' }
|
|
|
|
let(:oldrev) { '3' }
|
|
|
|
|
|
|
|
# `#new_commits` returns too many commits, where some commits are not
|
|
|
|
# part of the current change.
|
|
|
|
let(:new_commits) do
|
|
|
|
[
|
|
|
|
create_commit('1', %w[2]),
|
|
|
|
create_commit('2', %w[3]),
|
|
|
|
create_commit('3', %w[4]),
|
|
|
|
create_commit('4', %w[])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_commits) do
|
|
|
|
[
|
|
|
|
create_commit('1', %w[2]),
|
|
|
|
create_commit('2', %w[3])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a listing of new commits'
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
describe '#single_change_accesses' do
|
|
|
|
let(:commits_for) { {} }
|
|
|
|
let(:expected_accesses) { [] }
|
|
|
|
|
|
|
|
shared_examples '#single_change_access' do
|
|
|
|
before do
|
2022-03-02 08:16:31 +05:30
|
|
|
commits_for.each do |oldrev, newrev, commits|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(subject)
|
|
|
|
.to receive(:commits_for)
|
2022-03-02 08:16:31 +05:30
|
|
|
.with(oldrev, newrev)
|
2021-11-11 11:23:49 +05:30
|
|
|
.and_return(commits)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an array of SingleChangeAccess' do
|
|
|
|
# Commits are wrapped in a Gitlab::Lazy and thus need to be resolved
|
|
|
|
# first such that we can directly compare types.
|
|
|
|
actual_accesses = subject.single_change_accesses
|
|
|
|
.each { |access| access.instance_variable_set(:@commits, access.commits.to_a) }
|
|
|
|
|
|
|
|
expect(actual_accesses).to match_array(expected_accesses)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no changes' do
|
|
|
|
let(:changes) { [] }
|
|
|
|
|
|
|
|
it_behaves_like '#single_change_access'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single change and no new commits' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:commits_for) do
|
|
|
|
[
|
|
|
|
['old', 'new', []]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
let(:changes) do
|
|
|
|
[
|
|
|
|
{ oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_accesses) do
|
|
|
|
[
|
|
|
|
have_attributes(oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch', commits: [])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#single_change_access'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single change and new commits' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:commits_for) do
|
|
|
|
[
|
|
|
|
['old', 'new', [create_commit('new', [])]]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
let(:changes) do
|
|
|
|
[
|
|
|
|
{ oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_accesses) do
|
|
|
|
[
|
|
|
|
have_attributes(oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch', commits: [create_commit('new', [])])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#single_change_access'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple changes' do
|
|
|
|
let(:commits_for) do
|
2022-03-02 08:16:31 +05:30
|
|
|
[
|
|
|
|
[nil, 'a', [create_commit('a', [])]],
|
|
|
|
['a', 'c', [create_commit('c', [])]],
|
|
|
|
[nil, 'd', []]
|
|
|
|
]
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:changes) do
|
|
|
|
[
|
|
|
|
{ newrev: 'a', ref: 'refs/heads/a' },
|
|
|
|
{ oldrev: 'b', ref: 'refs/heads/b' },
|
|
|
|
{ oldrev: 'a', newrev: 'c', ref: 'refs/heads/c' },
|
|
|
|
{ newrev: 'd', ref: 'refs/heads/d' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_accesses) do
|
|
|
|
[
|
|
|
|
have_attributes(newrev: 'a', ref: 'refs/heads/a', commits: [create_commit('a', [])]),
|
|
|
|
have_attributes(oldrev: 'b', ref: 'refs/heads/b', commits: []),
|
|
|
|
have_attributes(oldrev: 'a', newrev: 'c', ref: 'refs/heads/c', commits: [create_commit('c', [])]),
|
|
|
|
have_attributes(newrev: 'd', ref: 'refs/heads/d', commits: [])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '#single_change_access'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def create_commit(id, parent_ids)
|
|
|
|
Gitlab::Git::Commit.new(project.repository, {
|
|
|
|
id: id,
|
|
|
|
parent_ids: parent_ids
|
|
|
|
})
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|