debian-mirror-gitlab/spec/graphql/resolvers/snippets/blobs_resolver_spec.rb

74 lines
2.3 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::Snippets::BlobsResolver do
include GraphqlHelpers
2021-01-29 00:20:46 +05:30
specify do
expect(described_class).to have_nullable_graphql_type(Types::Snippets::BlobType.connection_type)
end
2021-01-03 14:25:43 +05:30
describe '#resolve' do
let_it_be(:current_user) { create(:user) }
let_it_be(:snippet) { create(:personal_snippet, :private, :repository, author: current_user) }
2022-02-27 12:50:16 +05:30
let(:query_context) { {} }
2021-01-03 14:25:43 +05:30
context 'when user is not authorized' do
let(:other_user) { create(:user) }
2021-02-22 17:27:13 +05:30
it 'redacts the field' do
expect(resolve_blobs(snippet, user: other_user)).to be_nil
2022-02-27 12:50:16 +05:30
expect(query_context[:unretrievable_blobs?]).to eq(false)
2021-01-03 14:25:43 +05:30
end
end
context 'when using no filter' do
it 'returns all snippet blobs' do
2021-02-22 17:27:13 +05:30
result = resolve_blobs(snippet, args: {})
expect(result).to match_array(snippet.list_files.map do |file|
have_attributes(path: file)
end)
2022-02-27 12:50:16 +05:30
expect(query_context[:unretrievable_blobs?]).to eq(false)
2021-01-03 14:25:43 +05:30
end
end
context 'when using filters' do
context 'when paths is a single string' do
it 'returns an array of files' do
path = 'CHANGELOG'
2021-02-22 17:27:13 +05:30
expect(resolve_blobs(snippet, paths: [path])).to contain_exactly(have_attributes(path: path))
2022-02-27 12:50:16 +05:30
expect(query_context[:unretrievable_blobs?]).to eq(false)
2021-02-22 17:27:13 +05:30
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
2022-02-27 12:50:16 +05:30
expect(query_context[:unretrievable_blobs?]).to eq(true)
2021-01-03 14:25:43 +05:30
end
end
context 'when paths is an array of string' do
it 'returns an array of files' do
paths = ['CHANGELOG', 'README.md']
2021-02-22 17:27:13 +05:30
expect(resolve_blobs(snippet, paths: paths)).to match_array(paths.map do |file|
have_attributes(path: file)
end)
2022-02-27 12:50:16 +05:30
expect(query_context[:unretrievable_blobs?]).to eq(false)
2021-01-03 14:25:43 +05:30
end
end
end
end
2022-02-27 12:50:16 +05:30
def resolve_blobs(snippet, user: current_user, paths: [], args: { paths: paths }, has_unretrievable_blobs: false)
query_context[:current_user] = user
query_context[:unretrievable_blobs?] = has_unretrievable_blobs
resolve(described_class, args: args, ctx: query_context, obj: snippet)
2021-01-03 14:25:43 +05:30
end
end