2020-02-27 15:09:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 09:08:32 +00:00
|
|
|
RSpec.describe Gitlab::Checks::SnippetCheck do
|
2020-02-27 15:09:24 +00:00
|
|
|
include_context 'change access checks context'
|
|
|
|
|
2020-09-16 12:10:15 +00:00
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :repository) }
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
let(:user_access) { Gitlab::UserAccessSnippet.new(user, snippet: snippet) }
|
2020-09-16 12:10:15 +00:00
|
|
|
let(:default_branch) { snippet.default_branch }
|
2020-12-01 15:09:28 +00:00
|
|
|
let(:branch_name) { default_branch }
|
|
|
|
let(:creation) { false }
|
|
|
|
let(:deletion) { false }
|
2020-02-27 15:09:24 +00:00
|
|
|
|
2020-12-03 15:09:46 +00:00
|
|
|
subject { Gitlab::Checks::SnippetCheck.new(changes, default_branch: default_branch, root_ref: snippet.repository.root_ref, logger: logger) }
|
2020-02-27 15:09:24 +00:00
|
|
|
|
2020-03-17 06:09:21 +00:00
|
|
|
describe '#validate!' do
|
2020-02-27 15:09:24 +00:00
|
|
|
it 'does not raise any error' do
|
2020-03-17 06:09:21 +00:00
|
|
|
expect { subject.validate! }.not_to raise_error
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
|
2020-12-01 15:09:28 +00:00
|
|
|
shared_examples 'raises and logs error' do
|
|
|
|
specify do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(instance_of(Gitlab::GitAccess::ForbiddenError), default_branch: default_branch, branch_name: branch_name, creation: creation, deletion: deletion)
|
|
|
|
|
|
|
|
expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, 'You can not create or delete branches.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-27 15:09:24 +00:00
|
|
|
context 'trying to delete the branch' do
|
|
|
|
let(:newrev) { '0000000000000000000000000000000000000000' }
|
|
|
|
|
2020-12-01 15:09:28 +00:00
|
|
|
it_behaves_like 'raises and logs error' do
|
|
|
|
let(:deletion) { true }
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'trying to create the branch' do
|
|
|
|
let(:oldrev) { '0000000000000000000000000000000000000000' }
|
2020-03-03 15:08:08 +00:00
|
|
|
let(:ref) { 'refs/heads/feature' }
|
2020-02-27 15:09:24 +00:00
|
|
|
|
2020-12-01 15:09:28 +00:00
|
|
|
it_behaves_like 'raises and logs error' do
|
|
|
|
let(:creation) { true }
|
|
|
|
let(:branch_name) { 'feature' }
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
2020-03-03 15:08:08 +00:00
|
|
|
|
2020-12-03 15:09:46 +00:00
|
|
|
context 'when branch is the same as the default branch' do
|
|
|
|
let(:ref) { "refs/heads/#{default_branch}" }
|
2020-03-03 15:08:08 +00:00
|
|
|
|
2020-12-03 15:09:46 +00:00
|
|
|
it 'allows the operation' do
|
|
|
|
expect { subject.validate! }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet has an empty repo' do
|
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :empty_repo) }
|
|
|
|
|
|
|
|
it 'allows the operation' do
|
2020-03-17 06:09:21 +00:00
|
|
|
expect { subject.validate! }.not_to raise_error
|
2020-03-03 15:08:08 +00:00
|
|
|
end
|
|
|
|
end
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
2020-09-16 12:10:15 +00:00
|
|
|
|
|
|
|
context 'when default_branch is nil' do
|
|
|
|
let(:default_branch) { nil }
|
|
|
|
|
2020-12-01 15:09:28 +00:00
|
|
|
it_behaves_like 'raises and logs error' do
|
|
|
|
let(:branch_name) { 'master' }
|
2020-09-16 12:10:15 +00:00
|
|
|
end
|
|
|
|
end
|
2020-02-27 15:09:24 +00:00
|
|
|
end
|
|
|
|
end
|