debian-mirror-gitlab/spec/lib/gitlab/checks/snippet_check_spec.rb

74 lines
2.3 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Checks::SnippetCheck do
2020-04-08 14:13:33 +05:30
include_context 'change access checks context'
2020-11-24 15:15:51 +05:30
let_it_be(:snippet) { create(:personal_snippet, :repository) }
2020-04-08 14:13:33 +05:30
let(:user_access) { Gitlab::UserAccessSnippet.new(user, snippet: snippet) }
2020-11-24 15:15:51 +05:30
let(:default_branch) { snippet.default_branch }
2021-02-22 17:27:13 +05:30
let(:branch_name) { default_branch }
let(:creation) { false }
let(:deletion) { false }
2020-04-08 14:13:33 +05:30
2021-02-22 17:27:13 +05:30
subject { Gitlab::Checks::SnippetCheck.new(changes, default_branch: default_branch, root_ref: snippet.repository.root_ref, logger: logger) }
2020-04-08 14:13:33 +05:30
describe '#validate!' do
it 'does not raise any error' do
expect { subject.validate! }.not_to raise_error
end
2021-02-22 17:27:13 +05:30
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-04-08 14:13:33 +05:30
context 'trying to delete the branch' do
let(:newrev) { '0000000000000000000000000000000000000000' }
2021-02-22 17:27:13 +05:30
it_behaves_like 'raises and logs error' do
let(:deletion) { true }
2020-04-08 14:13:33 +05:30
end
end
context 'trying to create the branch' do
let(:oldrev) { '0000000000000000000000000000000000000000' }
let(:ref) { 'refs/heads/feature' }
2021-02-22 17:27:13 +05:30
it_behaves_like 'raises and logs error' do
let(:creation) { true }
let(:branch_name) { 'feature' }
2020-04-08 14:13:33 +05:30
end
2021-02-22 17:27:13 +05:30
context 'when branch is the same as the default branch' do
let(:ref) { "refs/heads/#{default_branch}" }
2020-04-08 14:13:33 +05:30
2021-02-22 17:27:13 +05:30
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-04-08 14:13:33 +05:30
expect { subject.validate! }.not_to raise_error
end
end
end
2020-11-24 15:15:51 +05:30
context 'when default_branch is nil' do
let(:default_branch) { nil }
2021-02-22 17:27:13 +05:30
it_behaves_like 'raises and logs error' do
let(:branch_name) { 'master' }
2020-11-24 15:15:51 +05:30
end
end
2020-04-08 14:13:33 +05:30
end
end