2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::WikisController do
|
2019-12-21 20:55:43 +05:30
|
|
|
let_it_be(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:user) { project.owner }
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:project_wiki) { ProjectWiki.new(project, user) }
|
|
|
|
let(:wiki) { project_wiki.wiki }
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:wiki_title) { 'page title test' }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
create_page(wiki_title, 'hello world')
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page(wiki_title)
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
describe 'GET #new' do
|
|
|
|
subject { get :new, params: { namespace_id: project.namespace, project_id: project } }
|
|
|
|
|
|
|
|
it 'redirects to #show and appends a `random_title` param' do
|
|
|
|
subject
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(Rails.application.routes.recognize_path(response.redirect_url)).to include(
|
|
|
|
controller: 'projects/wikis',
|
|
|
|
action: 'show'
|
|
|
|
)
|
|
|
|
expect(response.redirect_url).to match(/\?random_title=true\Z/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
describe 'GET #pages' do
|
|
|
|
subject { get :pages, params: { namespace_id: project.namespace, project_id: project, id: wiki_title } }
|
|
|
|
|
|
|
|
it 'does not load the pages content' do
|
|
|
|
expect(controller).to receive(:load_wiki).and_return(project_wiki)
|
|
|
|
|
|
|
|
expect(project_wiki).to receive(:list_pages).twice.and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
describe 'GET #history' do
|
|
|
|
before do
|
|
|
|
allow(controller)
|
|
|
|
.to receive(:can?)
|
|
|
|
.with(any_args)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
# The :create_wiki permission is irrelevant to reading history.
|
|
|
|
expect(controller)
|
|
|
|
.not_to receive(:can?)
|
|
|
|
.with(anything, :create_wiki, any_args)
|
|
|
|
|
|
|
|
allow(controller)
|
|
|
|
.to receive(:can?)
|
|
|
|
.with(anything, :read_wiki, any_args)
|
|
|
|
.and_return(allow_read_wiki)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'fetching history' do |expected_status|
|
|
|
|
before do
|
|
|
|
get :history, params: { namespace_id: project.namespace, project_id: project, id: wiki_title }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns status #{expected_status}" do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(expected_status)
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fetching history', :ok do
|
|
|
|
let(:allow_read_wiki) { true }
|
|
|
|
|
|
|
|
it 'assigns @page_versions' do
|
|
|
|
expect(assigns(:page_versions)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fetching history', :not_found do
|
|
|
|
let(:allow_read_wiki) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
describe 'GET #show' do
|
|
|
|
render_views
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:random_title) { nil }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: id, random_title: random_title } }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when page exists' do
|
|
|
|
let(:id) { wiki_title }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it 'limits the retrieved pages for the sidebar' do
|
|
|
|
subject
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(assigns(:page).title).to eq(wiki_title)
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(assigns(:sidebar_wiki_entries)).to contain_exactly(an_instance_of(WikiPage))
|
|
|
|
expect(assigns(:sidebar_limited)).to be(false)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is invalid' do
|
|
|
|
it 'sets flash error' do
|
|
|
|
allow(controller).to receive(:valid_encoding?).and_return(false)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(flash[:notice]).to eq(_('The content of this page is not encoded in UTF-8. Edits can only be made via the Git repository.'))
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when the page does not exist' do
|
|
|
|
let(:id) { 'does not exist' }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
subject
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it 'builds a new wiki page with the id as the title' do
|
|
|
|
expect(assigns(:page).title).to eq(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a random_title param is present' do
|
|
|
|
let(:random_title) { true }
|
|
|
|
|
|
|
|
it 'builds a new wiki page with no title' do
|
|
|
|
expect(assigns(:page).title).to be_empty
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
context 'when page is a file' do
|
|
|
|
include WikiHelpers
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:id) { upload_file_to_wiki(project, user, file_name) }
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
context 'when file is an image' do
|
|
|
|
let(:file_name) { 'dk.png' }
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it 'delivers the image' do
|
2020-04-22 19:07:51 +05:30
|
|
|
subject
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(response.headers['Content-Disposition']).to match(/^inline/)
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2019-01-03 12:48:30 +05:30
|
|
|
end
|
2018-12-23 12:14:25 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
context 'when file is a svg' do
|
|
|
|
let(:file_name) { 'unsanitized.svg' }
|
2018-12-23 12:14:25 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it 'delivers the image' do
|
2020-04-22 19:07:51 +05:30
|
|
|
subject
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(response.headers['Content-Disposition']).to match(/^inline/)
|
|
|
|
expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it_behaves_like 'project cache control headers'
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is a pdf' do
|
|
|
|
let(:file_name) { 'git-cheat-sheet.pdf' }
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it 'sets the content type to sets the content response headers' do
|
2020-04-22 19:07:51 +05:30
|
|
|
subject
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(response.headers['Content-Disposition']).to match(/^inline/)
|
|
|
|
expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it_behaves_like 'project cache control headers'
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe 'POST #preview_markdown' do
|
|
|
|
it 'renders json in a correct format' do
|
2019-02-15 15:39:39 +05:30
|
|
|
post :preview_markdown, params: { namespace_id: project.namespace, project_id: project, id: 'page/path', text: '*Markdown* text' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(json_response.keys).to match_array(%w(body references))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
describe 'GET #edit' do
|
2019-02-15 15:39:39 +05:30
|
|
|
subject { get(:edit, params: { namespace_id: project.namespace, project_id: project, id: wiki_title }) }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
context 'when page content encoding is invalid' do
|
|
|
|
it 'redirects to show' do
|
|
|
|
allow(controller).to receive(:valid_encoding?).and_return(false)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(response).to redirect_to_wiki(project, project_wiki.list_pages.first)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the page has nil content' do
|
|
|
|
let(:page) { create(:wiki_page) }
|
|
|
|
|
|
|
|
it 'redirects to show' do
|
|
|
|
allow(page).to receive(:content).and_return(nil)
|
|
|
|
allow(controller).to receive(:find_page).and_return(page)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to redirect_to_wiki(project, page)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is valid' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'shows the edit page' do
|
|
|
|
subject
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(response.body).to include(s_('Wiki|Edit Page'))
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH #update' do
|
|
|
|
let(:new_title) { 'New title' }
|
|
|
|
let(:new_content) { 'New content' }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
subject do
|
|
|
|
patch(:update,
|
2019-02-15 15:39:39 +05:30
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: wiki_title,
|
|
|
|
wiki: { title: new_title, content: new_content }
|
|
|
|
})
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is invalid' do
|
|
|
|
it 'redirects to show' do
|
|
|
|
allow(controller).to receive(:valid_encoding?).and_return(false)
|
|
|
|
|
|
|
|
subject
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(response).to redirect_to_wiki(project, project_wiki.list_pages.first)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is valid' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'updates the page' do
|
|
|
|
subject
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
wiki_page = project_wiki.list_pages(load_content: true).first
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
expect(wiki_page.title).to eq new_title
|
|
|
|
expect(wiki_page.content).to eq new_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_page(name, content)
|
|
|
|
wiki.write_page(name, :markdown, content, commit_details(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_details(name)
|
|
|
|
Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "created page #{name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_page(title, dir = '')
|
|
|
|
page = wiki.page(title: title, dir: dir)
|
|
|
|
project_wiki.delete_page(page, "test commit")
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def redirect_to_wiki(project, page)
|
|
|
|
redirect_to(controller.project_wiki_path(project, page))
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|