summaryrefslogtreecommitdiff
path: root/spec/graphql/types/snippet_type_spec.rb
blob: 6e580711fda815610f22b5016ab5b4397a281237 (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
# frozen_string_literal: true

require 'spec_helper'

describe GitlabSchema.types['Snippet'] do
  let_it_be(:user) { create(:user) }

  it 'has the correct fields' do
    expected_fields = [:id, :title, :project, :author,
                       :file_name, :description,
                       :visibility_level, :created_at, :updated_at,
                       :web_url, :raw_url, :ssh_url_to_repo, :http_url_to_repo,
                       :notes, :discussions, :user_permissions,
                       :description_html, :blob]

    expect(described_class).to have_graphql_fields(*expected_fields)
  end

  describe 'authorizations' do
    it { expect(described_class).to require_graphql_authorizations(:read_snippet) }
  end

  shared_examples 'response without repository URLs' do
    it 'does not respond with repository URLs' do
      expect(response['sshUrlToRepo']).to be_nil
      expect(response['httpUrlToRepo']).to be_nil
    end
  end

  shared_examples 'snippets with repositories' do
    context 'when snippet has repository' do
      let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }

      it 'responds with repository URLs' do
        expect(response['sshUrlToRepo']).to eq(snippet.ssh_url_to_repo)
        expect(response['httpUrlToRepo']).to eq(snippet.http_url_to_repo)
      end

      context 'when version_snippets feature is disabled' do
        before do
          stub_feature_flags(version_snippets: false)
        end

        it_behaves_like 'response without repository URLs'
      end
    end
  end

  shared_examples 'snippets without repositories' do
    context 'when snippet does not have a repository' do
      let_it_be(:snippet) { create(:personal_snippet, :public, author: user) }

      it_behaves_like 'response without repository URLs'
    end
  end

  describe 'Repository URLs' do
    let(:query) do
      %(
        {
          snippets {
            nodes {
              sshUrlToRepo
              httpUrlToRepo
            }
          }
        }
      )
    end
    let(:response) { subject.dig('data', 'snippets', 'nodes')[0] }

    subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }

    context 'when RequestStore is disabled' do
      it_behaves_like 'snippets with repositories'
      it_behaves_like 'snippets without repositories'
    end

    context 'when RequestStore is enabled', :request_store do
      it_behaves_like 'snippets with repositories'
      it_behaves_like 'snippets without repositories'
    end
  end

  describe '#blob' do
    let(:query_blob) { subject.dig('data', 'snippets', 'edges')[0]['node']['blob'] }
    let(:query) do
      %(
        {
          snippets {
            edges {
              node {
                blob {
                  name
                  path
                }
              }
            }
          }
        }
      )
    end

    subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }

    context 'when snippet has repository' do
      let!(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
      let(:blob) { snippet.blobs.first }

      it 'returns blob from the repository' do
        expect(query_blob['name']).to eq blob.name
        expect(query_blob['path']).to eq blob.path
      end
    end

    context 'when snippet does not have a repository' do
      let!(:snippet) { create(:personal_snippet, :public, author: user) }
      let(:blob) { snippet.blob }

      it 'returns SnippetBlob type' do
        expect(query_blob['name']).to eq blob.name
        expect(query_blob['path']).to eq blob.path
      end
    end
  end
end