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

update_spec.rb « merge_requests « mutations « graphql « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c34ec939d122c70c15f8661537c9384421509298 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::MergeRequests::Update, feature_category: :team_planning do
  let(:merge_request) { create(:merge_request) }
  let(:user) { create(:user) }

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

  specify { expect(described_class).to require_graphql_authorizations(:update_merge_request) }

  describe '#resolve' do
    let(:attributes) { { title: 'new title', description: 'new description', target_branch: 'new-branch' } }
    let(:arguments) { attributes }
    let(:mutated_merge_request) { subject[:merge_request] }

    subject do
      mutation.resolve(project_path: merge_request.project.full_path, iid: merge_request.iid, **arguments)
    end

    it_behaves_like 'permission level for merge request mutation is correctly verified'

    context 'when the user can update the merge request' do
      before do
        merge_request.project.add_developer(user)
      end

      context 'when all attributes except timeEstimate are provided' do
        before do
          merge_request.update!(time_estimate: 3600)
        end

        it 'applies all attributes' do
          expect(mutated_merge_request).to eq(merge_request)
          expect(mutated_merge_request).to have_attributes(attributes)
          expect(mutated_merge_request.time_estimate).to eq(3600)
          expect(subject[:errors]).to be_empty
        end
      end

      context 'when timeEstimate attribute is provided' do
        let(:time_estimate) { '0' }
        let(:attributes) { { time_estimate: time_estimate } }

        before do
          merge_request.update!(time_estimate: 3600)
        end

        context 'when timeEstimate is invalid' do
          let(:time_estimate) { '1e' }

          it 'changes are not applied' do
            expect { mutation.ready?(time_estimate: time_estimate) }
              .to raise_error(
                Gitlab::Graphql::Errors::ArgumentError,
                'timeEstimate must be formatted correctly, for example `1h 30m`')
            expect(mutated_merge_request.time_estimate).to eq(3600)
          end
        end

        context 'when timeEstimate is negative' do
          let(:time_estimate) { '-1h' }

          it 'raises an argument error and changes are not applied' do
            expect { mutation.ready?(time_estimate: time_estimate) }
            .to raise_error(Gitlab::Graphql::Errors::ArgumentError,
              'timeEstimate must be greater than or equal to zero. ' \
              'Remember that every new timeEstimate overwrites the previous value.')
            expect { subject }.not_to change { merge_request.time_estimate }
          end
        end

        context 'when timeEstimate is 0' do
          let(:time_estimate) { '0' }

          it 'resets the time estimate' do
            expect(mutated_merge_request.time_estimate).to eq(0)
            expect(subject[:errors]).to be_empty
          end
        end

        context 'when timeEstimate is a valid human readable time' do
          let(:time_estimate) { '1h 30m' }

          it 'updates the time estimate' do
            expect(mutated_merge_request.time_estimate).to eq(5400)
            expect(subject[:errors]).to be_empty
          end
        end
      end

      context 'the merge request is invalid' do
        before do
          merge_request.allow_broken = true
          merge_request.update!(source_project: nil)
        end

        it 'returns error information, and changes were not applied' do
          expect(mutated_merge_request).not_to have_attributes(attributes)
          expect(subject[:errors]).not_to be_empty
        end
      end

      context 'our change is invalid' do
        let(:attributes) { { target_branch: 'this is not a branch' } }

        it 'returns error information, and changes were not applied' do
          expect(mutated_merge_request).not_to have_attributes(attributes)
          expect(subject[:errors]).not_to be_empty
        end
      end

      context 'when passing subset of attributes' do
        let(:attributes) { { title: 'no, this title' } }

        it 'only changes the mentioned attributes' do
          expect { subject }.not_to change { merge_request.reset.description }

          expect(mutated_merge_request).to have_attributes(attributes)
        end
      end

      context 'when closing the MR' do
        let(:arguments) { { state_event: ::Types::MergeRequestStateEventEnum.values['CLOSED'].value } }

        it 'closes the MR' do
          expect(mutated_merge_request).to be_closed
        end
      end

      context 'when re-opening the MR' do
        let(:arguments) { { state_event: ::Types::MergeRequestStateEventEnum.values['OPEN'].value } }

        it 'closes the MR' do
          merge_request.close!

          expect(mutated_merge_request).to be_open
        end
      end
    end
  end

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

    let(:arguments) do
      {
        project_path: merge_request.project.full_path,
        iid: merge_request.iid
      }.merge(extra_args)
    end

    subject(:ready) { mutation.ready?(**arguments) }

    context 'when timeEstimate is provided' do
      let(:extra_args) { { time_estimate: time_estimate } }

      context 'when the value is invalid' do
        let(:time_estimate) { '1e' }

        it 'raises an argument error' do
          expect { subject }.to raise_error(
            Gitlab::Graphql::Errors::ArgumentError,
            'timeEstimate must be formatted correctly, for example `1h 30m`')
        end
      end

      context 'when the value valid' do
        let(:time_estimate) { '1d' }

        it 'returns true' do
          expect(subject).to eq(true)
        end
      end
    end
  end
end