summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/project_query_spec.rb
blob: 796ffc9d569725ce9402e762054a018b20846781 (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
require 'spec_helper'

describe 'getting project information' do
  include GraphqlHelpers

  let(:project) { create(:project, :repository) }
  let(:current_user) { create(:user) }

  let(:query) do
    graphql_query_for('project', 'fullPath' => project.full_path)
  end

  context 'when the user has access to the project' do
    before do
      project.add_developer(current_user)
    end

    it 'includes the project' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']).not_to be_nil
    end

    it_behaves_like 'a working graphql query' do
      before do
        post_graphql(query, current_user: current_user)
      end
    end

    context 'when requesting a nested merge request' do
      let(:merge_request) { create(:merge_request, source_project: project) }
      let(:merge_request_graphql_data) { graphql_data['project']['mergeRequest'] }

      let(:query) do
        graphql_query_for(
          'project',
          { 'fullPath' => project.full_path },
          query_graphql_field('mergeRequest', iid: merge_request.iid)
        )
      end

      it_behaves_like 'a working graphql query' do
        before do
          post_graphql(query, current_user: current_user)
        end
      end

      it 'contains merge request information' do
        post_graphql(query, current_user: current_user)

        expect(merge_request_graphql_data).not_to be_nil
      end

      # This is a field coming from the `MergeRequestPresenter`
      it 'includes a web_url' do
        post_graphql(query, current_user: current_user)

        expect(merge_request_graphql_data['webUrl']).to be_present
      end

      context 'when the user does not have access to the merge request' do
        let(:project) { create(:project, :public, :repository) }

        it 'returns nil' do
          project.project_feature.update!(merge_requests_access_level: ProjectFeature::PRIVATE)

          post_graphql(query)

          expect(merge_request_graphql_data).to be_nil
        end
      end
    end
  end

  context 'when the user does not have access to the project' do
    it 'returns an empty field' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']).to be_nil
    end

    it_behaves_like 'a working graphql query' do
      before do
        post_graphql(query, current_user: current_user)
      end
    end
  end
end