diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-28 21:09:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-28 21:09:07 +0300 |
commit | 1c8fa70f9d0818e2a82089c8643a6e455bca47fd (patch) | |
tree | f339f97de0425270bdd909e2f4d378927b6e0a18 /spec/services | |
parent | 736d36d8597d0d1ec1b47644e6d091c3f4a78f45 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
4 files changed, 93 insertions, 71 deletions
diff --git a/spec/services/deploy_tokens/create_service_spec.rb b/spec/services/deploy_tokens/create_service_spec.rb deleted file mode 100644 index fbb66fe4cb7..00000000000 --- a/spec/services/deploy_tokens/create_service_spec.rb +++ /dev/null @@ -1,63 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe DeployTokens::CreateService do - let(:project) { create(:project) } - let(:user) { create(:user) } - let(:deploy_token_params) { attributes_for(:deploy_token) } - - describe '#execute' do - subject { described_class.new(project, user, deploy_token_params).execute } - - context 'when the deploy token is valid' do - it 'creates a new DeployToken' do - expect { subject }.to change { DeployToken.count }.by(1) - end - - it 'creates a new ProjectDeployToken' do - expect { subject }.to change { ProjectDeployToken.count }.by(1) - end - - it 'returns a DeployToken' do - expect(subject).to be_an_instance_of DeployToken - end - end - - context 'when expires at date is not passed' do - let(:deploy_token_params) { attributes_for(:deploy_token, expires_at: '') } - - it 'sets Forever.date' do - expect(subject.read_attribute(:expires_at)).to eq(Forever.date) - end - end - - context 'when username is empty string' do - let(:deploy_token_params) { attributes_for(:deploy_token, username: '') } - - it 'converts it to nil' do - expect(subject.read_attribute(:username)).to be_nil - end - end - - context 'when username is provided' do - let(:deploy_token_params) { attributes_for(:deploy_token, username: 'deployer') } - - it 'keeps the provided username' do - expect(subject.read_attribute(:username)).to eq('deployer') - end - end - - context 'when the deploy token is invalid' do - let(:deploy_token_params) { attributes_for(:deploy_token, read_repository: false, read_registry: false) } - - it 'does not create a new DeployToken' do - expect { subject }.not_to change { DeployToken.count } - end - - it 'does not create a new ProjectDeployToken' do - expect { subject }.not_to change { ProjectDeployToken.count } - end - end - end -end diff --git a/spec/services/groups/deploy_tokens/create_service_spec.rb b/spec/services/groups/deploy_tokens/create_service_spec.rb new file mode 100644 index 00000000000..20c609bc828 --- /dev/null +++ b/spec/services/groups/deploy_tokens/create_service_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Groups::DeployTokens::CreateService do + it_behaves_like 'a deploy token creation service' do + let(:entity) { create(:group) } + let(:deploy_token_class) { GroupDeployToken } + end +end diff --git a/spec/services/projects/deploy_tokens/create_service_spec.rb b/spec/services/projects/deploy_tokens/create_service_spec.rb new file mode 100644 index 00000000000..5c3ada8af4e --- /dev/null +++ b/spec/services/projects/deploy_tokens/create_service_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Projects::DeployTokens::CreateService do + it_behaves_like 'a deploy token creation service' do + let(:entity) { create(:project) } + let(:deploy_token_class) { ProjectDeployToken } + end +end diff --git a/spec/services/snippets/create_service_spec.rb b/spec/services/snippets/create_service_spec.rb index 37b203c2341..5c853f8b7d7 100644 --- a/spec/services/snippets/create_service_spec.rb +++ b/spec/services/snippets/create_service_spec.rb @@ -143,37 +143,102 @@ describe Snippets::CreateService do end end - shared_examples 'creates repository' do - it do + shared_examples 'creates repository and files' do + it 'creates repository' do subject - expect(snippet.repository_exists?).to be_truthy + expect(snippet.repository.exists?).to be_truthy + end + + it 'commit the files to the repository' do + subject + + blob = snippet.repository.blob_at('master', base_opts[:file_name]) + + expect(blob.data).to eq base_opts[:content] + end + + context 'when repository creation action fails' do + before do + allow_next_instance_of(Snippet) do |instance| + allow(instance).to receive(:create_repository).and_return(nil) + end + end + + it 'does not create the snippet' do + expect { subject }.not_to change { Snippet.count } + end + + it 'returns the error' do + expect(snippet.errors.full_messages).to include('Repository could not be created') + end + end + + context 'when the commit action fails' do + before do + allow_next_instance_of(SnippetRepository) do |instance| + allow(instance).to receive(:multi_files_action).and_raise(SnippetRepository::CommitError.new('foobar')) + end + end + + it 'does not create the snippet' do + expect { subject }.not_to change { Snippet.count } + end + + it 'does not create the repository' do + expect(snippet.repository_exists?).to be_falsey + end + + it 'destroys the existing repository' do + expect(Repositories::DestroyService).to receive(:new).and_call_original + + subject + end + + it 'returns the error' do + response = subject + + expect(response).to be_error + expect(response.payload[:snippet].errors.full_messages).to eq ['foobar'] + end end context 'when snippet creation fails' do let(:extra_opts) { { content: nil } } it 'does not create repository' do - subject + expect do + subject + end.not_to change(Snippet, :count) expect(snippet.repository_exists?).to be_falsey end end context 'when feature flag :version_snippets is disabled' do - it 'does not create snippet repository' do + before do stub_feature_flags(version_snippets: false) + end + it 'does not create snippet repository' do expect do subject end.to change(Snippet, :count).by(1) expect(snippet.repository_exists?).to be_falsey end + + it 'does not try to commit files' do + expect_next_instance_of(described_class) do |instance| + expect(instance).not_to receive(:create_commit) + end + + subject + end end end - context 'when Project Snippet' do + context 'when ProjectSnippet' do let_it_be(:project) { create(:project) } before do @@ -185,7 +250,7 @@ describe Snippets::CreateService do it_behaves_like 'spam check is performed' it_behaves_like 'snippet create data is tracked' it_behaves_like 'an error service response when save fails' - it_behaves_like 'creates repository' + it_behaves_like 'creates repository and files' end context 'when PersonalSnippet' do @@ -196,7 +261,7 @@ describe Snippets::CreateService do it_behaves_like 'spam check is performed' it_behaves_like 'snippet create data is tracked' it_behaves_like 'an error service response when save fails' - it_behaves_like 'creates repository' + it_behaves_like 'creates repository and files' end end end |