summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/mutations/issues/move_spec.rb
blob: 20ed16879f607d6400782c1fe5ba74972d6dc47a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Moving an issue' do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:issue) { create(:issue) }
  let_it_be(:target_project) { create(:project) }

  let(:mutation) do
    variables = {
      project_path: issue.project.full_path,
      target_project_path: target_project.full_path,
      iid: issue.iid.to_s
    }

    graphql_mutation(:issue_move, variables,
                     <<-QL.strip_heredoc
                       clientMutationId
                       errors
                       issue {
                         title
                       }
                     QL
    )
  end

  def mutation_response
    graphql_mutation_response(:issue_move)
  end

  context 'when the user is not allowed to read source project' do
    it 'returns an error' do
      error = Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR
      post_graphql_mutation(mutation, current_user: user)

      expect(response).to have_gitlab_http_status(:success)
      expect(graphql_errors).to include(a_hash_including('message' => error))
    end
  end

  context 'when the user is not allowed to move issue to target project' do
    before do
      issue.project.add_developer(user)
    end

    it 'returns an error' do
      error = "Cannot move issue due to insufficient permissions!"
      post_graphql_mutation(mutation, current_user: user)

      expect(response).to have_gitlab_http_status(:success)
      expect(mutation_response['errors'][0]).to eq(error)
    end
  end

  context 'when the user is allowed to move issue' do
    before do
      issue.project.add_developer(user)
      target_project.add_developer(user)
    end

    it 'moves the issue' do
      post_graphql_mutation(mutation, current_user: user)

      expect(response).to have_gitlab_http_status(:success)
      expect(mutation_response.dig('issue', 'title')).to eq(issue.title)
      expect(issue.reload.state).to eq('closed')
      expect(target_project.issues.find_by_title(issue.title)).to be_present
    end
  end
end