Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_service_spec.rb « releases « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96b562a8071ad2210677d9056bd1da044e2f4896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Releases::UpdateService do
  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)
        expect(result[:milestones_updated]).to be_falsy
      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

    it 'executes hooks' do
      expect(service.release).to receive(:execute_hooks).with('update')

      service.execute
    end

    context 'when tag is protected and user does not have access to it' do
      let!(:protected_tag) { create(:protected_tag, :no_one_can_create, name: '*', project: project) }

      it 'track the error event' do
        stub_feature_flags(evalute_protected_tag_for_release_permissions: false)

        expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
          kind_of(described_class::ReleaseProtectedTagAccessError),
          project_id: project.id,
          user_id: user.id)

        service.execute
      end
    end

    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
      let!(:release) { }

      it_behaves_like 'a failed update'
    end

    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] }) }
      let(:new_milestone) { create(:milestone, project: project, title: new_title) }
      let(:service) { described_class.new(new_milestone.project, user, params_with_milestone) }

      before do
        release.milestones << milestone
      end

      context 'a different milestone' do
        let(:new_title) { 'v2.0' }

        it 'updates the related milestone accordingly' do
          result = service.execute
          release.reload

          expect(release.milestones.first.title).to eq(new_title)
          expect(result[:milestones_updated]).to be_truthy
        end
      end

      context 'an identical milestone' do
        let(:new_title) { 'v1.0' }

        it "raises an error" do
          expect { service.execute }.to raise_error(ActiveRecord::RecordInvalid)
        end
      end
    end

    context "when an 'empty' milestone is passed in" do
      let(:milestone) { create(:milestone, project: project, title: 'v1.0') }
      let(:params_with_empty_milestone) { params.merge!({ milestones: [] }) }

      before do
        release.milestones << milestone

        service.params = params_with_empty_milestone
      end

      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

    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(:params_with_milestones) { params.merge!({ milestones: [new_title_1, new_title_2] }) }
      let(:service) { described_class.new(project, user, params_with_milestones) }

      before do
        create(:milestone, project: project, title: new_title_1)
        create(:milestone, project: project, title: new_title_2)
        release.milestones << milestone
      end

      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
  end
end