debian-mirror-gitlab/spec/requests/api/project_snippets_spec.rb

442 lines
15 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
require 'spec_helper'
2016-06-02 11:05:42 +05:30
2023-03-04 22:38:38 +05:30
RSpec.describe API::ProjectSnippets, feature_category: :source_code_management do
2020-07-28 23:09:34 +05:30
include SnippetHelpers
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project, :public) }
2020-04-08 14:13:33 +05:30
let_it_be(:project_no_snippets) { create(:project, :snippets_disabled) }
2021-01-03 14:25:43 +05:30
let_it_be(:user) { create(:user, developer_projects: [project_no_snippets]) }
let_it_be(:admin) { create(:admin, developer_projects: [project_no_snippets]) }
let_it_be(:public_snippet, reload: true) { create(:project_snippet, :public, :repository, project: project) }
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
describe "GET /projects/:project_id/snippets/:id/user_agent_detail" do
2021-01-03 14:25:43 +05:30
let_it_be(:user_agent_detail) { create(:user_agent_detail, subject: public_snippet) }
2017-09-10 17:25:29 +05:30
it 'exposes known attributes' do
2021-01-03 14:25:43 +05:30
get api("/projects/#{project.id}/snippets/#{public_snippet.id}/user_agent_detail", admin)
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-09-10 17:25:29 +05:30
expect(json_response['user_agent']).to eq(user_agent_detail.user_agent)
expect(json_response['ip_address']).to eq(user_agent_detail.ip_address)
expect(json_response['akismet_submitted']).to eq(user_agent_detail.submitted)
end
2018-03-17 18:26:18 +05:30
it 'respects project scoping' do
other_project = create(:project)
2021-01-03 14:25:43 +05:30
get api("/projects/#{other_project.id}/snippets/#{public_snippet.id}/user_agent_detail", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
it "returns unauthorized for non-admin users" do
2021-01-03 14:25:43 +05:30
get api("/projects/#{public_snippet.project.id}/snippets/#{public_snippet.id}/user_agent_detail", user)
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
end
context 'with snippets disabled' do
it_behaves_like '403 response' do
2021-01-03 14:25:43 +05:30
let(:request) { get api("/projects/#{project_no_snippets.id}/snippets/#{non_existing_record_id}/user_agent_detail", admin) }
2020-04-08 14:13:33 +05:30
end
2017-09-10 17:25:29 +05:30
end
end
2016-06-02 11:05:42 +05:30
describe 'GET /projects/:project_id/snippets/' do
2016-09-13 17:45:13 +05:30
it 'returns all snippets available to team member' do
2017-08-17 22:00:37 +05:30
project.add_developer(user)
2021-01-03 14:25:43 +05:30
2016-06-02 11:05:42 +05:30
internal_snippet = create(:project_snippet, :internal, project: project)
private_snippet = create(:project_snippet, :private, project: project)
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/snippets", user)
2016-06-02 11:05:42 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
2021-01-03 14:25:43 +05:30
expect(json_response.map { |snippet| snippet['id'] }).to contain_exactly(public_snippet.id, internal_snippet.id, private_snippet.id)
2016-09-29 09:46:39 +05:30
expect(json_response.last).to have_key('web_url')
2016-06-02 11:05:42 +05:30
end
it 'hides private snippets from regular user' do
create(:project_snippet, :private, project: project)
get api("/projects/#{project.id}/snippets/", user)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
2021-01-03 14:25:43 +05:30
expect(json_response.map { |snippet| snippet['id'] }).to contain_exactly(public_snippet.id)
2016-06-02 11:05:42 +05:30
end
2020-04-08 14:13:33 +05:30
context 'with snippets disabled' do
it_behaves_like '403 response' do
let(:request) { get api("/projects/#{project_no_snippets.id}/snippets", user) }
end
end
2016-06-02 11:05:42 +05:30
end
2017-09-10 17:25:29 +05:30
describe 'GET /projects/:project_id/snippets/:id' do
2021-01-03 14:25:43 +05:30
let(:snippet) { public_snippet }
2017-09-10 17:25:29 +05:30
it 'returns snippet json' do
get api("/projects/#{project.id}/snippets/#{snippet.id}", user)
2020-07-28 23:09:34 +05:30
aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
2017-09-10 17:25:29 +05:30
2020-07-28 23:09:34 +05:30
expect(json_response['title']).to eq(snippet.title)
expect(json_response['description']).to eq(snippet.description)
expect(json_response['file_name']).to eq(snippet.file_name_on_repo)
2023-01-13 00:05:48 +05:30
expect(json_response['files']).to eq(snippet.blobs.map { |blob| snippet_blob_file(blob) })
2020-07-28 23:09:34 +05:30
expect(json_response['ssh_url_to_repo']).to eq(snippet.ssh_url_to_repo)
expect(json_response['http_url_to_repo']).to eq(snippet.http_url_to_repo)
end
2020-04-22 19:07:51 +05:30
end
2017-09-10 17:25:29 +05:30
it 'returns 404 for invalid snippet id' do
2020-04-22 19:07:51 +05:30
get api("/projects/#{project.id}/snippets/#{non_existing_record_id}", user)
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-09-10 17:25:29 +05:30
expect(json_response['message']).to eq('404 Not found')
end
2020-04-08 14:13:33 +05:30
context 'with snippets disabled' do
it_behaves_like '403 response' do
2021-01-03 14:25:43 +05:30
let(:request) { get api("/projects/#{project_no_snippets.id}/snippets/#{non_existing_record_id}", user) }
2020-04-08 14:13:33 +05:30
end
end
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'project snippet access levels' do
let(:path) { "/projects/#{snippet.project.id}/snippets/#{snippet.id}" }
2020-07-28 23:09:34 +05:30
end
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
describe 'POST /projects/:project_id/snippets/' do
2020-10-24 23:57:45 +05:30
let(:base_params) do
2017-08-17 22:00:37 +05:30
{
2016-06-02 11:05:42 +05:30
title: 'Test Title',
2017-09-10 17:25:29 +05:30
description: 'test description',
2017-08-17 22:00:37 +05:30
visibility: 'public'
2016-06-02 11:05:42 +05:30
}
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
2020-10-24 23:57:45 +05:30
let(:file_path) { 'file_1.rb' }
let(:file_content) { 'puts "hello world"' }
let(:file_params) { { files: [{ file_path: file_path, content: file_content }] } }
2021-01-03 14:25:43 +05:30
let(:params) { base_params.merge(file_params) }
subject { post api("/projects/#{project.id}/snippets/", actor), params: params }
2020-10-24 23:57:45 +05:30
2020-04-08 14:13:33 +05:30
shared_examples 'project snippet repository actions' do
let(:snippet) { ProjectSnippet.find(json_response['id']) }
it 'commit the files to the repository' do
subject
2021-01-03 14:25:43 +05:30
aggregate_failures do
expect(snippet.repository.exists?).to be_truthy
2021-09-30 23:02:18 +05:30
blob = snippet.repository.blob_at(snippet.default_branch, file_path)
2020-04-08 14:13:33 +05:30
2021-01-03 14:25:43 +05:30
expect(blob.data).to eq file_content
end
2020-04-08 14:13:33 +05:30
end
end
2020-03-28 13:19:24 +05:30
context 'with an external user' do
2021-01-03 14:25:43 +05:30
let(:actor) { create(:user, :external) }
2020-03-28 13:19:24 +05:30
context 'that belongs to the project' do
it 'creates a new snippet' do
2021-01-03 14:25:43 +05:30
project.add_developer(actor)
subject
2020-03-28 13:19:24 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2020-03-28 13:19:24 +05:30
end
end
context 'that does not belong to the project' do
it 'does not create a new snippet' do
2021-01-03 14:25:43 +05:30
subject
2020-03-28 13:19:24 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2020-03-28 13:19:24 +05:30
end
end
end
2019-10-12 21:52:04 +05:30
context 'with a regular user' do
2021-01-03 14:25:43 +05:30
let(:actor) { user }
2019-10-12 21:52:04 +05:30
2021-01-03 14:25:43 +05:30
before_all do
2019-10-12 21:52:04 +05:30
project.add_developer(user)
2021-01-03 14:25:43 +05:30
end
before do
2019-10-12 21:52:04 +05:30
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::PRIVATE])
params['visibility'] = 'internal'
end
it 'creates a new snippet' do
2020-10-24 23:57:45 +05:30
subject
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2019-10-12 21:52:04 +05:30
snippet = ProjectSnippet.find(json_response['id'])
2020-10-24 23:57:45 +05:30
expect(snippet.content).to eq(file_content)
2019-10-12 21:52:04 +05:30
expect(snippet.description).to eq(params[:description])
expect(snippet.title).to eq(params[:title])
2020-10-24 23:57:45 +05:30
expect(snippet.file_name).to eq(file_path)
2019-10-12 21:52:04 +05:30
expect(snippet.visibility_level).to eq(Snippet::INTERNAL)
end
2020-04-08 14:13:33 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'project snippet repository actions'
2019-10-12 21:52:04 +05:30
end
2020-10-24 23:57:45 +05:30
context 'with an admin' do
2021-01-03 14:25:43 +05:30
let(:actor) { admin }
2016-06-02 11:05:42 +05:30
2020-10-24 23:57:45 +05:30
it 'creates a new snippet' do
subject
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
expect(response).to have_gitlab_http_status(:created)
snippet = ProjectSnippet.find(json_response['id'])
expect(snippet.content).to eq(file_content)
expect(snippet.description).to eq(params[:description])
expect(snippet.title).to eq(params[:title])
expect(snippet.file_name).to eq(file_path)
expect(snippet.visibility_level).to eq(Snippet::PUBLIC)
end
2020-04-08 14:13:33 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'project snippet repository actions'
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
it 'returns 400 for missing parameters' do
params.delete(:title)
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
subject
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
end
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'snippet creation with files parameter'
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'snippet creation without files parameter'
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
it 'returns 400 if title is blank' do
params[:title] = ''
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
subject
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eq 'title is empty'
end
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
context 'when save fails because the repository could not be created' do
2021-01-03 14:25:43 +05:30
let(:actor) { admin }
2020-05-24 23:13:21 +05:30
before do
allow_next_instance_of(Snippets::CreateService) do |instance|
allow(instance).to receive(:create_repository).and_raise(Snippets::CreateService::CreateRepositoryError)
end
end
2018-11-20 20:47:30 +05:30
2020-05-24 23:13:21 +05:30
it 'returns 400' do
2021-01-03 14:25:43 +05:30
subject
2018-11-20 20:47:30 +05:30
2020-05-24 23:13:21 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
end
2018-11-20 20:47:30 +05:30
end
2017-08-17 22:00:37 +05:30
context 'when the snippet is spam' do
2021-01-03 14:25:43 +05:30
let(:actor) { user }
2017-08-17 22:00:37 +05:30
before do
2020-03-13 15:44:24 +05:30
allow_next_instance_of(Spam::AkismetService) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive(:spam?).and_return(true)
end
2021-01-03 14:25:43 +05:30
project.add_developer(user)
2017-08-17 22:00:37 +05:30
end
context 'when the snippet is private' do
it 'creates the snippet' do
2021-01-03 14:25:43 +05:30
params['visibility'] = 'private'
expect { subject }.to change { Snippet.count }.by(1)
2017-08-17 22:00:37 +05:30
end
end
context 'when the snippet is public' do
2021-01-03 14:25:43 +05:30
before do
params['visibility'] = 'public'
end
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
it 'rejects the snippet' do
expect { subject }.not_to change { Snippet.count }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2022-05-07 20:08:51 +05:30
expect(json_response['message']['error']).to match(/snippet has been recognized as spam/)
2017-08-17 22:00:37 +05:30
end
it 'creates a spam log' do
2021-01-03 14:25:43 +05:30
expect { subject }
2019-12-21 20:55:43 +05:30
.to log_spam(title: 'Test Title', user_id: user.id, noteable_type: 'ProjectSnippet')
2017-08-17 22:00:37 +05:30
end
end
2016-06-02 11:05:42 +05:30
end
2020-04-08 14:13:33 +05:30
context 'with snippets disabled' do
it_behaves_like '403 response' do
let(:request) { post api("/projects/#{project_no_snippets.id}/snippets", user), params: params }
end
end
2016-06-02 11:05:42 +05:30
end
describe 'PUT /projects/:project_id/snippets/:id/' do
2017-08-17 22:00:37 +05:30
let(:visibility_level) { Snippet::PUBLIC }
2020-04-08 14:13:33 +05:30
let(:snippet) { create(:project_snippet, :repository, author: admin, visibility_level: visibility_level, project: project) }
2017-08-17 22:00:37 +05:30
2020-11-24 15:15:51 +05:30
it_behaves_like 'snippet file updates'
it_behaves_like 'snippet non-file updates'
it_behaves_like 'snippet individual non-file updates'
it_behaves_like 'invalid snippet updates'
2018-11-20 20:47:30 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'update with repository actions' do
let(:snippet_without_repo) { create(:project_snippet, author: admin, project: project, visibility_level: visibility_level) }
end
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
context 'when the snippet is spam' do
2017-08-17 22:00:37 +05:30
before do
2020-03-13 15:44:24 +05:30
allow_next_instance_of(Spam::AkismetService) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive(:spam?).and_return(true)
end
2017-08-17 22:00:37 +05:30
end
context 'when the snippet is private' do
let(:visibility_level) { Snippet::PRIVATE }
it 'creates the snippet' do
2020-04-08 14:13:33 +05:30
expect { update_snippet(params: { title: 'Foo' }) }
2017-09-10 17:25:29 +05:30
.to change { snippet.reload.title }.to('Foo')
2017-08-17 22:00:37 +05:30
end
end
context 'when the snippet is public' do
let(:visibility_level) { Snippet::PUBLIC }
it 'rejects the snippet' do
2020-04-08 14:13:33 +05:30
expect { update_snippet(params: { title: 'Foo' }) }
2017-09-10 17:25:29 +05:30
.not_to change { snippet.reload.title }
2017-08-17 22:00:37 +05:30
end
it 'creates a spam log' do
2020-04-08 14:13:33 +05:30
expect { update_snippet(params: { title: 'Foo' }) }
2019-12-21 20:55:43 +05:30
.to log_spam(title: 'Foo', user_id: admin.id, noteable_type: 'ProjectSnippet')
2017-08-17 22:00:37 +05:30
end
end
context 'when the private snippet is made public' do
let(:visibility_level) { Snippet::PRIVATE }
it 'rejects the snippet' do
2020-04-08 14:13:33 +05:30
expect { update_snippet(params: { title: 'Foo', visibility: 'public' }) }
2017-09-10 17:25:29 +05:30
.not_to change { snippet.reload.title }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2022-05-07 20:08:51 +05:30
expect(json_response['message']['error']).to match(/snippet has been recognized as spam/)
2017-08-17 22:00:37 +05:30
end
it 'creates a spam log' do
2020-04-08 14:13:33 +05:30
expect { update_snippet(params: { title: 'Foo', visibility: 'public' }) }
2019-12-21 20:55:43 +05:30
.to log_spam(title: 'Foo', user_id: admin.id, noteable_type: 'ProjectSnippet')
2017-08-17 22:00:37 +05:30
end
end
end
2020-04-08 14:13:33 +05:30
context 'with snippets disabled' do
it_behaves_like '403 response' do
2021-01-03 14:25:43 +05:30
let(:request) { put api("/projects/#{project_no_snippets.id}/snippets/#{non_existing_record_id}", admin), params: { description: 'foo' } }
2020-04-08 14:13:33 +05:30
end
end
def update_snippet(snippet_id: snippet.id, params: {})
put api("/projects/#{snippet.project.id}/snippets/#{snippet_id}", admin), params: params
end
2016-06-02 11:05:42 +05:30
end
describe 'DELETE /projects/:project_id/snippets/:id/' do
2021-01-03 14:25:43 +05:30
let_it_be(:snippet, refind: true) { public_snippet }
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
it 'deletes snippet' do
delete api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2017-08-17 22:00:37 +05:30
end
it 'returns 404 for invalid snippet id' do
2020-04-22 19:07:51 +05:30
delete api("/projects/#{snippet.project.id}/snippets/#{non_existing_record_id}", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
expect(json_response['message']).to eq('404 Snippet Not Found')
2016-06-02 11:05:42 +05:30
end
2018-03-17 18:26:18 +05:30
it_behaves_like '412 response' do
let(:request) { api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin) }
end
2020-04-08 14:13:33 +05:30
context 'with snippets disabled' do
it_behaves_like '403 response' do
2021-01-03 14:25:43 +05:30
let(:request) { delete api("/projects/#{project_no_snippets.id}/snippets/#{non_existing_record_id}", admin) }
2020-04-08 14:13:33 +05:30
end
end
2016-06-02 11:05:42 +05:30
end
describe 'GET /projects/:project_id/snippets/:id/raw' do
2021-01-03 14:25:43 +05:30
let_it_be(:snippet) { create(:project_snippet, :repository, :public, author: admin, project: project) }
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
it 'returns raw text' do
2016-06-02 11:05:42 +05:30
get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-07-28 23:09:34 +05:30
expect(response.media_type).to eq 'text/plain'
2021-12-11 22:18:48 +05:30
expect(response.parsed_body).to be_empty
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns 404 for invalid snippet id' do
2020-04-22 19:07:51 +05:30
get api("/projects/#{snippet.project.id}/snippets/#{non_existing_record_id}/raw", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
expect(json_response['message']).to eq('404 Snippet Not Found')
end
2020-04-08 14:13:33 +05:30
2021-01-03 14:25:43 +05:30
it_behaves_like 'project snippet access levels' do
let(:path) { "/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw" }
end
2020-04-08 14:13:33 +05:30
context 'with snippets disabled' do
it_behaves_like '403 response' do
2021-01-03 14:25:43 +05:30
let(:request) { get api("/projects/#{project_no_snippets.id}/snippets/#{non_existing_record_id}/raw", admin) }
2020-04-08 14:13:33 +05:30
end
end
2020-05-24 23:13:21 +05:30
it_behaves_like 'snippet blob content' do
let_it_be(:snippet_with_empty_repo) { create(:project_snippet, :empty_repo, author: admin, project: project) }
subject { get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", snippet.author) }
end
2016-06-02 11:05:42 +05:30
end
2020-07-28 23:09:34 +05:30
describe 'GET /projects/:project_id/snippets/:id/files/:ref/:file_path/raw' do
let_it_be(:snippet) { create(:project_snippet, :repository, author: admin, project: project) }
it_behaves_like 'raw snippet files' do
let(:api_path) { "/projects/#{snippet.project.id}/snippets/#{snippet_id}/files/#{ref}/#{file_path}/raw" }
end
2021-01-03 14:25:43 +05:30
it_behaves_like 'project snippet access levels' do
let(:path) { "/projects/#{snippet.project.id}/snippets/#{snippet.id}/files/master/%2Egitattributes/raw" }
end
2020-07-28 23:09:34 +05:30
end
2016-06-02 11:05:42 +05:30
end