Commit da9b86a3 authored by Alex Kalderimis's avatar Alex Kalderimis

Snippets: improve GraphQL tests

Add test for resolution of user permissions
Add tests to verify resolution of all fields
Add integration spec for snippets GraphQL queries
parent 10736e4f
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe GitlabSchema.types['Snippet'] do
include GraphqlHelpers
let_it_be(:user) { create(:user) }
it 'has the correct fields' do
......@@ -25,6 +27,14 @@ RSpec.describe GitlabSchema.types['Snippet'] do
end
end
describe '#user_permissions' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
it 'can resolve the snippet permissions' do
expect(resolve_field(:user_permissions, snippet)).to eq(snippet)
end
end
context 'when restricted visibility level is set to public' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
......
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe GitlabSchema.types['SnippetBlob'] do
include GraphqlHelpers
it 'has the correct fields' do
expected_fields = [:rich_data, :plain_data,
:raw_path, :size, :binary, :name, :path,
......@@ -24,4 +26,15 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
specify { expect(described_class.fields['mode'].type).not_to be_non_null }
specify { expect(described_class.fields['externalStorage'].type).not_to be_non_null }
specify { expect(described_class.fields['renderedAsText'].type).to be_non_null }
let_it_be(:snippet) { create(:snippet, :public, :repository) }
described_class.fields.each_value do |field|
it "resolves #{field.graphql_name} using the presenter", :request_store do
blob = Snippet.find(snippet.id).blobs.first
presented = SnippetBlobPresenter.new(blob)
expect(resolve_field(field, blob)).to eq(presented.try(field.method_sym))
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'snippets' do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let_it_be(:snippets) { create_list(:personal_snippet, 3, :repository, author: current_user) }
describe 'querying for all fields' do
let(:query) do
graphql_query_for(:snippets, { ids: [global_id_of(snippets.first)] }, <<~SELECT)
nodes { #{all_graphql_fields_for('Snippet')} }
SELECT
end
it 'can successfully query for snippets and their blobs' do
post_graphql(query, current_user: current_user)
expect(graphql_data_at(:snippets, :nodes)).to be_one
expect(graphql_data_at(:snippets, :nodes, :blobs, :nodes)).to be_present
end
end
describe 'snippet_blob_content' do
let_it_be(:query_file) do
Pathname.new(Rails.root.join('app/graphql/queries/snippet/snippet_blob_content.query.graphql'))
end
it 'can query for rich snippet blob content' do
ids = snippets.map { |s| global_id_of(s) }
vars = {
rich: true,
paths: ['.gitattributes'],
ids: ids
}
post_graphql(query_file.read, current_user: current_user, variables: vars)
expect(graphql_data_at(:snippets, :nodes, :blobs, :nodes, :path))
.to contain_exactly('.gitattributes', '.gitattributes', '.gitattributes')
end
it 'can query for plain snippet blob content' do
ids = snippets.map { |s| global_id_of(s) }
vars = {
rich: false,
paths: ['.gitattributes'],
ids: ids
}
post_graphql(query_file.read, current_user: current_user, variables: vars)
expect(graphql_data_at(:snippets, :nodes, :blobs, :nodes, :path))
.to contain_exactly('.gitattributes', '.gitattributes', '.gitattributes')
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment