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 'Creating a Snippet' do
|
2020-01-01 13:55:28 +05:30
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
let(:description) { 'Initial description' }
|
|
|
|
let(:title) { 'Initial title' }
|
|
|
|
let(:visibility_level) { 'public' }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:action) { :create }
|
2022-08-27 11:52:29 +05:30
|
|
|
let(:file_1) { { filePath: 'example_file1', content: 'This is the example file 1' } }
|
|
|
|
let(:file_2) { { filePath: 'example_file2', content: 'This is the example file 2' } }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:actions) { [{ action: action }.merge(file_1), { action: action }.merge(file_2)] }
|
2020-01-01 13:55:28 +05:30
|
|
|
let(:project_path) { nil }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:uploaded_files) { nil }
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:mutation_vars) do
|
|
|
|
{
|
2020-01-01 13:55:28 +05:30
|
|
|
description: description,
|
|
|
|
visibility_level: visibility_level,
|
|
|
|
title: title,
|
2020-05-24 23:13:21 +05:30
|
|
|
project_path: project_path,
|
2020-11-24 15:15:51 +05:30
|
|
|
uploaded_files: uploaded_files,
|
|
|
|
blob_actions: actions
|
2021-09-30 23:02:18 +05:30
|
|
|
}
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:mutation) do
|
|
|
|
graphql_mutation(:create_snippet, mutation_vars)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def mutation_response
|
|
|
|
graphql_mutation_response(:create_snippet)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
subject { post_graphql_mutation(mutation, current_user: current_user) }
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
context 'when the user does not have permission' do
|
|
|
|
let(:current_user) { nil }
|
|
|
|
|
|
|
|
it_behaves_like 'a mutation that returns top-level errors',
|
|
|
|
errors: [Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR]
|
|
|
|
|
|
|
|
it 'does not create the Snippet' do
|
|
|
|
expect do
|
2020-05-24 23:13:21 +05:30
|
|
|
subject
|
2020-01-01 13:55:28 +05:30
|
|
|
end.not_to change { Snippet.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not authorized in the project' do
|
|
|
|
let(:project_path) { project.full_path }
|
|
|
|
|
|
|
|
it 'does not create the snippet when the user is not authorized' do
|
|
|
|
expect do
|
2020-05-24 23:13:21 +05:30
|
|
|
subject
|
2020-01-01 13:55:28 +05:30
|
|
|
end.not_to change { Snippet.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user has permission' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
shared_examples 'does not create snippet' do
|
|
|
|
it 'does not create the Snippet' do
|
2020-01-01 13:55:28 +05:30
|
|
|
expect do
|
2020-05-24 23:13:21 +05:30
|
|
|
subject
|
2020-11-24 15:15:51 +05:30
|
|
|
end.not_to change { Snippet.count }
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'does not return Snippet' do
|
2020-05-24 23:13:21 +05:30
|
|
|
subject
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(mutation_response['snippet']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'creates snippet' do
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'returns the created Snippet', :aggregate_failures do
|
2020-11-24 15:15:51 +05:30
|
|
|
expect do
|
|
|
|
subject
|
|
|
|
end.to change { Snippet.count }.by(1)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
snippet = Snippet.last
|
2021-09-30 23:02:18 +05:30
|
|
|
created_file_1 = snippet.repository.blob_at(snippet.default_branch, file_1[:filePath])
|
|
|
|
created_file_2 = snippet.repository.blob_at(snippet.default_branch, file_2[:filePath])
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
expect(created_file_1.data).to match(file_1[:content])
|
|
|
|
expect(created_file_2.data).to match(file_2[:content])
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(mutation_response['snippet']['title']).to eq(title)
|
|
|
|
expect(mutation_response['snippet']['description']).to eq(description)
|
|
|
|
expect(mutation_response['snippet']['visibilityLevel']).to eq(visibility_level)
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
context 'when action is invalid' do
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:file_1) { { filePath: 'example_file1' } }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a mutation that returns errors in the response', errors: ['Snippet actions have invalid data']
|
|
|
|
it_behaves_like 'does not create snippet'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'snippet edit usage data counters'
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a mutation which can mutate a spammable' do
|
2021-01-03 14:25:43 +05:30
|
|
|
let(:service) { Snippets::CreateService }
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
it_behaves_like 'creates snippet'
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:project_path) { project.full_path }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(current_user)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'creates snippet'
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
context 'when the project path is invalid' do
|
|
|
|
let(:project_path) { 'foobar' }
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it_behaves_like 'a mutation that returns top-level errors',
|
|
|
|
errors: [Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR]
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the feature is disabled' do
|
2020-06-23 00:09:42 +05:30
|
|
|
before do
|
2020-01-01 13:55:28 +05:30
|
|
|
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::DISABLED)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a mutation that returns top-level errors',
|
|
|
|
errors: [Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR]
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it_behaves_like 'snippet edit usage data counters'
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when there are ActiveRecord validation errors' do
|
|
|
|
let(:title) { '' }
|
|
|
|
|
|
|
|
it_behaves_like 'a mutation that returns errors in the response', errors: ["Title can't be blank"]
|
|
|
|
it_behaves_like 'does not create snippet'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there non ActiveRecord errors' do
|
2022-08-27 11:52:29 +05:30
|
|
|
let(:file_1) { { filePath: 'invalid://file/path', content: 'foobar' } }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a mutation that returns errors in the response', errors: ['Repository Error creating the snippet - Invalid file name']
|
|
|
|
it_behaves_like 'does not create snippet'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are uploaded files' do
|
2020-05-24 23:13:21 +05:30
|
|
|
shared_examples 'expected files argument' do |file_value, expected_value|
|
|
|
|
let(:uploaded_files) { file_value }
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:snippet) { build(:snippet) }
|
|
|
|
let(:creation_response) do
|
|
|
|
::ServiceResponse.error(message: 'urk', payload: { snippet: snippet })
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
it do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(::Snippets::CreateService).to receive(:new)
|
2021-09-30 23:02:18 +05:30
|
|
|
.with(project: nil, current_user: user, params: hash_including(files: expected_value), spam_params: instance_of(::Spam::SpamParams))
|
2021-02-22 17:27:13 +05:30
|
|
|
.and_return(double(execute: creation_response))
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
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
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
it_behaves_like 'has spam protection' do
|
|
|
|
let(:mutation_class) { ::Mutations::Snippets::Create }
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|