Commit ee31658d authored by Sean McGivern's avatar Sean McGivern

Merge branch 'fj-add-files-param-snippet-create-mutation' into 'master'

Add `uploaded_files` param to Snippet create mutation

See merge request gitlab-org/gitlab!32309
parents b7a2bf25 1b52e42f
...@@ -36,6 +36,10 @@ module Mutations ...@@ -36,6 +36,10 @@ module Mutations
required: false, required: false,
description: 'The project full path the snippet is associated with' 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) def resolve(args)
project_path = args.delete(:project_path) project_path = args.delete(:project_path)
...@@ -45,9 +49,14 @@ module Mutations ...@@ -45,9 +49,14 @@ module Mutations
raise_resource_not_available_error! raise_resource_not_available_error!
end 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, service_response = ::Snippets::CreateService.new(project,
context[:current_user], context[:current_user],
args).execute args).execute
snippet = service_response.payload[:snippet] 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 { ...@@ -1334,6 +1334,11 @@ input CreateSnippetInput {
""" """
title: String! title: String!
"""
The paths to files uploaded in the snippet description
"""
uploadedFiles: [String!]
""" """
The visibility level of the snippet The visibility level of the snippet
""" """
......
...@@ -3608,6 +3608,24 @@ ...@@ -3608,6 +3608,24 @@
}, },
"defaultValue": null "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", "name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.", "description": "A unique identifier for the client performing the mutation.",
...@@ -13,6 +13,7 @@ describe 'Creating a Snippet' do ...@@ -13,6 +13,7 @@ describe 'Creating a Snippet' do
let(:file_name) { 'Initial file_name' } let(:file_name) { 'Initial file_name' }
let(:visibility_level) { 'public' } let(:visibility_level) { 'public' }
let(:project_path) { nil } let(:project_path) { nil }
let(:uploaded_files) { nil }
let(:mutation) do let(:mutation) do
variables = { variables = {
...@@ -21,7 +22,8 @@ describe 'Creating a Snippet' do ...@@ -21,7 +22,8 @@ describe 'Creating a Snippet' do
visibility_level: visibility_level, visibility_level: visibility_level,
file_name: file_name, file_name: file_name,
title: title, title: title,
project_path: project_path project_path: project_path,
uploaded_files: uploaded_files
} }
graphql_mutation(:create_snippet, variables) graphql_mutation(:create_snippet, variables)
...@@ -31,6 +33,8 @@ describe 'Creating a Snippet' do ...@@ -31,6 +33,8 @@ describe 'Creating a Snippet' do
graphql_mutation_response(:create_snippet) graphql_mutation_response(:create_snippet)
end end
subject { post_graphql_mutation(mutation, current_user: current_user) }
context 'when the user does not have permission' do context 'when the user does not have permission' do
let(:current_user) { nil } let(:current_user) { nil }
...@@ -39,7 +43,7 @@ describe 'Creating a Snippet' do ...@@ -39,7 +43,7 @@ describe 'Creating a Snippet' do
it 'does not create the Snippet' do it 'does not create the Snippet' do
expect do expect do
post_graphql_mutation(mutation, current_user: current_user) subject
end.not_to change { Snippet.count } end.not_to change { Snippet.count }
end end
...@@ -48,7 +52,7 @@ describe 'Creating a Snippet' do ...@@ -48,7 +52,7 @@ describe 'Creating a Snippet' do
it 'does not create the snippet when the user is not authorized' do it 'does not create the snippet when the user is not authorized' do
expect do expect do
post_graphql_mutation(mutation, current_user: current_user) subject
end.not_to change { Snippet.count } end.not_to change { Snippet.count }
end end
end end
...@@ -60,12 +64,12 @@ describe 'Creating a Snippet' do ...@@ -60,12 +64,12 @@ describe 'Creating a Snippet' do
context 'with PersonalSnippet' do context 'with PersonalSnippet' do
it 'creates the Snippet' do it 'creates the Snippet' do
expect do expect do
post_graphql_mutation(mutation, current_user: current_user) subject
end.to change { Snippet.count }.by(1) end.to change { Snippet.count }.by(1)
end end
it 'returns the created Snippet' do 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']['richData']).to be_nil
expect(mutation_response['snippet']['blob']['plainData']).to match(content) expect(mutation_response['snippet']['blob']['plainData']).to match(content)
...@@ -86,12 +90,12 @@ describe 'Creating a Snippet' do ...@@ -86,12 +90,12 @@ describe 'Creating a Snippet' do
it 'creates the Snippet' do it 'creates the Snippet' do
expect do expect do
post_graphql_mutation(mutation, current_user: current_user) subject
end.to change { Snippet.count }.by(1) end.to change { Snippet.count }.by(1)
end end
it 'returns the created Snippet' do 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']['richData']).to be_nil
expect(mutation_response['snippet']['blob']['plainData']).to match(content) expect(mutation_response['snippet']['blob']['plainData']).to match(content)
...@@ -106,7 +110,7 @@ describe 'Creating a Snippet' do ...@@ -106,7 +110,7 @@ describe 'Creating a Snippet' do
let(:project_path) { 'foobar' } let(:project_path) { 'foobar' }
it 'returns an an error' do it 'returns an an error' do
post_graphql_mutation(mutation, current_user: current_user) subject
errors = json_response['errors'] errors = json_response['errors']
expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR) expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
...@@ -117,7 +121,7 @@ describe 'Creating a Snippet' do ...@@ -117,7 +121,7 @@ describe 'Creating a Snippet' do
it 'returns an an error' do it 'returns an an error' do
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::DISABLED) project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::DISABLED)
post_graphql_mutation(mutation, current_user: current_user) subject
errors = json_response['errors'] errors = json_response['errors']
expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR) expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
...@@ -132,15 +136,41 @@ describe 'Creating a Snippet' do ...@@ -132,15 +136,41 @@ describe 'Creating a Snippet' do
it 'does not create the Snippet' do it 'does not create the Snippet' do
expect do expect do
post_graphql_mutation(mutation, current_user: current_user) subject
end.not_to change { Snippet.count } end.not_to change { Snippet.count }
end end
it 'does not return Snippet' do it 'does not return Snippet' do
post_graphql_mutation(mutation, current_user: current_user) subject
expect(mutation_response['snippet']).to be_nil expect(mutation_response['snippet']).to be_nil
end end
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
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