debian-mirror-gitlab/spec/graphql/resolvers/projects/snippets_resolver_spec.rb

98 lines
3.1 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Resolvers::Projects::SnippetsResolver do
2020-01-01 13:55:28 +05:30
include GraphqlHelpers
describe '#resolve' do
2021-02-22 17:27:13 +05:30
let_it_be(:user) { create(:user) }
2020-01-01 13:55:28 +05:30
let_it_be(:other_user) { create(:user) }
let_it_be(:project) { create(:project) }
2021-02-22 17:27:13 +05:30
let_it_be(:personal_snippet) { create(:personal_snippet, :private, author: user) }
let_it_be(:project_snippet) { create(:project_snippet, :internal, author: user, project: project) }
2020-01-01 13:55:28 +05:30
let_it_be(:other_project_snippet) { create(:project_snippet, :public, author: other_user, project: project) }
2021-02-22 17:27:13 +05:30
let(:current_user) { user }
before_all do
project.add_developer(user)
2020-01-01 13:55:28 +05:30
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
2021-02-22 17:27:13 +05:30
snippets = resolve_snippets(args: { ids: [global_id_of(project_snippet)] })
2020-01-01 13:55:28 +05:30
expect(snippets).to contain_exactly(project_snippet)
end
it 'returns the snippets by array of gid' do
args = {
2021-02-22 17:27:13 +05:30
ids: [global_id_of(project_snippet), global_id_of(other_project_snippet)]
2020-01-01 13:55:28 +05:30
}
snippets = resolve_snippets(args: args)
expect(snippets).to contain_exactly(project_snippet, other_project_snippet)
end
2021-02-22 17:27:13 +05:30
it 'returns an error if the gid is invalid' do
expect do
resolve_snippets(args: { ids: ['foo'] })
end.to raise_error(GraphQL::CoercionError)
end
2020-01-01 13:55:28 +05:30
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
2021-02-22 17:27:13 +05:30
let(:current_user) { other_user }
2020-01-01 13:55:28 +05:30
it 'returns no snippets' do
2021-02-22 17:27:13 +05:30
expect(resolve_snippets(args: { ids: [global_id_of(project_snippet)] })).to be_empty
2020-01-01 13:55:28 +05:30
end
end
2020-04-08 14:13:33 +05:30
context 'when project snippets are disabled' do
it 'raises an error' do
disabled_snippet_project = create(:project, :snippets_disabled)
disabled_snippet_project.add_developer(current_user)
expect(SnippetsFinder).not_to receive(:new)
expect { resolve_snippets(obj: disabled_snippet_project) }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
2020-01-01 13:55:28 +05:30
end
def resolve_snippets(args: {}, context: { current_user: current_user }, obj: project)
resolve(described_class, obj: obj, args: args, ctx: context)
end
end