debian-mirror-gitlab/spec/controllers/projects/snippets_controller_spec.rb

332 lines
8.7 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::SnippetsController do
2020-04-08 14:13:33 +05:30
include Gitlab::Routing
let_it_be(:user) { create(:user) }
2020-11-24 15:15:51 +05:30
let_it_be(:other_user) { create(:user) }
let_it_be(:project) { create(:project_empty_repo, :public) }
2016-06-02 11:05:42 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2020-11-24 15:15:51 +05:30
project.add_developer(other_user)
2016-06-02 11:05:42 +05:30
end
describe 'GET #index' do
2020-07-28 23:09:34 +05:30
let(:base_params) do
{
namespace_id: project.namespace,
project_id: project
}
end
subject { get :index, params: base_params }
2019-12-04 20:38:33 +05:30
it_behaves_like 'paginated collection' do
let(:collection) { project.snippets }
2020-07-28 23:09:34 +05:30
let(:params) { base_params }
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
before do
create(:project_snippet, :public, project: project, author: user)
2017-08-17 22:00:37 +05:30
end
end
2020-03-13 15:44:24 +05:30
it 'fetches snippet counts via the snippet count service' do
service = double(:count_service, execute: {})
expect(Snippets::CountService)
.to receive(:new).with(nil, project: project)
.and_return(service)
2020-07-28 23:09:34 +05:30
subject
end
it_behaves_like 'snippets sort order' do
let(:params) { base_params }
2020-03-13 15:44:24 +05:30
end
2016-06-02 11:05:42 +05:30
context 'when the project snippet is private' do
2020-11-24 15:15:51 +05:30
let_it_be(:project_snippet) { create(:project_snippet, :private, project: project, author: user) }
2016-06-02 11:05:42 +05:30
context 'when anonymous' do
it 'does not include the private snippet' do
2020-07-28 23:09:34 +05:30
subject
2016-06-02 11:05:42 +05:30
expect(assigns(:snippets)).not_to include(project_snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-06-02 11:05:42 +05:30
end
end
context 'when signed in as the author' do
2020-11-24 15:15:51 +05:30
it 'renders the snippet' do
2017-09-10 17:25:29 +05:30
sign_in(user)
2016-06-02 11:05:42 +05:30
2020-07-28 23:09:34 +05:30
subject
2016-06-02 11:05:42 +05:30
expect(assigns(:snippets)).to include(project_snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-06-02 11:05:42 +05:30
end
end
context 'when signed in as a project member' do
it 'renders the snippet' do
2020-11-24 15:15:51 +05:30
sign_in(other_user)
2020-07-28 23:09:34 +05:30
subject
2016-06-02 11:05:42 +05:30
expect(assigns(:snippets)).to include(project_snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-06-02 11:05:42 +05:30
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST #mark_as_spam' do
2020-11-24 15:15:51 +05:30
let_it_be(:snippet) { create(:project_snippet, :private, project: project, author: 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|
2019-12-26 22:10:19 +05:30
allow(instance).to receive_messages(submit_spam: true)
end
2017-08-17 22:00:37 +05:30
stub_application_setting(akismet_enabled: true)
end
def mark_as_spam
admin = create(:admin)
create(:user_agent_detail, subject: snippet)
2018-11-18 11:00:15 +05:30
project.add_maintainer(admin)
2017-08-17 22:00:37 +05:30
sign_in(admin)
post :mark_as_spam,
2019-02-15 15:39:39 +05:30
params: {
namespace_id: project.namespace,
project_id: project,
id: snippet.id
}
2017-08-17 22:00:37 +05:30
end
it 'updates the snippet' do
mark_as_spam
expect(snippet.reload).not_to be_submittable_as_spam
end
end
2020-04-08 14:13:33 +05:30
shared_examples 'successful response' do
it 'renders the snippet' do
subject
expect(assigns(:snippet)).to eq(project_snippet)
expect(response).to have_gitlab_http_status(:ok)
end
end
2016-06-02 11:05:42 +05:30
%w[show raw].each do |action|
describe "GET ##{action}" do
context 'when the project snippet is private' do
2020-11-24 15:15:51 +05:30
let_it_be(:project_snippet) { create(:project_snippet, :private, :repository, project: project, author: user) }
2020-04-08 14:13:33 +05:30
subject { get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } }
2016-06-02 11:05:42 +05:30
context 'when anonymous' do
it 'responds with status 404' do
2020-04-08 14:13:33 +05:30
subject
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-06-02 11:05:42 +05:30
end
end
context 'when signed in as the author' do
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
2016-06-02 11:05:42 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'successful response'
2016-06-02 11:05:42 +05:30
end
context 'when signed in as a project member' do
2017-09-10 17:25:29 +05:30
before do
2020-11-24 15:15:51 +05:30
sign_in(other_user)
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'successful response'
2016-06-02 11:05:42 +05:30
end
end
context 'when the project snippet does not exist' do
2020-04-08 14:13:33 +05:30
subject { get action, params: { namespace_id: project.namespace, project_id: project, id: 42 } }
2016-06-02 11:05:42 +05:30
context 'when anonymous' do
it 'responds with status 404' do
2020-04-08 14:13:33 +05:30
subject
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-06-02 11:05:42 +05:30
end
end
context 'when signed in' do
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
2016-06-02 11:05:42 +05:30
it 'responds with status 404' do
2020-04-08 14:13:33 +05:30
subject
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-06-02 11:05:42 +05:30
end
end
end
end
end
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
describe 'GET #show as JSON' do
it 'renders the blob from the repository' do
project_snippet = create(:project_snippet, :public, :repository, project: project, author: user)
get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param }, format: :json
expect(assigns(:blob)).to eq(project_snippet.blobs.first)
end
end
2019-01-03 12:48:30 +05:30
describe "GET #show for embeddable content" do
2020-04-08 14:13:33 +05:30
let(:project_snippet) { create(:project_snippet, :repository, snippet_permission, project: project, author: user) }
2020-10-24 23:57:45 +05:30
let(:extra_params) { {} }
2019-01-03 12:48:30 +05:30
before do
sign_in(user)
end
2020-10-24 23:57:45 +05:30
subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param, **extra_params }, format: :js }
2020-04-08 14:13:33 +05:30
2019-01-03 12:48:30 +05:30
context 'when snippet is private' do
let(:snippet_permission) { :private }
it 'responds with status 404' do
2020-04-08 14:13:33 +05:30
subject
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-01-03 12:48:30 +05:30
end
end
context 'when snippet is public' do
let(:snippet_permission) { :public }
2020-10-24 23:57:45 +05:30
it 'renders the blob from the repository' do
subject
expect(assigns(:snippet)).to eq(project_snippet)
expect(assigns(:blobs)).to eq(project_snippet.blobs)
expect(response).to have_gitlab_http_status(:ok)
end
it 'does not show the blobs expanded by default' do
subject
expect(project_snippet.blobs.map(&:expanded?)).to be_all(false)
end
context 'when param expanded is set' do
let(:extra_params) { { expanded: true } }
it 'shows all blobs expanded' do
subject
expect(project_snippet.blobs.map(&:expanded?)).to be_all(true)
end
end
2019-01-03 12:48:30 +05:30
end
context 'when the project is private' do
let(:project) { create(:project_empty_repo, :private) }
context 'when snippet is public' do
let(:project_snippet) { create(:project_snippet, :public, project: project, author: user) }
it 'responds with status 404' do
2020-04-08 14:13:33 +05:30
subject
2019-01-03 12:48:30 +05:30
expect(assigns(:snippet)).to eq(project_snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-01-03 12:48:30 +05:30
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET #raw' do
2020-04-22 19:07:51 +05:30
let(:inline) { nil }
let(:line_ending) { nil }
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: project_snippet.to_param,
inline: inline,
line_ending: line_ending
}
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
subject { get :raw, params: params }
context 'when repository is empty' do
2020-11-24 15:15:51 +05:30
let_it_be(:content) { "first line\r\nsecond line\r\nthird line" }
let_it_be(:project_snippet) do
2020-04-22 19:07:51 +05:30
create(
:project_snippet, :public, :empty_repo,
project: project,
author: user,
content: content
)
2017-08-17 22:00:37 +05:30
end
2020-11-24 15:15:51 +05:30
let(:formatted_content) { content.gsub(/\r\n/, "\n") }
2020-04-22 19:07:51 +05:30
context 'CRLF line ending' do
before do
allow_next_instance_of(Blob) do |instance|
allow(instance).to receive(:data).and_return(content)
end
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
it 'returns LF line endings by default' do
subject
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
expect(response.body).to eq(formatted_content)
end
context 'when line_ending parameter present' do
let(:line_ending) { :raw }
it 'does not convert line endings' do
subject
expect(response.body).to eq(content)
end
end
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
end
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
context 'when repository is not empty' do
2020-11-24 15:15:51 +05:30
let_it_be(:project_snippet) do
2020-04-22 19:07:51 +05:30
create(
:project_snippet, :public, :repository,
project: project,
author: user
)
end
it 'sends the blob' do
subject
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true'
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
it_behaves_like 'project cache control headers'
it_behaves_like 'content disposition headers'
2017-08-17 22:00:37 +05:30
end
end
2016-06-02 11:05:42 +05:30
end