summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/project/tree/tree_spec.rb
blob: b07aa1e12d3f3880a8f8ec661f98739a9f0ffce2 (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'

describe 'getting a tree in a project' do
  include GraphqlHelpers

  let(:project) { create(:project, :repository) }
  let(:current_user) { project.owner }
  let(:path) { "" }
  let(:ref) { "master" }
  let(:fields) do
    <<~QUERY
      tree(path:"#{path}", ref:"#{ref}") {
        #{all_graphql_fields_for('tree'.classify)}
      }
    QUERY
  end
  let(:query) do
    graphql_query_for(
      'project',
      { 'fullPath' => project.full_path },
      query_graphql_field('repository', {}, fields)
    )
  end

  context 'when path does not exist' do
    let(:path) { "testing123" }

    it 'returns empty tree' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']['repository']['tree']['trees']['edges']).to eq([])
      expect(graphql_data['project']['repository']['tree']['submodules']['edges']).to eq([])
      expect(graphql_data['project']['repository']['tree']['blobs']['edges']).to eq([])
    end
  end

  context 'when ref does not exist' do
    let(:ref) { "testing123" }

    it 'returns empty tree' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']['repository']['tree']['trees']['edges']).to eq([])
      expect(graphql_data['project']['repository']['tree']['submodules']['edges']).to eq([])
      expect(graphql_data['project']['repository']['tree']['blobs']['edges']).to eq([])
    end
  end

  context 'when ref and path exist' do
    it 'returns tree' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']['repository']['tree']).to be_present
    end

    it 'returns blobs, subtrees and submodules inside tree' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']['repository']['tree']['trees']['edges'].size).to be > 0
      expect(graphql_data['project']['repository']['tree']['blobs']['edges'].size).to be > 0
      expect(graphql_data['project']['repository']['tree']['submodules']['edges'].size).to be > 0
    end
  end

  context 'when current user is nil' do
    it 'returns empty project' do
      post_graphql(query, current_user: nil)

      expect(graphql_data['project']).to be(nil)
    end
  end
end