diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-11 12:10:29 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-11 12:10:29 +0300 |
commit | 871b886a1794e5baefd6b2f96caf2ac4ce5da6ca (patch) | |
tree | a92b04af5c5704314c31981ac4bb92c1489c46fd /spec/services | |
parent | c0496e1078f8612b0c468430a79bd03c8102e2b9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/milestones/create_service_spec.rb | 68 | ||||
-rw-r--r-- | spec/services/milestones/update_service_spec.rb | 90 |
2 files changed, 125 insertions, 33 deletions
diff --git a/spec/services/milestones/create_service_spec.rb b/spec/services/milestones/create_service_spec.rb index 78cb05532eb..70010d88fbd 100644 --- a/spec/services/milestones/create_service_spec.rb +++ b/spec/services/milestones/create_service_spec.rb @@ -3,24 +3,70 @@ require 'spec_helper' RSpec.describe Milestones::CreateService, feature_category: :team_planning do - let(:project) { create(:project) } - let(:user) { create(:user) } + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:params) { { title: 'New Milestone', description: 'Description' } } + + subject(:create_milestone) { described_class.new(project, user, params) } describe '#execute' do - context "valid params" do + context 'when milestone is saved successfully' do + it 'creates a new milestone' do + expect { create_milestone.execute }.to change { Milestone.count }.by(1) + end + + it 'opens the milestone if it is a project milestone' do + expect_next_instance_of(EventCreateService) do |instance| + expect(instance).to receive(:open_milestone) + end + + create_milestone.execute + end + + it 'returns the created milestone' do + milestone = create_milestone.execute + expect(milestone).to be_a(Milestone) + expect(milestone.title).to eq('New Milestone') + expect(milestone.description).to eq('Description') + end + end + + context 'when milestone fails to save' do before do - project.add_maintainer(user) + allow_next_instance_of(Milestone) do |instance| + allow(instance).to receive(:save).and_return(false) + end + end + + it 'does not create a new milestone' do + expect { create_milestone.execute }.not_to change { Milestone.count } + end - opts = { - title: 'v2.1.9', - description: 'Patch release to fix security issue' - } + it 'does not open the milestone' do + expect(EventCreateService).not_to receive(:open_milestone) + + create_milestone.execute + end - @milestone = described_class.new(project, user, opts).execute + it 'returns the unsaved milestone' do + milestone = create_milestone.execute + expect(milestone).to be_a(Milestone) + expect(milestone.title).to eq('New Milestone') + expect(milestone.persisted?).to be_falsey end + end + + it 'calls before_create method' do + expect(create_milestone).to receive(:before_create) + create_milestone.execute + end + end - it { expect(@milestone).to be_valid } - it { expect(@milestone.title).to eq('v2.1.9') } + describe '#before_create' do + it 'checks for spam' do + milestone = build(:milestone) + expect(milestone).to receive(:check_for_spam).with(user: user, action: :create) + subject.send(:before_create, milestone) end end end diff --git a/spec/services/milestones/update_service_spec.rb b/spec/services/milestones/update_service_spec.rb index 76110af2514..44de49960d4 100644 --- a/spec/services/milestones/update_service_spec.rb +++ b/spec/services/milestones/update_service_spec.rb @@ -2,40 +2,86 @@ require 'spec_helper' RSpec.describe Milestones::UpdateService, feature_category: :team_planning do - let(:project) { create(:project) } - let(:user) { build(:user) } - let(:milestone) { create(:milestone, project: project) } + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:milestone) { create(:milestone, project: project) } + let_it_be(:params) { { title: 'New Title' } } + + subject(:update_milestone) { described_class.new(project, user, params) } describe '#execute' do - context "valid params" do - let(:inner_service) { double(:service) } + context 'when state_event is "activate"' do + let(:params) { { state_event: 'activate' } } - before do - project.add_maintainer(user) + it 'calls Milestones::ReopenService' do + reopen_service = instance_double(Milestones::ReopenService) + expect(Milestones::ReopenService).to receive(:new).with(project, user, {}).and_return(reopen_service) + expect(reopen_service).to receive(:execute).with(milestone) + + update_milestone.execute(milestone) end + end - subject { described_class.new(project, user, { title: 'new_title' }).execute(milestone) } + context 'when state_event is "close"' do + let(:params) { { state_event: 'close' } } + + it 'calls Milestones::CloseService' do + close_service = instance_double(Milestones::CloseService) + expect(Milestones::CloseService).to receive(:new).with(project, user, {}).and_return(close_service) + expect(close_service).to receive(:execute).with(milestone) + + update_milestone.execute(milestone) + end + end - it { expect(subject).to be_valid } - it { expect(subject.title).to eq('new_title') } + context 'when params are present' do + it 'assigns the params to the milestone' do + expect(milestone).to receive(:assign_attributes).with(params.except(:state_event)) - context 'state_event is activate' do - it 'calls ReopenService' do - expect(Milestones::ReopenService).to receive(:new).with(project, user, {}).and_return(inner_service) - expect(inner_service).to receive(:execute).with(milestone) + update_milestone.execute(milestone) + end + end - described_class.new(project, user, { state_event: 'activate' }).execute(milestone) - end + context 'when milestone is changed' do + before do + allow(milestone).to receive(:changed?).and_return(true) end - context 'state_event is close' do - it 'calls ReopenService' do - expect(Milestones::CloseService).to receive(:new).with(project, user, {}).and_return(inner_service) - expect(inner_service).to receive(:execute).with(milestone) + it 'calls before_update' do + expect(update_milestone).to receive(:before_update).with(milestone) - described_class.new(project, user, { state_event: 'close' }).execute(milestone) - end + update_milestone.execute(milestone) end end + + context 'when milestone is not changed' do + before do + allow(milestone).to receive(:changed?).and_return(false) + end + + it 'does not call before_update' do + expect(update_milestone).not_to receive(:before_update) + + update_milestone.execute(milestone) + end + end + + it 'saves the milestone' do + expect(milestone).to receive(:save) + + update_milestone.execute(milestone) + end + + it 'returns the milestone' do + expect(update_milestone.execute(milestone)).to eq(milestone) + end + end + + describe '#before_update' do + it 'checks for spam' do + expect(milestone).to receive(:check_for_spam).with(user: user, action: :update) + + update_milestone.send(:before_update, milestone) + end end end |