summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/releases/update_spec.rb
blob: c541afd53a189f9585a8c2d7d893cb06b4d26147 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::Releases::Update do
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:milestone_12_3) { create(:milestone, project: project, title: '12.3') }
  let_it_be(:milestone_12_4) { create(:milestone, project: project, title: '12.4') }
  let_it_be(:reporter) { create(:user) }
  let_it_be(:developer) { create(:user) }

  let_it_be(:tag) { 'v1.1.0'}
  let_it_be(:name) { 'Version 1.0'}
  let_it_be(:description) { 'The first release :rocket:' }
  let_it_be(:released_at) { Time.parse('2018-12-10').utc }
  let_it_be(:created_at) { Time.parse('2018-11-05').utc }
  let_it_be(:milestones) { [milestone_12_3.title, milestone_12_4.title] }

  let_it_be(:release) do
    create(:release, project: project, tag: tag, name: name,
           description: description, released_at: released_at,
           created_at: created_at, milestones: [milestone_12_3, milestone_12_4])
  end

  let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }

  let(:mutation_arguments) do
    {
      project_path: project.full_path,
      tag: tag
    }
  end

  around do |example|
    freeze_time { example.run }
  end

  before do
    project.add_reporter(reporter)
    project.add_developer(developer)
  end

  shared_examples 'no changes to the release except for the' do |except_for|
    it 'does not change other release properties' do
      expect(updated_release.project).to eq(project)
      expect(updated_release.tag).to eq(tag)

      expect(updated_release.name).to eq(name) unless except_for == :name
      expect(updated_release.description).to eq(description) unless except_for == :description
      expect(updated_release.released_at).to eq(released_at) unless except_for == :released_at
      expect(updated_release.milestones.order_by_dates_and_title).to eq([milestone_12_3, milestone_12_4]) unless except_for == :milestones
    end
  end

  shared_examples 'validation error with message' do |message|
    it 'returns the updated release as nil' do
      expect(updated_release).to be_nil
    end

    it 'returns a validation error' do
      expect(subject[:errors]).to eq([message])
    end
  end

  describe '#ready?' do
    let(:current_user) { developer }

    subject(:ready) do
      mutation.ready?(**mutation_arguments)
    end

    context 'when released_at is included as an argument but is passed nil' do
      let(:mutation_arguments) { super().merge(released_at: nil) }

      it 'raises a validation error' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'if the releasedAt argument is provided, it cannot be null')
      end
    end

    context 'when milestones is included as an argument but is passed nil' do
      let(:mutation_arguments) { super().merge(milestones: nil) }

      it 'raises a validation error' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'if the milestones argument is provided, it cannot be null')
      end
    end
  end

  describe '#resolve' do
    subject(:resolve) do
      mutation.resolve(**mutation_arguments)
    end

    let(:updated_release) { subject[:release] }

    context 'when the current user has access to create releases' do
      let(:current_user) { developer }

      context 'name' do
        let(:mutation_arguments) { super().merge(name: updated_name) }

        context 'when a new name is provided' do
          let(:updated_name) { 'Updated name' }

          it 'updates the name' do
            expect(updated_release.name).to eq(updated_name)
          end

          it_behaves_like 'no changes to the release except for the', :name
        end

        context 'when nil is provided' do
          let(:updated_name) { nil }

          it 'updates the name to be the tag name' do
            expect(updated_release.name).to eq(tag)
          end

          it_behaves_like 'no changes to the release except for the', :name
        end
      end

      context 'description' do
        let(:mutation_arguments) { super().merge(description: updated_description) }

        context 'when a new description is provided' do
          let(:updated_description) { 'Updated description' }

          it 'updates the description' do
            expect(updated_release.description).to eq(updated_description)
          end

          it_behaves_like 'no changes to the release except for the', :description
        end

        context 'when nil is provided' do
          let(:updated_description) { nil }

          it 'updates the description to nil' do
            expect(updated_release.description).to eq(nil)
          end

          it_behaves_like 'no changes to the release except for the', :description
        end
      end

      context 'released_at' do
        let(:mutation_arguments) { super().merge(released_at: updated_released_at) }

        context 'when a new released_at is provided' do
          let(:updated_released_at) { Time.parse('2020-12-10').utc }

          it 'updates the released_at' do
            expect(updated_release.released_at).to eq(updated_released_at)
          end

          it_behaves_like 'no changes to the release except for the', :released_at
        end
      end

      context 'milestones' do
        let(:mutation_arguments) { super().merge(milestones: updated_milestones) }

        context 'when a new set of milestones is provided provided' do
          let(:updated_milestones) { [milestone_12_3.title] }

          it 'updates the milestone associations' do
            expect(updated_release.milestones).to eq([milestone_12_3])
          end

          it_behaves_like 'no changes to the release except for the', :milestones
        end

        context 'when an empty array is provided' do
          let(:updated_milestones) { [] }

          it 'removes all milestone associations' do
            expect(updated_release.milestones).to eq([])
          end

          it_behaves_like 'no changes to the release except for the', :milestones
        end

        context 'when a non-existent milestone title is provided' do
          let(:updated_milestones) { ['not real'] }

          it_behaves_like 'validation error with message', 'Milestone(s) not found: not real'
        end

        context 'when a milestone title from a different project is provided' do
          let(:milestone_in_different_project) { create(:milestone, title: 'milestone in different project') }
          let(:updated_milestones) { [milestone_in_different_project.title] }

          it_behaves_like 'validation error with message', 'Milestone(s) not found: milestone in different project'
        end
      end

      context 'validation' do
        context 'when no updated fields are provided' do
          it_behaves_like 'validation error with message', 'params is empty'
        end

        context 'when the tag does not exist' do
          let(:mutation_arguments) { super().merge(tag: 'not-a-real-tag') }

          it_behaves_like 'validation error with message', 'Tag does not exist'
        end

        context 'when the project does not exist' do
          let(:mutation_arguments) { super().merge(project_path: 'not/a/real/path') }

          it 'raises an error' do
            expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable, "The resource that you are attempting to access does not exist or you don't have permission to perform this action")
          end
        end
      end
    end

    context "when the current user doesn't have access to update releases" do
      let(:current_user) { reporter }

      it 'raises an error' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable, "The resource that you are attempting to access does not exist or you don't have permission to perform this action")
      end
    end
  end
end