Add files argument to snippets create mutation

parent 1e87c00a
......@@ -36,6 +36,10 @@ module Mutations
required: false,
description: 'The project full path the snippet is associated with'
argument :uploaded_files, [GraphQL::STRING_TYPE],
required: false,
description: 'The paths to files uploaded in the snippet description'
def resolve(args)
project_path = args.delete(:project_path)
......@@ -45,9 +49,14 @@ module Mutations
raise_resource_not_available_error!
end
# We need to rename `uploaded_files` into `files` because
# it's the expected key param
args[:files] = args.delete(:uploaded_files)
service_response = ::Snippets::CreateService.new(project,
context[:current_user],
args).execute
snippet = service_response.payload[:snippet]
{
......
---
title: Add files param to snippet create mutation
merge_request: 32309
author:
type: changed
......@@ -1334,6 +1334,11 @@ input CreateSnippetInput {
"""
title: String!
"""
The paths to files uploaded in the snippet description
"""
uploadedFiles: [String!]
"""
The visibility level of the snippet
"""
......
......@@ -3608,6 +3608,24 @@
},
"defaultValue": null
},
{
"name": "uploadedFiles",
"description": "The paths to files uploaded in the snippet description",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
"defaultValue": null
},
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
......@@ -13,6 +13,7 @@ describe 'Creating a Snippet' do
let(:file_name) { 'Initial file_name' }
let(:visibility_level) { 'public' }
let(:project_path) { nil }
let(:uploaded_files) { nil }
let(:mutation) do
variables = {
......@@ -21,7 +22,8 @@ describe 'Creating a Snippet' do
visibility_level: visibility_level,
file_name: file_name,
title: title,
project_path: project_path
project_path: project_path,
uploaded_files: uploaded_files
}
graphql_mutation(:create_snippet, variables)
......@@ -31,6 +33,8 @@ describe 'Creating a Snippet' do
graphql_mutation_response(:create_snippet)
end
subject { post_graphql_mutation(mutation, current_user: current_user) }
context 'when the user does not have permission' do
let(:current_user) { nil }
......@@ -39,7 +43,7 @@ describe 'Creating a Snippet' do
it 'does not create the Snippet' do
expect do
post_graphql_mutation(mutation, current_user: current_user)
subject
end.not_to change { Snippet.count }
end
......@@ -48,7 +52,7 @@ describe 'Creating a Snippet' do
it 'does not create the snippet when the user is not authorized' do
expect do
post_graphql_mutation(mutation, current_user: current_user)
subject
end.not_to change { Snippet.count }
end
end
......@@ -60,12 +64,12 @@ describe 'Creating a Snippet' do
context 'with PersonalSnippet' do
it 'creates the Snippet' do
expect do
post_graphql_mutation(mutation, current_user: current_user)
subject
end.to change { Snippet.count }.by(1)
end
it 'returns the created Snippet' do
post_graphql_mutation(mutation, current_user: current_user)
subject
expect(mutation_response['snippet']['blob']['richData']).to be_nil
expect(mutation_response['snippet']['blob']['plainData']).to match(content)
......@@ -86,12 +90,12 @@ describe 'Creating a Snippet' do
it 'creates the Snippet' do
expect do
post_graphql_mutation(mutation, current_user: current_user)
subject
end.to change { Snippet.count }.by(1)
end
it 'returns the created Snippet' do
post_graphql_mutation(mutation, current_user: current_user)
subject
expect(mutation_response['snippet']['blob']['richData']).to be_nil
expect(mutation_response['snippet']['blob']['plainData']).to match(content)
......@@ -106,7 +110,7 @@ describe 'Creating a Snippet' do
let(:project_path) { 'foobar' }
it 'returns an an error' do
post_graphql_mutation(mutation, current_user: current_user)
subject
errors = json_response['errors']
expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
......@@ -117,7 +121,7 @@ describe 'Creating a Snippet' do
it 'returns an an error' do
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::DISABLED)
post_graphql_mutation(mutation, current_user: current_user)
subject
errors = json_response['errors']
expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
......@@ -132,15 +136,41 @@ describe 'Creating a Snippet' do
it 'does not create the Snippet' do
expect do
post_graphql_mutation(mutation, current_user: current_user)
subject
end.not_to change { Snippet.count }
end
it 'does not return Snippet' do
post_graphql_mutation(mutation, current_user: current_user)
subject
expect(mutation_response['snippet']).to be_nil
end
end
context 'when there uploaded files' do
shared_examples 'expected files argument' do |file_value, expected_value|
let(:uploaded_files) { file_value }
it do
expect(::Snippets::CreateService).to receive(:new).with(nil, user, hash_including(files: expected_value))
subject
end
end
it_behaves_like 'expected files argument', nil, nil
it_behaves_like 'expected files argument', %w(foo bar), %w(foo bar)
it_behaves_like 'expected files argument', 'foo', %w(foo)
context 'when files has an invalid value' do
let(:uploaded_files) { [1] }
it 'returns an error' do
subject
expect(json_response['errors']).to be
end
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