debian-mirror-gitlab/spec/services/releases/update_service_spec.rb

164 lines
5 KiB
Ruby
Raw Permalink Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2019-02-15 15:39:39 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Releases::UpdateService, feature_category: :continuous_integration do
2019-02-15 15:39:39 +05:30
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:new_name) { 'A new name' }
let(:new_description) { 'The best release!' }
let(:params) { { name: new_name, description: new_description, tag: tag_name } }
let(:service) { described_class.new(project, user, params) }
let!(:release) { create(:release, project: project, author: user, tag: tag_name) }
let(:tag_name) { 'v1.1.0' }
before do
project.add_developer(user)
end
describe '#execute' do
shared_examples 'a failed update' do
it 'raises an error' do
result = service.execute
expect(result[:status]).to eq(:error)
2020-03-13 15:44:24 +05:30
expect(result[:milestones_updated]).to be_falsy
2019-02-15 15:39:39 +05:30
end
end
it 'successfully updates an existing release' do
result = service.execute
expect(result[:status]).to eq(:success)
expect(result[:release].name).to eq(new_name)
expect(result[:release].description).to eq(new_description)
end
2021-01-29 00:20:46 +05:30
it 'executes hooks' do
expect(service.release).to receive(:execute_hooks).with('update')
service.execute
end
2019-02-15 15:39:39 +05:30
context 'when the tag does not exists' do
let(:tag_name) { 'foobar' }
it_behaves_like 'a failed update'
end
context 'when the release does not exist' do
2022-08-27 11:52:29 +05:30
let!(:release) {}
2019-02-15 15:39:39 +05:30
it_behaves_like 'a failed update'
end
2019-12-04 20:38:33 +05:30
context 'when a milestone is passed in' do
let(:milestone) { create(:milestone, project: project, title: 'v1.0') }
let(:params_with_milestone) { params.merge!({ milestones: [new_title] }) }
2020-03-13 15:44:24 +05:30
let(:new_milestone) { create(:milestone, project: project, title: new_title) }
2019-12-04 20:38:33 +05:30
let(:service) { described_class.new(new_milestone.project, user, params_with_milestone) }
before do
release.milestones << milestone
2020-03-13 15:44:24 +05:30
end
2019-12-04 20:38:33 +05:30
2023-04-23 21:23:45 +05:30
shared_examples 'updates milestones' do
2020-03-13 15:44:24 +05:30
it 'updates the related milestone accordingly' do
release.reload
2023-04-23 21:23:45 +05:30
result = service.execute
2020-03-13 15:44:24 +05:30
expect(release.milestones.first.title).to eq(new_title)
expect(result[:milestones_updated]).to be_truthy
end
2019-12-04 20:38:33 +05:30
end
2023-04-23 21:23:45 +05:30
context 'a different milestone' do
let(:new_title) { 'v2.0' }
it_behaves_like 'updates milestones'
end
2020-03-13 15:44:24 +05:30
context 'an identical milestone' do
let(:new_title) { 'v1.0' }
it "raises an error" do
expect { service.execute }.to raise_error(ActiveRecord::RecordInvalid)
end
2019-12-04 20:38:33 +05:30
end
2023-04-23 21:23:45 +05:30
context 'by ids' do
let(:new_title) { 'v2.0' }
let(:params_with_milestone) { params.merge!({ milestone_ids: [new_milestone.id] }) }
it_behaves_like 'updates milestones'
end
2019-12-04 20:38:33 +05:30
end
context "when an 'empty' milestone is passed in" do
let(:milestone) { create(:milestone, project: project, title: 'v1.0') }
before do
release.milestones << milestone
service.params = params_with_empty_milestone
end
2023-04-23 21:23:45 +05:30
shared_examples 'removes milestones' do
it 'removes the old milestone and does not associate any new milestone' do
result = service.execute
release.reload
expect(release.milestones).not_to be_present
expect(result[:milestones_updated]).to be_truthy
end
end
2020-03-13 15:44:24 +05:30
2023-04-23 21:23:45 +05:30
context 'by title' do
let(:params_with_empty_milestone) { params.merge!({ milestones: [] }) }
it_behaves_like 'removes milestones'
end
context 'by id' do
let(:params_with_empty_milestone) { params.merge!({ milestone_ids: [] }) }
it_behaves_like 'removes milestones'
2019-12-04 20:38:33 +05:30
end
end
context "when multiple new milestones are passed in" do
let(:new_title_1) { 'v2.0' }
let(:new_title_2) { 'v2.0-rc' }
let(:milestone) { create(:milestone, project: project, title: 'v1.0') }
let(:service) { described_class.new(project, user, params_with_milestones) }
2023-04-23 21:23:45 +05:30
let!(:new_milestone_1) { create(:milestone, project: project, title: new_title_1) }
let!(:new_milestone_2) { create(:milestone, project: project, title: new_title_2) }
2019-12-04 20:38:33 +05:30
before do
release.milestones << milestone
end
2023-04-23 21:23:45 +05:30
shared_examples 'updates multiple milestones' do
it 'removes the old milestone and update the release with the new ones' do
result = service.execute
release.reload
milestone_titles = release.milestones.map(&:title)
expect(milestone_titles).to match_array([new_title_1, new_title_2])
expect(result[:milestones_updated]).to be_truthy
end
end
context 'by title' do
let(:params_with_milestones) { params.merge!({ milestones: [new_title_1, new_title_2] }) }
it_behaves_like 'updates multiple milestones'
end
context 'by id' do
let(:params_with_milestones) { params.merge!({ milestone_ids: [new_milestone_1.id, new_milestone_2.id] }) }
2020-03-13 15:44:24 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'updates multiple milestones'
2019-12-04 20:38:33 +05:30
end
end
2019-02-15 15:39:39 +05:30
end
end