debian-mirror-gitlab/spec/requests/api/graphql/mutations/snippets/update_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

208 lines
6.4 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 'Updating a Snippet' do
2020-01-01 13:55:28 +05:30
include GraphqlHelpers
2022-08-13 15:12:31 +05:30
include SessionHelpers
2020-01-01 13:55:28 +05:30
let_it_be(:original_content) { 'Initial content' }
let_it_be(:original_description) { 'Initial description' }
let_it_be(:original_title) { 'Initial title' }
let_it_be(:original_file_name) { 'Initial file_name' }
2021-09-30 23:02:18 +05:30
2020-01-01 13:55:28 +05:30
let(:updated_content) { 'Updated content' }
let(:updated_description) { 'Updated description' }
let(:updated_title) { 'Updated_title' }
let(:current_user) { snippet.author }
2020-11-24 15:15:51 +05:30
let(:updated_file) { 'CHANGELOG' }
let(:deleted_file) { 'README' }
2020-05-24 23:13:21 +05:30
let(:snippet_gid) { GitlabSchema.id_from_object(snippet).to_s }
2020-07-28 23:09:34 +05:30
let(:mutation_vars) do
{
2020-05-24 23:13:21 +05:30
id: snippet_gid,
2020-01-01 13:55:28 +05:30
description: updated_description,
visibility_level: 'public',
2020-11-24 15:15:51 +05:30
title: updated_title,
blob_actions: [
{ action: :update, filePath: updated_file, content: updated_content },
{ action: :delete, filePath: deleted_file }
]
2021-09-30 23:02:18 +05:30
}
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
2020-07-28 23:09:34 +05:30
let(:mutation) do
graphql_mutation(:update_snippet, mutation_vars)
2020-01-01 13:55:28 +05:30
end
def mutation_response
graphql_mutation_response(:update_snippet)
end
2021-01-03 14:25:43 +05:30
subject { post_graphql_mutation(mutation, current_user: current_user) }
2020-01-01 13:55:28 +05:30
shared_examples 'graphql update actions' do
context 'when the user does not have permission' do
let(:current_user) { create(:user) }
it_behaves_like 'a mutation that returns top-level errors',
errors: [Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR]
it 'does not update the Snippet' do
expect do
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
end.not_to change { snippet.reload }
end
end
context 'when the user has permission' do
2020-11-24 15:15:51 +05:30
it 'updates the snippet record' do
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
expect(snippet.reload.title).to eq(updated_title)
end
2020-11-24 15:15:51 +05:30
it 'updates the Snippet' do
blob_to_update = blob_at(updated_file)
blob_to_delete = blob_at(deleted_file)
expect(blob_to_update.data).not_to eq updated_content
expect(blob_to_delete).to be_present
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
2020-11-24 15:15:51 +05:30
blob_to_update = blob_at(updated_file)
blob_to_delete = blob_at(deleted_file)
aggregate_failures do
expect(blob_to_update.data).to eq updated_content
expect(blob_to_delete).to be_nil
expect(mutation_response['snippet']['title']).to eq(updated_title)
expect(mutation_response['snippet']['description']).to eq(updated_description)
expect(mutation_response['snippet']['visibilityLevel']).to eq('public')
end
2020-01-01 13:55:28 +05:30
end
context 'when there are ActiveRecord validation errors' do
let(:updated_title) { '' }
it_behaves_like 'a mutation that returns errors in the response', errors: ["Title can't be blank"]
it 'does not update the Snippet' do
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
expect(snippet.reload.title).to eq(original_title)
end
it 'returns the Snippet with its original values' do
2020-11-24 15:15:51 +05:30
blob_to_update = blob_at(updated_file)
blob_to_delete = blob_at(deleted_file)
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
2020-11-24 15:15:51 +05:30
aggregate_failures do
expect(blob_at(updated_file).data).to eq blob_to_update.data
expect(blob_at(deleted_file).data).to eq blob_to_delete.data
expect(mutation_response['snippet']['title']).to eq(original_title)
expect(mutation_response['snippet']['description']).to eq(original_description)
expect(mutation_response['snippet']['visibilityLevel']).to eq('private')
end
2020-01-01 13:55:28 +05:30
end
2021-03-11 19:13:27 +05:30
end
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it_behaves_like 'a mutation which can mutate a spammable' do
let(:service) { Snippets::UpdateService }
2020-11-24 15:15:51 +05:30
end
def blob_at(filename)
snippet.repository.blob_at('HEAD', filename)
end
2020-01-01 13:55:28 +05:30
end
end
describe 'PersonalSnippet' do
2020-05-24 23:13:21 +05:30
let(:snippet) do
create(:personal_snippet,
:private,
2020-11-24 15:15:51 +05:30
:repository,
2020-05-24 23:13:21 +05:30
file_name: original_file_name,
title: original_title,
content: original_content,
description: original_description)
2020-01-01 13:55:28 +05:30
end
2020-05-24 23:13:21 +05:30
it_behaves_like 'graphql update actions'
it_behaves_like 'when the snippet is not found'
2020-11-24 15:15:51 +05:30
it_behaves_like 'snippet edit usage data counters'
2021-04-29 21:17:54 +05:30
it_behaves_like 'has spam protection' do
let(:mutation_class) { ::Mutations::Snippets::Update }
end
2020-01-01 13:55:28 +05:30
end
describe 'ProjectSnippet' do
let_it_be(:project) { create(:project, :private) }
2021-09-30 23:02:18 +05:30
2020-04-08 14:13:33 +05:30
let(:snippet) do
2020-01-01 13:55:28 +05:30
create(:project_snippet,
:private,
2020-11-24 15:15:51 +05:30
:repository,
2020-01-01 13:55:28 +05:30
project: project,
author: create(:user),
file_name: original_file_name,
title: original_title,
content: original_content,
description: original_description)
end
context 'when the author is not a member of the project' do
it 'returns an an error' do
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
errors = json_response['errors']
expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
end
end
2022-08-13 15:12:31 +05:30
context 'when the author is a member of the project', :snowplow do
2020-01-01 13:55:28 +05:30
before do
project.add_developer(current_user)
end
it_behaves_like 'graphql update actions'
context 'when the snippet project feature is disabled' do
it 'returns an an error' do
project.project_feature.update_attribute(:snippets_access_level, ProjectFeature::DISABLED)
2021-01-03 14:25:43 +05:30
subject
2020-01-01 13:55:28 +05:30
errors = json_response['errors']
expect(errors.first['message']).to eq(Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR)
end
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'
2021-04-29 21:17:54 +05:30
it_behaves_like 'has spam protection' do
let(:mutation_class) { ::Mutations::Snippets::Update }
end
2022-08-13 15:12:31 +05:30
context 'when not sessionless', :clean_gitlab_redis_sessions do
before do
stub_session('warden.user.user.key' => [[current_user.id], current_user.authenticatable_salt])
end
it_behaves_like 'Snowplow event tracking' do
let(:user) { current_user }
let(:namespace) { project.namespace }
let(:category) { 'ide_edit' }
let(:action) { 'g_edit_by_snippet_ide' }
let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
end
end
2020-07-28 23:09:34 +05:30
end
2020-11-24 15:15:51 +05:30
it_behaves_like 'when the snippet is not found'
2020-07-28 23:09:34 +05:30
end
2020-01-01 13:55:28 +05:30
end