diff options
Diffstat (limited to 'spec/services/snippets/update_service_spec.rb')
-rw-r--r-- | spec/services/snippets/update_service_spec.rb | 256 |
1 files changed, 244 insertions, 12 deletions
diff --git a/spec/services/snippets/update_service_spec.rb b/spec/services/snippets/update_service_spec.rb index 7e6441ad2f9..66dddcc49de 100644 --- a/spec/services/snippets/update_service_spec.rb +++ b/spec/services/snippets/update_service_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Snippets::UpdateService do +RSpec.describe Snippets::UpdateService do describe '#execute' do let_it_be(:user) { create(:user) } let_it_be(:admin) { create :user, admin: true } @@ -302,22 +302,22 @@ describe Snippets::UpdateService do end end - shared_examples 'when snippet_files param is present' do + shared_examples 'when snippet_actions 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(:snippet_actions) { [{ action: 'update', previous_path: file_path, file_path: file_path, content: content }] } let(:base_opts) do { title: new_title, - snippet_files: snippet_files + snippet_actions: snippet_actions } 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 + snippet_actions[0][:action] = 'move' + snippet_actions[0][:file_path] = file_path response = subject snippet = response.payload[:snippet] @@ -328,7 +328,7 @@ describe Snippets::UpdateService do expect(snippet.content).to eq(content) end - it 'commit the files to the repository' do + it 'commits the files to the repository' do subject blob = snippet.repository.blob_at('master', file_path) @@ -349,15 +349,27 @@ describe Snippets::UpdateService do end end - context 'when snippet_files param is invalid' do - let(:snippet_files) { [{ action: 'invalid_action' }] } + context 'when snippet_file content is not present' do + let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: 'new_file_path' }] } + + it 'does not update snippet content' do + content = snippet.content + + expect(subject).to be_success + + expect(snippet.reload.content).to eq content + end + end + + context 'when snippet_actions param is invalid' do + let(:snippet_actions) { [{ 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'] + expect(snippet.errors.full_messages_for(:snippet_actions)).to eq ['Snippet actions have invalid data'] end end @@ -376,6 +388,226 @@ describe Snippets::UpdateService do expect(snippet.content).to eq(content) end end + + context 'commit actions' do + let(:new_path) { 'created_new_file' } + let(:base_opts) { { snippet_actions: snippet_actions } } + + shared_examples 'returns an error' do |error_msg| + specify do + response = subject + + expect(response).to be_error + expect(response.message).to eq error_msg + end + end + + context 'update action' do + let(:snippet_actions) { [{ action: :update, file_path: file_path, content: content }] } + + it 'updates the file content' do + expect(subject).to be_success + + blob = blob(file_path) + + expect(blob.data).to eq content + end + + context 'when previous_path is present' do + let(:snippet_actions) { [{ action: :update, previous_path: file_path, file_path: file_path, content: content }] } + + it 'updates the file content' do + expect(subject).to be_success + + blob = blob(file_path) + + expect(blob.data).to eq content + end + end + + context 'when content is not present' do + let(:snippet_actions) { [{ action: :update, file_path: file_path }] } + + it_behaves_like 'returns an error', 'Snippet actions have invalid data' + end + + context 'when file_path does not exist' do + let(:snippet_actions) { [{ action: :update, file_path: 'makeup_name', content: content }] } + + it_behaves_like 'returns an error', 'Repository Error updating the snippet' + end + end + + context 'move action' do + context 'when file_path and previous_path are the same' do + let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: file_path }] } + + it_behaves_like 'returns an error', 'Snippet actions have invalid data' + end + + context 'when file_path and previous_path are different' do + let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: new_path }] } + + it 'renames the file' do + old_blob = blob(file_path) + + expect(subject).to be_success + + blob = blob(new_path) + + expect(blob).to be_present + expect(blob.data).to eq old_blob.data + end + end + + context 'when previous_path does not exist' do + let(:snippet_actions) { [{ action: :move, previous_path: 'makeup_name', file_path: new_path }] } + + it_behaves_like 'returns an error', 'Repository Error updating the snippet' + end + + context 'when user wants to rename the file and update content' do + let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: new_path, content: content }] } + + it 'performs both operations' do + expect(subject).to be_success + + blob = blob(new_path) + + expect(blob).to be_present + expect(blob.data).to eq content + end + end + end + + context 'delete action' do + let(:snippet_actions) { [{ action: :delete, file_path: file_path }] } + + shared_examples 'deletes the file' do + specify do + old_blob = blob(file_path) + expect(old_blob).to be_present + + expect(subject).to be_success + expect(blob(file_path)).to be_nil + end + end + + it_behaves_like 'deletes the file' + + context 'when previous_path is present and same as file_path' do + let(:snippet_actions) { [{ action: :delete, previous_path: file_path, file_path: file_path }] } + + it_behaves_like 'deletes the file' + end + + context 'when previous_path is present and is different from file_path' do + let(:snippet_actions) { [{ action: :delete, previous_path: 'foo', file_path: file_path }] } + + it_behaves_like 'deletes the file' + end + + context 'when content is present' do + let(:snippet_actions) { [{ action: :delete, file_path: file_path, content: 'foo' }] } + + it_behaves_like 'deletes the file' + end + + context 'when file_path does not exist' do + let(:snippet_actions) { [{ action: :delete, file_path: 'makeup_name' }] } + + it_behaves_like 'returns an error', 'Repository Error updating the snippet' + end + end + + context 'create action' do + let(:snippet_actions) { [{ action: :create, file_path: new_path, content: content }] } + + it 'creates the file' do + expect(subject).to be_success + + blob = blob(new_path) + expect(blob).to be_present + expect(blob.data).to eq content + end + + context 'when content is not present' do + let(:snippet_actions) { [{ action: :create, file_path: new_path }] } + + it_behaves_like 'returns an error', 'Snippet actions have invalid data' + end + + context 'when file_path is not present or empty' do + let(:snippet_actions) { [{ action: :create, content: content }, { action: :create, file_path: '', content: content }] } + + it 'generates the file path for the files' do + expect(blob('snippetfile1.txt')).to be_nil + expect(blob('snippetfile2.txt')).to be_nil + + expect(subject).to be_success + + expect(blob('snippetfile1.txt').data).to eq content + expect(blob('snippetfile2.txt').data).to eq content + end + end + + context 'when file_path already exists in the repository' do + let(:snippet_actions) { [{ action: :create, file_path: file_path, content: content }] } + + it_behaves_like 'returns an error', 'Repository Error updating the snippet' + end + + context 'when previous_path is present' do + let(:snippet_actions) { [{ action: :create, previous_path: 'foo', file_path: new_path, content: content }] } + + it 'creates the file' do + expect(subject).to be_success + + blob = blob(new_path) + expect(blob).to be_present + expect(blob.data).to eq content + end + end + end + + context 'combination of actions' do + let(:delete_file_path) { 'CHANGELOG' } + let(:create_file_path) { 'created_new_file' } + let(:update_file_path) { 'LICENSE' } + let(:move_previous_path) { 'VERSION' } + let(:move_file_path) { 'VERSION_new' } + + let(:snippet_actions) do + [ + { action: :create, file_path: create_file_path, content: content }, + { action: :update, file_path: update_file_path, content: content }, + { action: :delete, file_path: delete_file_path }, + { action: :move, previous_path: move_previous_path, file_path: move_file_path, content: content } + ] + end + + it 'performs all operations' do + expect(subject).to be_success + + expect(blob(delete_file_path)).to be_nil + + created_blob = blob(create_file_path) + expect(created_blob.data).to eq content + + updated_blob = blob(update_file_path) + expect(updated_blob.data).to eq content + + expect(blob(move_previous_path)).to be_nil + + moved_blob = blob(move_file_path) + expect(moved_blob.data).to eq content + end + end + + def blob(path) + snippet.repository.blob_at('master', path) + end + end end shared_examples 'only file_name is present' do @@ -446,7 +678,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 'when snippet_actions 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 @@ -473,7 +705,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 'when snippet_actions 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 |