diff options
Diffstat (limited to 'spec/services/snippets/update_service_spec.rb')
-rw-r--r-- | spec/services/snippets/update_service_spec.rb | 90 |
1 files changed, 67 insertions, 23 deletions
diff --git a/spec/services/snippets/update_service_spec.rb b/spec/services/snippets/update_service_spec.rb index 05fb725c065..38747ae907f 100644 --- a/spec/services/snippets/update_service_spec.rb +++ b/spec/services/snippets/update_service_spec.rb @@ -7,7 +7,7 @@ describe Snippets::UpdateService do let_it_be(:user) { create(:user) } let_it_be(:admin) { create :user, admin: true } let(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE } - let(:options) do + let(:base_opts) do { title: 'Test snippet', file_name: 'snippet.rb', @@ -15,6 +15,8 @@ describe Snippets::UpdateService do visibility_level: visibility_level } end + let(:extra_opts) { {} } + let(:options) { base_opts.merge(extra_opts) } let(:updater) { user } let(:service) { Snippets::UpdateService.new(project, updater, options) } @@ -85,7 +87,7 @@ describe Snippets::UpdateService do end context 'when update fails' do - let(:options) { { title: '' } } + let(:extra_opts) { { title: '' } } it 'does not increment count' do expect { subject }.not_to change { counter.read(:update) } @@ -112,25 +114,16 @@ describe Snippets::UpdateService do expect(blob.data).to eq options[:content] end - context 'when the repository does not exist' do - it 'does not try to commit file' do - allow(snippet).to receive(:repository_exists?).and_return(false) - - expect(service).not_to receive(:create_commit) - - subject - end - end - - context 'when feature flag is disabled' do + context 'when the repository creation fails' do before do - stub_feature_flags(version_snippets: false) + allow(snippet).to receive(:repository_exists?).and_return(false) end - it 'does not create repository' do - subject + it 'raise an error' do + response = subject - expect(snippet.repository).not_to exist + expect(response).to be_error + expect(response.payload[:snippet].errors[:repository].to_sentence).to eq 'Error updating the snippet - Repository could not be created' end it 'does not try to commit file' do @@ -205,14 +198,24 @@ describe Snippets::UpdateService do end end - it 'rolls back any snippet modifications' do - option_keys = options.stringify_keys.keys - orig_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) } + context 'with snippet modifications' do + let(:option_keys) { options.stringify_keys.keys } - subject + it 'rolls back any snippet modifications' do + orig_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) } + + subject + + persisted_attrs = snippet.reload.attributes.select { |k, v| k.in?(option_keys) } + expect(orig_attrs).to eq persisted_attrs + end + + it 'keeps any snippet modifications' do + subject - current_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) } - expect(orig_attrs).to eq current_attrs + instance_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) } + expect(options.stringify_keys).to eq instance_attrs + end end end @@ -270,6 +273,35 @@ describe Snippets::UpdateService do end end + shared_examples 'committable attributes' do + context 'when file_name is updated' do + let(:extra_opts) { { file_name: 'snippet.rb' } } + + it 'commits to repository' do + expect(service).to receive(:create_commit) + expect(subject).to be_success + end + end + + context 'when content is updated' do + let(:extra_opts) { { content: 'puts "hello world"' } } + + it 'commits to repository' do + expect(service).to receive(:create_commit) + expect(subject).to be_success + end + end + + context 'when content or file_name is not updated' do + let(:options) { { title: 'Test snippet' } } + + it 'does not perform any commit' do + expect(service).not_to receive(:create_commit) + expect(subject).to be_success + end + end + end + context 'when Project Snippet' do let_it_be(:project) { create(:project) } let!(:snippet) { create(:project_snippet, :repository, author: user, project: project) } @@ -283,6 +315,12 @@ describe Snippets::UpdateService do it_behaves_like 'snippet update data is tracked' it_behaves_like 'updates repository content' it_behaves_like 'commit operation fails' + it_behaves_like 'committable attributes' + it_behaves_like 'snippets spam check is performed' do + before do + subject + end + end context 'when snippet does not have a repository' do let!(:snippet) { create(:project_snippet, author: user, project: project) } @@ -301,6 +339,12 @@ describe Snippets::UpdateService do it_behaves_like 'snippet update data is tracked' it_behaves_like 'updates repository content' it_behaves_like 'commit operation fails' + it_behaves_like 'committable attributes' + it_behaves_like 'snippets spam check is performed' do + before do + subject + end + end context 'when snippet does not have a repository' do let!(:snippet) { create(:personal_snippet, author: user, project: project) } |