Commit 9b1b1fc7 authored by charlie ablett's avatar charlie ablett

Merge branch 'ajk-graphql-snippets-test-improvements' into 'master'

Improve GraphQL snippets tests

See merge request gitlab-org/gitlab!55322
parents 1e7ea614 518b33fd
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe GitlabSchema.types['Snippet'] do RSpec.describe GitlabSchema.types['Snippet'] do
include GraphqlHelpers
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
it 'has the correct fields' do it 'has the correct fields' do
...@@ -25,6 +27,14 @@ RSpec.describe GitlabSchema.types['Snippet'] do ...@@ -25,6 +27,14 @@ RSpec.describe GitlabSchema.types['Snippet'] do
end end
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 context 'when restricted visibility level is set to public' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) } let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe GitlabSchema.types['SnippetBlob'] do RSpec.describe GitlabSchema.types['SnippetBlob'] do
include GraphqlHelpers
it 'has the correct fields' do it 'has the correct fields' do
expected_fields = [:rich_data, :plain_data, expected_fields = [:rich_data, :plain_data,
:raw_path, :size, :binary, :name, :path, :raw_path, :size, :binary, :name, :path,
...@@ -12,16 +14,37 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do ...@@ -12,16 +14,37 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
expect(described_class).to have_graphql_fields(*expected_fields) expect(described_class).to have_graphql_fields(*expected_fields)
end end
specify { expect(described_class.fields['richData'].type).not_to be_non_null } let_it_be(:nullity) do
specify { expect(described_class.fields['plainData'].type).not_to be_non_null } {
specify { expect(described_class.fields['rawPath'].type).to be_non_null } 'richData' => be_nullable,
specify { expect(described_class.fields['size'].type).to be_non_null } 'plainData' => be_nullable,
specify { expect(described_class.fields['binary'].type).to be_non_null } 'rawPath' => be_non_null,
specify { expect(described_class.fields['name'].type).not_to be_non_null } 'size' => be_non_null,
specify { expect(described_class.fields['path'].type).not_to be_non_null } 'binary' => be_non_null,
specify { expect(described_class.fields['simpleViewer'].type).to be_non_null } 'name' => be_nullable,
specify { expect(described_class.fields['richViewer'].type).not_to be_non_null } 'path' => be_nullable,
specify { expect(described_class.fields['mode'].type).not_to be_non_null } 'simpleViewer' => be_non_null,
specify { expect(described_class.fields['externalStorage'].type).not_to be_non_null } 'richViewer' => be_nullable,
specify { expect(described_class.fields['renderedAsText'].type).to be_non_null } 'mode' => be_nullable,
'externalStorage' => be_nullable,
'renderedAsText' => be_non_null
}
end
let_it_be(:blob) { create(:snippet, :public, :repository).blobs.first }
shared_examples 'a field from the snippet blob presenter' do |field|
it "resolves using the presenter", :request_store do
presented = SnippetBlobPresenter.new(blob)
expect(resolve_field(field, blob)).to eq(presented.try(field.method_sym))
end
end
described_class.fields.each_value do |field|
describe field.graphql_name do
it_behaves_like 'a field from the snippet blob presenter', field
specify { expect(field.type).to match(nullity.fetch(field.graphql_name)) }
end
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
end
# frozen_string_literal: true # frozen_string_literal: true
RSpec::Matchers.define_negated_matcher :be_nullable, :be_non_null
RSpec::Matchers.define :require_graphql_authorizations do |*expected| RSpec::Matchers.define :require_graphql_authorizations do |*expected|
match do |klass| match do |klass|
permissions = if klass.respond_to?(:required_permissions) permissions = if klass.respond_to?(:required_permissions)
...@@ -90,7 +92,7 @@ RSpec::Matchers.define :have_graphql_arguments do |*expected| ...@@ -90,7 +92,7 @@ RSpec::Matchers.define :have_graphql_arguments do |*expected|
@names ||= Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) } @names ||= Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) }
if field.type.try(:ancestors)&.include?(GraphQL::Types::Relay::BaseConnection) if field.type.try(:ancestors)&.include?(GraphQL::Types::Relay::BaseConnection)
@names | %w(after before first last) @names | %w[after before first last]
else else
@names @names
end end
...@@ -103,9 +105,10 @@ RSpec::Matchers.define :have_graphql_arguments do |*expected| ...@@ -103,9 +105,10 @@ RSpec::Matchers.define :have_graphql_arguments do |*expected|
end end
failure_message do |field| failure_message do |field|
names = expected_names(field) names = expected_names(field).inspect
args = field.arguments.keys.inspect
"expected that #{field.name} would have the following fields: #{names.inspect}, but it has #{field.arguments.keys.inspect}." "expected that #{field.name} would have the following arguments: #{names}, but it has #{args}."
end 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