diff options
Diffstat (limited to 'spec/services/snippets')
-rw-r--r-- | spec/services/snippets/bulk_destroy_service_spec.rb | 12 | ||||
-rw-r--r-- | spec/services/snippets/create_service_spec.rb | 59 | ||||
-rw-r--r-- | spec/services/snippets/update_service_spec.rb | 78 |
3 files changed, 148 insertions, 1 deletions
diff --git a/spec/services/snippets/bulk_destroy_service_spec.rb b/spec/services/snippets/bulk_destroy_service_spec.rb index f03d7496f94..6e5623e575f 100644 --- a/spec/services/snippets/bulk_destroy_service_spec.rb +++ b/spec/services/snippets/bulk_destroy_service_spec.rb @@ -69,6 +69,18 @@ describe Snippets::BulkDestroyService do it_behaves_like 'error is raised' do let(:error_message) { "You don't have access to delete these snippets." } end + + context 'when hard_delete option is passed' do + subject { described_class.new(service_user, snippets).execute(hard_delete: true) } + + it 'returns a ServiceResponse success response' do + expect(subject).to be_success + end + + it 'deletes all the snippets that belong to the user' do + expect { subject }.to change(Snippet, :count).by(-2) + end + end end context 'when an error is raised deleting the repository' do diff --git a/spec/services/snippets/create_service_spec.rb b/spec/services/snippets/create_service_spec.rb index 786fc3ec8dd..fa8cbc87563 100644 --- a/spec/services/snippets/create_service_spec.rb +++ b/spec/services/snippets/create_service_spec.rb @@ -109,7 +109,7 @@ describe Snippets::CreateService do expect(snippet.repository.exists?).to be_truthy end - it 'commit the files to the repository' do + it 'commits the files to the repository' do subject blob = snippet.repository.blob_at('master', base_opts[:file_name]) @@ -230,6 +230,61 @@ describe Snippets::CreateService do end end + shared_examples 'when snippet_files param is present' do + let(:file_path) { 'snippet_file_path.rb' } + let(:content) { 'snippet_content' } + let(:snippet_files) { [{ action: 'create', file_path: file_path, content: content }] } + let(:base_opts) do + { + title: 'Test snippet', + visibility_level: Gitlab::VisibilityLevel::PRIVATE, + snippet_files: snippet_files + } + end + + it 'creates a snippet with the provided attributes' do + expect(snippet.title).to eq(opts[:title]) + expect(snippet.visibility_level).to eq(opts[:visibility_level]) + expect(snippet.file_name).to eq(file_path) + expect(snippet.content).to eq(content) + end + + it 'commit the files to the repository' do + subject + + blob = snippet.repository.blob_at('master', file_path) + + expect(blob.data).to eq content + end + + context 'when content or file_name params are present' do + let(:extra_opts) { { content: 'foo', file_name: 'path' } } + + it 'a validation error is raised' do + response = subject + snippet = response.payload[:snippet] + + expect(response).to be_error + expect(snippet.errors.full_messages_for(:content)).to eq ['Content and snippet files cannot be used together'] + expect(snippet.errors.full_messages_for(:file_name)).to eq ['File name and snippet files cannot be used together'] + expect(snippet.repository.exists?).to be_falsey + end + end + + context 'when snippet_files param is invalid' do + let(:snippet_files) { [{ action: 'invalid_action', file_path: 'snippet_file_path.rb', content: 'snippet_content' }] } + + it 'a validation error is raised' do + response = subject + snippet = response.payload[:snippet] + + expect(response).to be_error + expect(snippet.errors.full_messages_for(:snippet_files)).to eq ['Snippet files have invalid data'] + expect(snippet.repository.exists?).to be_falsey + end + end + end + context 'when ProjectSnippet' do let_it_be(:project) { create(:project) } @@ -244,6 +299,7 @@ describe Snippets::CreateService do it_behaves_like 'an error service response when save fails' it_behaves_like 'creates repository and files' it_behaves_like 'after_save callback to store_mentions', ProjectSnippet + it_behaves_like 'when snippet_files param is present' context 'when uploaded files are passed to the service' do let(:extra_opts) { { files: ['foo'] } } @@ -270,6 +326,7 @@ describe Snippets::CreateService do it_behaves_like 'an error service response when save fails' it_behaves_like 'creates repository and files' it_behaves_like 'after_save callback to store_mentions', PersonalSnippet + it_behaves_like 'when snippet_files param is present' context 'when the snippet description contains files' do include FileMoverHelpers diff --git a/spec/services/snippets/update_service_spec.rb b/spec/services/snippets/update_service_spec.rb index 6c3ae52befc..7e6441ad2f9 100644 --- a/spec/services/snippets/update_service_spec.rb +++ b/spec/services/snippets/update_service_spec.rb @@ -302,6 +302,82 @@ describe Snippets::UpdateService do end end + shared_examples 'when snippet_files param is present' do + let(:file_path) { 'CHANGELOG' } + let(:content) { 'snippet_content' } + let(:new_title) { 'New title' } + let(:snippet_files) { [{ action: 'update', previous_path: file_path, file_path: file_path, content: content }] } + let(:base_opts) do + { + title: new_title, + snippet_files: snippet_files + } + end + + it 'updates a snippet with the provided attributes' do + file_path = 'foo' + snippet_files[0][:action] = 'move' + snippet_files[0][:file_path] = file_path + + response = subject + snippet = response.payload[:snippet] + + expect(response).to be_success + expect(snippet.title).to eq(new_title) + expect(snippet.file_name).to eq(file_path) + expect(snippet.content).to eq(content) + end + + it 'commit the files to the repository' do + subject + + blob = snippet.repository.blob_at('master', file_path) + + expect(blob.data).to eq content + end + + context 'when content or file_name params are present' do + let(:extra_opts) { { content: 'foo', file_name: 'path' } } + + it 'raises a validation error' do + response = subject + snippet = response.payload[:snippet] + + expect(response).to be_error + expect(snippet.errors.full_messages_for(:content)).to eq ['Content and snippet files cannot be used together'] + expect(snippet.errors.full_messages_for(:file_name)).to eq ['File name and snippet files cannot be used together'] + end + end + + context 'when snippet_files param is invalid' do + let(:snippet_files) { [{ action: 'invalid_action' }] } + + it 'raises a validation error' do + response = subject + snippet = response.payload[:snippet] + + expect(response).to be_error + expect(snippet.errors.full_messages_for(:snippet_files)).to eq ['Snippet files have invalid data'] + end + end + + context 'when an error is raised committing the file' do + it 'keeps any snippet modifications' do + expect_next_instance_of(described_class) do |instance| + expect(instance).to receive(:create_repository_for).and_raise(StandardError) + end + + response = subject + snippet = response.payload[:snippet] + + expect(response).to be_error + expect(snippet.title).to eq(new_title) + expect(snippet.file_name).to eq(file_path) + expect(snippet.content).to eq(content) + end + end + end + shared_examples 'only file_name is present' do let(:base_opts) do { @@ -370,6 +446,7 @@ describe Snippets::UpdateService do it_behaves_like 'updates repository content' it_behaves_like 'commit operation fails' it_behaves_like 'committable attributes' + it_behaves_like 'when snippet_files param is present' it_behaves_like 'only file_name is present' it_behaves_like 'only content is present' it_behaves_like 'snippets spam check is performed' do @@ -396,6 +473,7 @@ describe Snippets::UpdateService do it_behaves_like 'updates repository content' it_behaves_like 'commit operation fails' it_behaves_like 'committable attributes' + it_behaves_like 'when snippet_files param is present' it_behaves_like 'only file_name is present' it_behaves_like 'only content is present' it_behaves_like 'snippets spam check is performed' do |