2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe API::Snippets do
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe 'GET /snippets/' do
|
|
|
|
it 'returns snippets available' do
|
|
|
|
public_snippet = create(:personal_snippet, :public, author: user)
|
|
|
|
private_snippet = create(:personal_snippet, :private, author: user)
|
|
|
|
internal_snippet = create(:personal_snippet, :internal, author: user)
|
|
|
|
|
|
|
|
get api("/snippets/", user)
|
|
|
|
|
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
|
|
|
|
expect(json_response.map { |snippet| snippet['id']} ).to contain_exactly(
|
|
|
|
public_snippet.id,
|
|
|
|
internal_snippet.id,
|
|
|
|
private_snippet.id)
|
|
|
|
expect(json_response.last).to have_key('web_url')
|
|
|
|
expect(json_response.last).to have_key('raw_url')
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(json_response.last).to have_key('visibility')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'hides private snippets from regular user' do
|
|
|
|
create(:personal_snippet, :private)
|
|
|
|
|
|
|
|
get api("/snippets/", user)
|
|
|
|
|
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
|
|
|
|
expect(json_response.size).to eq(0)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'returns 404 for non-authenticated' do
|
|
|
|
create(:personal_snippet, :internal)
|
|
|
|
|
|
|
|
get api("/snippets/")
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return snippets related to a project with disable feature visibility' do
|
|
|
|
project = create(:project)
|
|
|
|
create(:project_member, project: project, user: user)
|
|
|
|
public_snippet = create(:personal_snippet, :public, author: user, project: project)
|
|
|
|
project.project_feature.update_attribute(:snippets_access_level, 0)
|
|
|
|
|
|
|
|
get api("/snippets/", user)
|
|
|
|
|
|
|
|
json_response.each do |snippet|
|
|
|
|
expect(snippet["id"]).not_to eq(public_snippet.id)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /snippets/public' do
|
|
|
|
let!(:other_user) { create(:user) }
|
|
|
|
let!(:public_snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
let!(:private_snippet) { create(:personal_snippet, :private, author: user) }
|
|
|
|
let!(:internal_snippet) { create(:personal_snippet, :internal, author: user) }
|
|
|
|
let!(:public_snippet_other) { create(:personal_snippet, :public, author: other_user) }
|
|
|
|
let!(:private_snippet_other) { create(:personal_snippet, :private, author: other_user) }
|
|
|
|
let!(:internal_snippet_other) { create(:personal_snippet, :internal, author: other_user) }
|
2020-01-01 13:55:28 +05:30
|
|
|
let!(:public_snippet_project) { create(:project_snippet, :public, author: user) }
|
|
|
|
let!(:private_snippet_project) { create(:project_snippet, :private, author: user) }
|
|
|
|
let!(:internal_snippet_project) { create(:project_snippet, :internal, author: user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns all snippets with public visibility from all users' do
|
|
|
|
get api("/snippets/public", user)
|
|
|
|
|
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
|
|
|
|
expect(json_response.map { |snippet| snippet['id']} ).to contain_exactly(
|
|
|
|
public_snippet.id,
|
|
|
|
public_snippet_other.id)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(json_response.map { |snippet| snippet['web_url']} ).to contain_exactly(
|
2017-08-17 22:00:37 +05:30
|
|
|
"http://localhost/snippets/#{public_snippet.id}",
|
|
|
|
"http://localhost/snippets/#{public_snippet_other.id}")
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(json_response.map { |snippet| snippet['raw_url']} ).to contain_exactly(
|
2017-08-17 22:00:37 +05:30
|
|
|
"http://localhost/snippets/#{public_snippet.id}/raw",
|
|
|
|
"http://localhost/snippets/#{public_snippet_other.id}/raw")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /snippets/:id/raw' do
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:author) { create(:user) }
|
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :private, author: author) }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'requires authentication' do
|
|
|
|
get api("/snippets/#{snippet.id}", nil)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns raw text' do
|
2019-07-07 11:18:12 +05:30
|
|
|
get api("/snippets/#{snippet.id}/raw", author)
|
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.content_type).to eq 'text/plain'
|
|
|
|
expect(response.body).to eq(snippet.content)
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it 'forces attachment content disposition' do
|
2019-07-07 11:18:12 +05:30
|
|
|
get api("/snippets/#{snippet.id}/raw", author)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
expect(headers['Content-Disposition']).to match(/^attachment/)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns 404 for invalid snippet id' do
|
2019-07-07 11:18:12 +05:30
|
|
|
snippet.destroy
|
|
|
|
|
|
|
|
get api("/snippets/#{snippet.id}/raw", author)
|
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
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'hides private snippets from ordinary users' do
|
|
|
|
get api("/snippets/#{snippet.id}/raw", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows internal snippets to ordinary users' do
|
|
|
|
internal_snippet = create(:personal_snippet, :internal, author: author)
|
|
|
|
|
|
|
|
get api("/snippets/#{internal_snippet.id}/raw", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe 'GET /snippets/:id' do
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:admin) { create(:user, :admin) }
|
|
|
|
let_it_be(:author) { create(:user) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let_it_be(:private_snippet) { create(:personal_snippet, :repository, :private, author: author) }
|
|
|
|
let_it_be(:internal_snippet) { create(:personal_snippet, :repository, :internal, author: author) }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'requires authentication' do
|
|
|
|
get api("/snippets/#{private_snippet.id}", nil)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'returns snippet json' do
|
2019-07-07 11:18:12 +05:30
|
|
|
get api("/snippets/#{private_snippet.id}", author)
|
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
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(json_response['title']).to eq(private_snippet.title)
|
|
|
|
expect(json_response['description']).to eq(private_snippet.description)
|
|
|
|
expect(json_response['file_name']).to eq(private_snippet.file_name)
|
|
|
|
expect(json_response['visibility']).to eq(private_snippet.visibility)
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(json_response['ssh_url_to_repo']).to eq(private_snippet.ssh_url_to_repo)
|
|
|
|
expect(json_response['http_url_to_repo']).to eq(private_snippet.http_url_to_repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature flag :version_snippets is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(version_snippets: false)
|
|
|
|
|
|
|
|
get api("/snippets/#{private_snippet.id}", author)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'snippet response without repository URLs'
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows private snippets to an admin' do
|
|
|
|
get api("/snippets/#{private_snippet.id}", admin)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'hides private snippets from an ordinary user' do
|
|
|
|
get api("/snippets/#{private_snippet.id}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows internal snippets to an ordinary user' do
|
|
|
|
get api("/snippets/#{internal_snippet.id}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 for invalid snippet id' do
|
2019-07-07 11:18:12 +05:30
|
|
|
private_snippet.destroy
|
|
|
|
|
|
|
|
get api("/snippets/#{private_snippet.id}", admin)
|
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)
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(json_response['message']).to eq('404 Snippet Not Found')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'POST /snippets/' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
title: 'Test Title',
|
|
|
|
file_name: 'test.rb',
|
2017-09-10 17:25:29 +05:30
|
|
|
description: 'test description',
|
2017-08-17 22:00:37 +05:30
|
|
|
content: 'puts "hello world"',
|
|
|
|
visibility: 'public'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
shared_examples 'snippet creation' do
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:snippet) { Snippet.find(json_response["id"]) }
|
|
|
|
|
|
|
|
subject { post api("/snippets/", user), params: params }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'creates a new snippet' do
|
|
|
|
expect do
|
2020-04-08 14:13:33 +05:30
|
|
|
subject
|
2019-10-12 21:52:04 +05:30
|
|
|
end.to change { PersonalSnippet.count }.by(1)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(json_response['title']).to eq(params[:title])
|
|
|
|
expect(json_response['description']).to eq(params[:description])
|
|
|
|
expect(json_response['file_name']).to eq(params[:file_name])
|
|
|
|
expect(json_response['visibility']).to eq(params[:visibility])
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
it 'creates repository' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.repository.exists?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'commit the files to the repository' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
blob = snippet.repository.blob_at('master', params[:file_name])
|
|
|
|
|
|
|
|
expect(blob.data).to eq params[:content]
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature flag :version_snippets is disabled' do
|
|
|
|
it 'does not create snippet repository' do
|
|
|
|
stub_feature_flags(version_snippets: false)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject
|
|
|
|
end.to change { PersonalSnippet.count }.by(1)
|
|
|
|
|
|
|
|
expect(snippet.repository_exists?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with restricted visibility settings' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels:
|
|
|
|
[Gitlab::VisibilityLevel::INTERNAL,
|
|
|
|
Gitlab::VisibilityLevel::PRIVATE])
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'snippet creation'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'snippet creation'
|
|
|
|
|
2020-03-28 13:19:24 +05:30
|
|
|
context 'with an external user' do
|
|
|
|
let(:user) { create(:user, :external) }
|
|
|
|
|
|
|
|
it 'does not create a new snippet' do
|
|
|
|
post api("/snippets/", user), params: params
|
|
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns 400 for missing parameters' do
|
|
|
|
params.delete(:title)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
post api("/snippets/", user), params: params
|
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)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet is spam' do
|
|
|
|
def create_snippet(snippet_params = {})
|
2019-02-15 15:39:39 +05:30
|
|
|
post api('/snippets', user), params: params.merge(snippet_params)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
it 'creates the snippet' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect { create_snippet(visibility: 'private') }
|
|
|
|
.to change { Snippet.count }.by(1)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet is public' do
|
|
|
|
it 'rejects the shippet' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect { create_snippet(visibility: 'public') }
|
|
|
|
.not_to change { Snippet.count }
|
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)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['message']).to eq({ "error" => "Spam detected" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a spam log' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect { create_snippet(visibility: 'public') }
|
2019-12-21 20:55:43 +05:30
|
|
|
.to log_spam(title: 'Test Title', user_id: user.id, noteable_type: 'PersonalSnippet')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /snippets/:id' do
|
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
let(:other_user) { create(:user) }
|
|
|
|
let(:snippet) do
|
2020-04-08 14:13:33 +05:30
|
|
|
create(:personal_snippet, :repository, author: user, visibility_level: visibility_level)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
shared_examples 'snippet updates' do
|
|
|
|
it 'updates a snippet' do
|
|
|
|
new_content = 'New content'
|
|
|
|
new_description = 'New description'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
update_snippet(params: { content: new_content, description: new_description, visibility: 'internal' })
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-10-12 21:52:04 +05:30
|
|
|
snippet.reload
|
|
|
|
expect(snippet.content).to eq(new_content)
|
|
|
|
expect(snippet.description).to eq(new_description)
|
|
|
|
expect(snippet.visibility).to eq('internal')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'with restricted visibility settings' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels:
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC,
|
|
|
|
Gitlab::VisibilityLevel::PRIVATE])
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'snippet updates'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'snippet updates'
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns 404 for invalid snippet id' do
|
2020-04-22 19:07:51 +05:30
|
|
|
update_snippet(snippet_id: non_existing_record_id, params: { title: 'Foo' })
|
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
|
|
|
|
|
|
|
|
it "returns 404 for another user's snippet" do
|
2020-04-08 14:13:33 +05:30
|
|
|
update_snippet(requester: other_user, params: { title: 'foobar' })
|
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
|
|
|
|
|
|
|
|
it 'returns 400 for missing parameters' do
|
2020-04-08 14:13:33 +05:30
|
|
|
update_snippet
|
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)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it_behaves_like 'update with repository actions' do
|
|
|
|
let(:snippet_without_repo) { create(:personal_snippet, author: user, 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 'updates 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 shippet' 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
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['message']).to eq({ "error" => "Spam detected" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a spam log' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect { update_snippet(params: { title: 'Foo' }) }.to log_spam(title: 'Foo', user_id: user.id, noteable_type: 'PersonalSnippet')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a 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
|
|
|
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: user.id, noteable_type: 'PersonalSnippet')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
def update_snippet(snippet_id: snippet.id, params: {}, requester: user)
|
|
|
|
put api("/snippets/#{snippet_id}", requester), params: params
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /snippets/:id' do
|
|
|
|
let!(:public_snippet) { create(:personal_snippet, :public, author: user) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'deletes snippet' do
|
|
|
|
expect do
|
|
|
|
delete api("/snippets/#{public_snippet.id}", user)
|
|
|
|
|
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.to change { PersonalSnippet.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 for invalid snippet id' do
|
2020-04-22 19:07:51 +05:30
|
|
|
delete api("/snippets/#{non_existing_record_id}", user)
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/snippets/#{public_snippet.id}", user) }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
describe "GET /snippets/:id/user_agent_detail" do
|
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
let(:snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
let!(:user_agent_detail) { create(:user_agent_detail, subject: snippet) }
|
|
|
|
|
|
|
|
it 'exposes known attributes' do
|
|
|
|
get api("/snippets/#{snippet.id}/user_agent_detail", admin)
|
|
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
it "returns unauthorized for non-admin users" do
|
2017-09-10 17:25:29 +05:30
|
|
|
get api("/snippets/#{snippet.id}/user_agent_detail", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|