diff options
Diffstat (limited to 'spec/graphql')
-rw-r--r-- | spec/graphql/resolvers/projects/snippets_resolver_spec.rb | 83 | ||||
-rw-r--r-- | spec/graphql/resolvers/snippets_resolver_spec.rb | 128 | ||||
-rw-r--r-- | spec/graphql/resolvers/users/snippets_resolver_spec.rb | 84 | ||||
-rw-r--r-- | spec/graphql/types/permission_types/project_spec.rb | 2 | ||||
-rw-r--r-- | spec/graphql/types/permission_types/snippet_spec.rb | 15 | ||||
-rw-r--r-- | spec/graphql/types/permission_types/user_spec.rb | 15 | ||||
-rw-r--r-- | spec/graphql/types/project_type_spec.rb | 11 | ||||
-rw-r--r-- | spec/graphql/types/query_type_spec.rb | 2 | ||||
-rw-r--r-- | spec/graphql/types/snippet_type_spec.rb | 19 | ||||
-rw-r--r-- | spec/graphql/types/user_type_spec.rb | 17 |
10 files changed, 373 insertions, 3 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 diff --git a/spec/graphql/types/permission_types/project_spec.rb b/spec/graphql/types/permission_types/project_spec.rb index 6d5a905c128..a3a9872ee1a 100644 --- a/spec/graphql/types/permission_types/project_spec.rb +++ b/spec/graphql/types/permission_types/project_spec.rb @@ -8,7 +8,7 @@ describe Types::PermissionTypes::Project do :change_namespace, :change_visibility_level, :rename_project, :remove_project, :archive_project, :remove_fork_project, :remove_pages, :read_project, :create_merge_request_in, :read_wiki, :read_project_member, :create_issue, :upload_file, :read_cycle_analytics, - :download_code, :download_wiki_code, :fork_project, :create_project_snippet, + :download_code, :download_wiki_code, :fork_project, :create_snippet, :read_commit_status, :request_access, :create_pipeline, :create_pipeline_schedule, :create_merge_request_from, :create_wiki, :push_code, :create_deployment, :push_to_delete_protected_branch, :admin_wiki, :admin_project, :update_pages, :admin_remote_mirror, :create_label, diff --git a/spec/graphql/types/permission_types/snippet_spec.rb b/spec/graphql/types/permission_types/snippet_spec.rb new file mode 100644 index 00000000000..71843153d43 --- /dev/null +++ b/spec/graphql/types/permission_types/snippet_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Types::PermissionTypes::Snippet do + it 'returns the snippets permissions' do + expected_permissions = [ + :create_note, :award_emoji, :read_snippet, :update_snippet, :admin_snippet + ] + + expected_permissions.each do |permission| + expect(described_class).to have_graphql_field(permission) + end + end +end diff --git a/spec/graphql/types/permission_types/user_spec.rb b/spec/graphql/types/permission_types/user_spec.rb new file mode 100644 index 00000000000..1e8201db568 --- /dev/null +++ b/spec/graphql/types/permission_types/user_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Types::PermissionTypes::User do + it 'returns user permissions' do + expected_permissions = [ + :create_snippet + ] + + expected_permissions.each do |permission| + expect(described_class).to have_graphql_field(permission) + end + end +end diff --git a/spec/graphql/types/project_type_spec.rb b/spec/graphql/types/project_type_spec.rb index 8a697b1bcae..5d1a5fe1987 100644 --- a/spec/graphql/types/project_type_spec.rb +++ b/spec/graphql/types/project_type_spec.rb @@ -22,7 +22,7 @@ describe GitlabSchema.types['Project'] do only_allow_merge_if_pipeline_succeeds request_access_enabled only_allow_merge_if_all_discussions_are_resolved printing_merge_request_link_enabled namespace group statistics repository merge_requests merge_request issues - issue pipelines removeSourceBranchAfterMerge sentryDetailedError + issue pipelines removeSourceBranchAfterMerge sentryDetailedError snippets ] is_expected.to have_graphql_fields(*expected_fields) @@ -63,4 +63,13 @@ describe GitlabSchema.types['Project'] do is_expected.to have_graphql_resolver(Resolvers::MergeRequestsResolver) end end + + describe 'snippets field' do + subject { described_class.fields['snippets'] } + + it 'returns snippets' do + is_expected.to have_graphql_type(Types::SnippetType.connection_type) + is_expected.to have_graphql_resolver(Resolvers::Projects::SnippetsResolver) + end + end end diff --git a/spec/graphql/types/query_type_spec.rb b/spec/graphql/types/query_type_spec.rb index 1365bc0dc14..b2d0ba27d4e 100644 --- a/spec/graphql/types/query_type_spec.rb +++ b/spec/graphql/types/query_type_spec.rb @@ -7,7 +7,7 @@ describe GitlabSchema.types['Query'] do expect(described_class.graphql_name).to eq('Query') end - it { is_expected.to have_graphql_fields(:project, :namespace, :group, :echo, :metadata, :current_user) } + it { is_expected.to have_graphql_fields(:project, :namespace, :group, :echo, :metadata, :current_user, :snippets) } describe 'namespace field' do subject { described_class.fields['namespace'] } diff --git a/spec/graphql/types/snippet_type_spec.rb b/spec/graphql/types/snippet_type_spec.rb new file mode 100644 index 00000000000..3c3250a5fa2 --- /dev/null +++ b/spec/graphql/types/snippet_type_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe GitlabSchema.types['Snippet'] do + it 'has the correct fields' do + expected_fields = [:id, :title, :project, :author, + :file_name, :content, :description, + :visibility, :created_at, :updated_at, + :web_url, :raw_url, :notes, :discussions, + :user_permissions, :description_html] + + is_expected.to have_graphql_fields(*expected_fields) + end + + describe 'authorizations' do + it { expect(described_class).to require_graphql_authorizations(:read_snippet) } + end +end diff --git a/spec/graphql/types/user_type_spec.rb b/spec/graphql/types/user_type_spec.rb index 8134cc13eb4..b9174b9a90b 100644 --- a/spec/graphql/types/user_type_spec.rb +++ b/spec/graphql/types/user_type_spec.rb @@ -6,4 +6,21 @@ describe GitlabSchema.types['User'] do it { expect(described_class.graphql_name).to eq('User') } it { expect(described_class).to require_graphql_authorizations(:read_user) } + + it 'has the expected fields' do + expected_fields = %w[ + user_permissions snippets name username avatarUrl webUrl todos + ] + + is_expected.to have_graphql_fields(*expected_fields) + end + + describe 'snippets field' do + subject { described_class.fields['snippets'] } + + it 'returns snippets' do + is_expected.to have_graphql_type(Types::SnippetType.connection_type) + is_expected.to have_graphql_resolver(Resolvers::Users::SnippetsResolver) + end + end end |