summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/merge_requests/update_spec.rb
blob: 4a1fdf6e74ba1760bc0800b055751aef8dd5b139 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::MergeRequests::Update 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(:mutated_merge_request) { subject[:merge_request] }

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

    it 'raises an error if the resource is not accessible to the user' do
      expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end

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

      it 'applies all attributes' do
        expect(mutated_merge_request).to eq(merge_request)
        expect(mutated_merge_request).to have_attributes(attributes)
        expect(subject[:errors]).to be_empty
      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
    end
  end
end