summaryrefslogtreecommitdiff
path: root/spec/graphql/resolvers
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 07:53:40 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 07:53:40 +0000
commitcfc792b9ca064990e6540cb742e80529ea669a81 (patch)
tree147cd4256319990cebbc02fe8e4fbbbe06f5720a /spec/graphql/resolvers
parent93c6764dacd4c605027ef1cd367d3aebe420b223 (diff)
downloadgitlab-ce-cfc792b9ca064990e6540cb742e80529ea669a81.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/graphql/resolvers')
-rw-r--r--spec/graphql/resolvers/projects/snippets_resolver_spec.rb83
-rw-r--r--spec/graphql/resolvers/snippets_resolver_spec.rb128
-rw-r--r--spec/graphql/resolvers/users/snippets_resolver_spec.rb84
3 files changed, 295 insertions, 0 deletions
diff --git a/spec/graphql/resolvers/projects/snippets_resolver_spec.rb b/spec/graphql/resolvers/projects/snippets_resolver_spec.rb
new file mode 100644
index 00000000000..eef891bf984
--- /dev/null
+++ b/spec/graphql/resolvers/projects/snippets_resolver_spec.rb
@@ -0,0 +1,83 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Resolvers::Projects::SnippetsResolver do
+ include GraphqlHelpers
+
+ describe '#resolve' do
+ let_it_be(:current_user) { create(:user) }
+ let_it_be(:other_user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+
+ let_it_be(:personal_snippet) { create(:personal_snippet, :private, author: current_user) }
+ let_it_be(:project_snippet) { create(:project_snippet, :internal, author: current_user, project: project) }
+ let_it_be(:other_project_snippet) { create(:project_snippet, :public, author: other_user, project: project) }
+
+ before do
+ project.add_developer(current_user)
+ end
+
+ it 'calls SnippetsFinder' do
+ expect_next_instance_of(SnippetsFinder) do |finder|
+ expect(finder).to receive(:execute)
+ end
+
+ resolve_snippets
+ end
+
+ context 'when using no filter' do
+ it 'returns expected snippets' do
+ expect(resolve_snippets).to contain_exactly(project_snippet, other_project_snippet)
+ end
+ end
+
+ context 'when using filters' do
+ it 'returns the snippets by visibility' do
+ aggregate_failures do
+ expect(resolve_snippets(args: { visibility: 'are_private' })).to be_empty
+ expect(resolve_snippets(args: { visibility: 'are_internal' })).to contain_exactly(project_snippet)
+ expect(resolve_snippets(args: { visibility: 'are_public' })).to contain_exactly(other_project_snippet)
+ end
+ end
+
+ it 'returns the snippets by gid' do
+ snippets = resolve_snippets(args: { ids: project_snippet.to_global_id })
+
+ expect(snippets).to contain_exactly(project_snippet)
+ end
+
+ it 'returns the snippets by array of gid' do
+ args = {
+ ids: [project_snippet.to_global_id, other_project_snippet.to_global_id]
+ }
+
+ snippets = resolve_snippets(args: args)
+
+ expect(snippets).to contain_exactly(project_snippet, other_project_snippet)
+ end
+
+ it 'returns an error if the gid is invalid' do
+ expect do
+ resolve_snippets(args: { ids: 'foo' })
+ end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
+ end
+ end
+
+ context 'when no project is provided' do
+ it 'returns no snippets' do
+ expect(resolve_snippets(obj: nil)).to be_empty
+ end
+ end
+
+ context 'when provided user is not current user' do
+ it 'returns no snippets' do
+ expect(resolve_snippets(context: { current_user: other_user }, args: { ids: project_snippet.to_global_id })).to be_empty
+ end
+ end
+ end
+
+ def resolve_snippets(args: {}, context: { current_user: current_user }, obj: project)
+ resolve(described_class, obj: obj, args: args, ctx: context)
+ end
+end
diff --git a/spec/graphql/resolvers/snippets_resolver_spec.rb b/spec/graphql/resolvers/snippets_resolver_spec.rb
new file mode 100644
index 00000000000..89c350020f0
--- /dev/null
+++ b/spec/graphql/resolvers/snippets_resolver_spec.rb
@@ -0,0 +1,128 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Resolvers::SnippetsResolver do
+ include GraphqlHelpers
+
+ describe '#resolve' do
+ let_it_be(:current_user) { create(:user) }
+ let_it_be(:other_user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+
+ let_it_be(:personal_snippet) { create(:personal_snippet, :private, author: current_user) }
+ let_it_be(:other_personal_snippet) { create(:personal_snippet, :internal, author: other_user) }
+ let_it_be(:project_snippet) { create(:project_snippet, :internal, author: current_user, project: project) }
+ let_it_be(:other_project_snippet) { create(:project_snippet, :public, author: other_user, project: project) }
+
+ before do
+ project.add_developer(current_user)
+ end
+
+ it 'calls SnippetsFinder' do
+ expect_next_instance_of(SnippetsFinder) do |finder|
+ expect(finder).to receive(:execute)
+ end
+
+ resolve_snippets
+ end
+
+ context 'when using no filter' do
+ it 'returns expected snippets' do
+ expect(resolve_snippets).to contain_exactly(personal_snippet, other_personal_snippet, project_snippet, other_project_snippet)
+ end
+ end
+
+ context 'when using filters' do
+ context 'by author id' do
+ it 'returns the snippets' do
+ snippets = resolve_snippets(args: { author_id: current_user.to_global_id })
+
+ expect(snippets).to contain_exactly(personal_snippet, project_snippet)
+ end
+
+ it 'returns an error if the param id is invalid' do
+ expect do
+ resolve_snippets(args: { author_id: 'foo' })
+ end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
+ end
+ end
+
+ it 'returns the snippets by type' do
+ aggregate_failures do
+ expect(resolve_snippets(args: { type: 'personal' })).to contain_exactly(personal_snippet, other_personal_snippet)
+ expect(resolve_snippets(args: { type: 'project' })).to contain_exactly(project_snippet, other_project_snippet)
+ end
+ end
+
+ context 'by project id' do
+ it 'returns the snippets' do
+ snippets = resolve_snippets(args: { project_id: project.to_global_id })
+
+ expect(snippets).to contain_exactly(project_snippet, other_project_snippet)
+ end
+
+ it 'returns an error if the param id is invalid' do
+ expect do
+ resolve_snippets(args: { project_id: 'foo' })
+ end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
+ end
+ end
+
+ it 'returns the snippets by visibility' do
+ aggregate_failures do
+ expect(resolve_snippets(args: { visibility: 'are_private' })).to contain_exactly(personal_snippet)
+ expect(resolve_snippets(args: { visibility: 'are_internal' })).to contain_exactly(project_snippet, other_personal_snippet)
+ expect(resolve_snippets(args: { visibility: 'are_public' })).to contain_exactly(other_project_snippet)
+ end
+ end
+
+ it 'returns snippets to explore' do
+ snippets = resolve_snippets(args: { explore: true })
+
+ expect(snippets).to contain_exactly(other_personal_snippet)
+ end
+
+ it 'returns the snippets by single gid' do
+ snippets = resolve_snippets(args: { ids: personal_snippet.to_global_id })
+
+ expect(snippets).to contain_exactly(personal_snippet)
+ end
+
+ it 'returns the snippets by array of gid' do
+ args = {
+ ids: [personal_snippet.to_global_id, project_snippet.to_global_id]
+ }
+
+ snippets = resolve_snippets(args: args)
+
+ expect(snippets).to contain_exactly(personal_snippet, project_snippet)
+ end
+
+ it 'returns an error if the gid is invalid' do
+ args = {
+ ids: [personal_snippet.to_global_id, 'foo']
+ }
+
+ expect do
+ resolve_snippets(args: args)
+ end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
+ end
+
+ it 'returns an error if both project and author are provided' do
+ expect do
+ args = {
+ author_id: current_user.to_global_id,
+ project_id: project.to_global_id
+ }
+
+ resolve_snippets(args: args)
+ end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
+ end
+ end
+ end
+
+ def resolve_snippets(args: {})
+ resolve(described_class, obj: nil, args: args, ctx: { current_user: current_user })
+ end
+end
diff --git a/spec/graphql/resolvers/users/snippets_resolver_spec.rb b/spec/graphql/resolvers/users/snippets_resolver_spec.rb
new file mode 100644
index 00000000000..6412d77e02b
--- /dev/null
+++ b/spec/graphql/resolvers/users/snippets_resolver_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Resolvers::Users::SnippetsResolver do
+ include GraphqlHelpers
+
+ describe '#resolve' do
+ let_it_be(:current_user) { create(:user) }
+ let_it_be(:other_user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+
+ let_it_be(:private_personal_snippet) { create(:personal_snippet, :private, author: current_user) }
+ let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, author: current_user) }
+ let_it_be(:other_personal_snippet) { create(:personal_snippet, :internal, author: other_user) }
+ let_it_be(:internal_project_snippet) { create(:project_snippet, :internal, author: current_user, project: project) }
+ let_it_be(:other_project_snippet) { create(:project_snippet, :public, author: other_user, project: project) }
+
+ before do
+ project.add_developer(current_user)
+ end
+
+ it 'calls SnippetsFinder' do
+ expect_next_instance_of(SnippetsFinder) do |finder|
+ expect(finder).to receive(:execute)
+ end
+
+ resolve_snippets
+ end
+
+ context 'when using no filter' do
+ it 'returns expected authored snippets' do
+ expect(resolve_snippets).to contain_exactly(private_personal_snippet, public_personal_snippet, internal_project_snippet)
+ end
+ end
+
+ context 'when using filters' do
+ it 'returns the snippets by visibility' do
+ aggregate_failures do
+ expect(resolve_snippets(args: { visibility: 'are_private' })).to contain_exactly(private_personal_snippet)
+ expect(resolve_snippets(args: { visibility: 'are_internal' })).to contain_exactly(internal_project_snippet)
+ expect(resolve_snippets(args: { visibility: 'are_public' })).to contain_exactly(public_personal_snippet)
+ end
+ end
+
+ it 'returns the snippets by type' do
+ aggregate_failures do
+ expect(resolve_snippets(args: { type: 'personal' })).to contain_exactly(private_personal_snippet, public_personal_snippet)
+ expect(resolve_snippets(args: { type: 'project' })).to contain_exactly(internal_project_snippet)
+ end
+ end
+
+ it 'returns the snippets by single gid' do
+ snippets = resolve_snippets(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_snippets(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_snippets(args: args)
+ end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
+ end
+ end
+ end
+
+ def resolve_snippets(args: {})
+ resolve(described_class, args: args, ctx: { current_user: current_user }, obj: current_user)
+ end
+end