Skip to content
Snippets Groups Projects
Unverified Commit cbc37dbd authored by Francisco Javier López's avatar Francisco Javier López
Browse files

Only specs missing

parent 3c49dc64
No related branches found
No related tags found
No related merge requests found
# frozen_string_literal: true
module Resolvers
module Snippets
class BlobsResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
alias_method :snippet, :object
argument :paths, [GraphQL::STRING_TYPE],
required: false,
description: 'Path of the blob'
def resolve(**args)
authorize!(snippet)
return [snippet.blob] if snippet.empty_repo?
paths = Array(args.fetch(:paths, []))
if paths.empty?
snippet.blobs
else
snippet.repository.blobs_at(transformed_blob_paths(paths))
end
end
def authorized_resource?(snippet)
Ability.allowed?(context[:current_user], :read_snippet, snippet)
end
private
def transformed_blob_paths(paths)
ref = snippet.repository.root_ref
paths.map { |path| [ref, path] }
end
end
end
end
Loading
Loading
@@ -69,10 +69,11 @@ module Types
null: false,
deprecated: { reason: 'Use `blobs`', milestone: '13.3' }
 
field :blobs, type: [Types::Snippets::BlobType],
field :blobs, type: Types::Snippets::BlobType.connection_type,
description: 'Snippet blobs',
calls_gitaly: true,
null: false
null: true,
resolver: Resolvers::Snippets::BlobsResolver
 
field :ssh_url_to_repo, type: GraphQL::STRING_TYPE,
description: 'SSH URL to the snippet repository',
Loading
Loading
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::Snippets::BlobsResolver do
include GraphqlHelpers
describe '#resolve' do
let_it_be(:current_user) { create(:user) }
let_it_be(:snippet) { create(:personal_snippet, :private, :repository, author: current_user) }
let_it_be(:blobs) { snippet.blobs }
context 'when user is not authorized' do
let(:other_user) { create(:user) }
it 'raises an error' do
expect do
resolve_blobs(snippet, user: other_user)
end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
context 'when using no filter' do
it 'returns all snippet blobs' do
expect(resolve_blobs(snippet)).to contain_exactly(*blobs)
end
end
context 'when using filters' do
context 'when setting path as a single string' do
it 'returns an array of files' do
expect(resolve_blobs(snippet, args: { paths: 'CHANGELOG' })).to contain_exactly(snippet.repository.blob_at(nil, 'CHANGELOG'))
end
end
# it 'returns the snippets by visibility' do
# aggregate_failures do
# expect(resolve_blobs(args: { visibility: 'are_private' })).to contain_exactly(private_personal_snippet)
# expect(resolve_blobs(args: { visibility: 'are_internal' })).to contain_exactly(internal_project_snippet)
# expect(resolve_blobs(args: { visibility: 'are_public' })).to contain_exactly(public_personal_snippet)
# end
# end
# it 'returns the snippets by single gid' do
# snippets = resolve_blobs(args: { ids: private_personal_snippet.to_global_id })
# expect(snippets).to contain_exactly(private_personal_snippet)
# end
# it 'returns the snippets by array of gid' do
# args = {
# ids: [private_personal_snippet.to_global_id, public_personal_snippet.to_global_id]
# }
# snippets = resolve_blobs(args: args)
# expect(snippets).to contain_exactly(private_personal_snippet, public_personal_snippet)
# end
# it 'returns an error if the gid is invalid' do
# args = {
# ids: [private_personal_snippet.to_global_id, 'foo']
# }
# expect do
# resolve_blobs(args: args)
# end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
# end
end
end
def resolve_blobs(snippet, user: current_user, args: {})
resolve(described_class, args: args, ctx: { current_user: user }, obj: snippet)
end
end
Loading
Loading
@@ -16,6 +16,15 @@ RSpec.describe GitlabSchema.types['Snippet'] do
expect(described_class).to have_graphql_fields(*expected_fields)
end
 
describe 'blobs field' do
subject { described_class.fields['blobs'] }
it 'returns blobs' do
is_expected.to have_graphql_type(Types::Snippets::BlobType.connection_type)
is_expected.to have_graphql_resolver(Resolvers::Snippets::BlobsResolver)
end
end
context 'when restricted visibility level is set to public' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
 
Loading
Loading
@@ -142,9 +151,30 @@ RSpec.describe GitlabSchema.types['Snippet'] do
 
describe '#blobs' do
let_it_be(:snippet) { create(:personal_snippet, :public, author: user) }
let(:query_blobs) { subject.dig('data', 'snippets', 'edges')[0]['node']['blobs'] }
let(:query_blobs) { subject.dig('data', 'snippets', 'edges')[0].dig('node', 'blobs', 'edges') }
let(:paths) { [] }
let(:query) do
%(
{
snippets {
edges {
node {
blobs(paths: #{paths}) {
edges {
node {
name
path
}
}
}
}
}
}
}
)
end
 
subject { GitlabSchema.execute(snippet_query_for(field: 'blobs'), context: { current_user: user }).as_json }
subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }
 
shared_examples 'an array' do
it 'returns an array of snippet blobs' do
Loading
Loading
@@ -158,8 +188,8 @@ RSpec.describe GitlabSchema.types['Snippet'] do
it_behaves_like 'an array'
 
it 'contains the first blob from the snippet' do
expect(query_blobs.first['name']).to eq blob.name
expect(query_blobs.first['path']).to eq blob.path
expect(query_blobs.first['node']['name']).to eq blob.name
expect(query_blobs.first['node']['path']).to eq blob.path
end
end
 
Loading
Loading
@@ -170,10 +200,22 @@ RSpec.describe GitlabSchema.types['Snippet'] do
it_behaves_like 'an array'
 
it 'contains all the blobs from the repository' do
resulting_blobs_names = query_blobs.map { |b| b['name'] }
resulting_blobs_names = query_blobs.map { |b| b['node']['name'] }
 
expect(resulting_blobs_names).to match_array(blobs.map(&:name))
end
context 'when specific path is set' do
let(:paths) { ['CHANGELOG'] }
it_behaves_like 'an array'
it 'returns specific files' do
resulting_blobs_names = query_blobs.map { |b| b['node']['name'] }
expect(resulting_blobs_names).to match(paths)
end
end
end
end
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment