summaryrefslogtreecommitdiff
path: root/spec/graphql/resolvers/snippets/blobs_resolver_spec.rb
blob: ebe286769cf9b06f03375feeb31376fe59882c80 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::Snippets::BlobsResolver do
  include GraphqlHelpers

  specify do
    expect(described_class).to have_nullable_graphql_type(Types::Snippets::BlobType.connection_type)
  end

  describe '#resolve' do
    let_it_be(:current_user) { create(:user) }
    let_it_be(:snippet) { create(:personal_snippet, :private, :repository, author: current_user) }

    context 'when user is not authorized' do
      let(:other_user) { create(:user) }

      it 'redacts the field' do
        expect(resolve_blobs(snippet, user: other_user)).to be_nil
      end
    end

    context 'when using no filter' do
      it 'returns all snippet blobs' do
        result = resolve_blobs(snippet, args: {})

        expect(result).to match_array(snippet.list_files.map do |file|
          have_attributes(path: file)
        end)
      end
    end

    context 'when using filters' do
      context 'when paths is a single string' do
        it 'returns an array of files' do
          path = 'CHANGELOG'

          expect(resolve_blobs(snippet, paths: [path])).to contain_exactly(have_attributes(path: path))
        end
      end

      context 'the argument does not match anything' do
        it 'returns an empty result' do
          expect(resolve_blobs(snippet, paths: ['does not exist'])).to be_empty
        end
      end

      context 'when paths is an array of string' do
        it 'returns an array of files' do
          paths = ['CHANGELOG', 'README.md']

          expect(resolve_blobs(snippet, paths: paths)).to match_array(paths.map do |file|
            have_attributes(path: file)
          end)
        end
      end
    end
  end

  def resolve_blobs(snippet, user: current_user, paths: [], args: { paths: paths })
    resolve(described_class, args: args, ctx: { current_user: user }, obj: snippet)
  end
end