2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-20 04:56:41 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::WikisController do
|
2018-07-22 02:48:12 -04:00
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2018-07-31 02:08:19 -04:00
|
|
|
let(:user) { project.owner }
|
|
|
|
let(:project_wiki) { ProjectWiki.new(project, user) }
|
|
|
|
let(:wiki) { project_wiki.wiki }
|
|
|
|
let(:wiki_title) { 'page-title-test' }
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-07-31 02:08:19 -04:00
|
|
|
before do
|
|
|
|
create_page(wiki_title, 'hello world')
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page(wiki_title)
|
|
|
|
end
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2019-04-25 00:19:07 -04:00
|
|
|
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
|
|
|
|
|
2018-07-31 02:08:19 -04:00
|
|
|
describe 'GET #show' do
|
2018-07-22 02:48:12 -04:00
|
|
|
render_views
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: wiki_title } }
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-10-17 11:47:05 -04:00
|
|
|
it 'limits the retrieved pages for the sidebar' do
|
|
|
|
expect(controller).to receive(:load_wiki).and_return(project_wiki)
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-10-17 11:47:05 -04:00
|
|
|
# empty? call
|
2019-04-25 00:19:07 -04:00
|
|
|
expect(project_wiki).to receive(:list_pages).with(limit: 1).and_call_original
|
2018-10-17 11:47:05 -04:00
|
|
|
# Sidebar entries
|
2019-04-25 00:19:07 -04:00
|
|
|
expect(project_wiki).to receive(:list_pages).with(limit: 15).and_call_original
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-10-17 11:47:05 -04:00
|
|
|
subject
|
2018-07-31 02:08:19 -04:00
|
|
|
|
2018-10-17 11:47:05 -04:00
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(response.body).to include(wiki_title)
|
2018-07-31 02:08:19 -04:00
|
|
|
end
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-07-31 02:08:19 -04:00
|
|
|
context 'when page content encoding is invalid' do
|
|
|
|
it 'sets flash error' do
|
|
|
|
allow(controller).to receive(:valid_encoding?).and_return(false)
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-07-31 02:08:19 -04:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
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.'
|
|
|
|
end
|
2018-07-22 02:48:12 -04:00
|
|
|
end
|
2018-10-17 11:47:05 -04:00
|
|
|
|
|
|
|
context 'when page is a file' do
|
|
|
|
include WikiHelpers
|
|
|
|
|
|
|
|
let(:path) { upload_file_to_wiki(project, user, file_name) }
|
|
|
|
|
2018-12-19 06:51:07 -05:00
|
|
|
before do
|
|
|
|
get :show, params: { namespace_id: project.namespace, project_id: project, id: path }
|
|
|
|
end
|
2018-10-17 11:47:05 -04:00
|
|
|
|
|
|
|
context 'when file is an image' do
|
|
|
|
let(:file_name) { 'dk.png' }
|
|
|
|
|
2018-12-19 06:51:07 -05:00
|
|
|
it 'delivers the image' do
|
|
|
|
expect(response.headers['Content-Disposition']).to match(/^inline/)
|
|
|
|
expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
|
|
|
end
|
2018-12-06 16:22:39 -05:00
|
|
|
|
2018-12-19 06:51:07 -05:00
|
|
|
context 'when file is a svg' do
|
|
|
|
let(:file_name) { 'unsanitized.svg' }
|
2018-12-06 16:22:39 -05:00
|
|
|
|
2018-12-19 06:51:07 -05:00
|
|
|
it 'delivers the image' do
|
|
|
|
expect(response.headers['Content-Disposition']).to match(/^inline/)
|
|
|
|
expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2018-10-17 11:47:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is a pdf' do
|
|
|
|
let(:file_name) { 'git-cheat-sheet.pdf' }
|
|
|
|
|
2018-12-19 06:51:07 -05:00
|
|
|
it 'sets the content type to sets the content response headers' do
|
|
|
|
expect(response.headers['Content-Disposition']).to match(/^inline/)
|
|
|
|
expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2018-10-17 11:47:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-22 02:48:12 -04:00
|
|
|
end
|
2017-04-20 04:56:41 -04:00
|
|
|
|
|
|
|
describe 'POST #preview_markdown' do
|
|
|
|
it 'renders json in a correct format' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post :preview_markdown, params: { namespace_id: project.namespace, project_id: project, id: 'page/path', text: '*Markdown* text' }
|
2017-04-20 04:56:41 -04:00
|
|
|
|
|
|
|
expect(JSON.parse(response.body).keys).to match_array(%w(body references))
|
|
|
|
end
|
|
|
|
end
|
2018-07-22 02:48:12 -04:00
|
|
|
|
2018-07-31 02:08:19 -04:00
|
|
|
describe 'GET #edit' do
|
2018-12-17 17:52:17 -05:00
|
|
|
subject { get(:edit, params: { namespace_id: project.namespace, project_id: project, id: wiki_title }) }
|
2018-07-31 02:08:19 -04:00
|
|
|
|
|
|
|
context 'when page content encoding is invalid' do
|
|
|
|
it 'redirects to show' do
|
|
|
|
allow(controller).to receive(:valid_encoding?).and_return(false)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2019-04-25 00:19:07 -04:00
|
|
|
expect(response).to redirect_to(project_wiki_path(project, project_wiki.list_pages.first))
|
2018-07-31 02:08:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is valid' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'shows the edit page' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(response.body).to include('Edit Page')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH #update' do
|
|
|
|
let(:new_title) { 'New title' }
|
|
|
|
let(:new_content) { 'New content' }
|
|
|
|
subject do
|
|
|
|
patch(:update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: wiki_title,
|
|
|
|
wiki: { title: new_title, content: new_content }
|
|
|
|
})
|
2018-07-31 02:08:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is invalid' do
|
|
|
|
it 'redirects to show' do
|
|
|
|
allow(controller).to receive(:valid_encoding?).and_return(false)
|
|
|
|
|
|
|
|
subject
|
2019-04-25 00:19:07 -04:00
|
|
|
expect(response).to redirect_to(project_wiki_path(project, project_wiki.list_pages.first))
|
2018-07-31 02:08:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page content encoding is valid' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'updates the page' do
|
|
|
|
subject
|
|
|
|
|
2019-04-25 00:19:07 -04:00
|
|
|
wiki_page = project_wiki.list_pages(load_content: true).first
|
2018-07-31 02:08:19 -04:00
|
|
|
|
|
|
|
expect(wiki_page.title).to eq new_title
|
|
|
|
expect(wiki_page.content).to eq new_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-22 02:48:12 -04:00
|
|
|
def create_page(name, content)
|
2018-07-31 02:08:19 -04:00
|
|
|
wiki.write_page(name, :markdown, content, commit_details(name))
|
2018-07-22 02:48:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def commit_details(name)
|
|
|
|
Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "created page #{name}")
|
|
|
|
end
|
2018-07-31 02:08:19 -04:00
|
|
|
|
|
|
|
def destroy_page(title, dir = '')
|
|
|
|
page = wiki.page(title: title, dir: dir)
|
|
|
|
project_wiki.delete_page(page, "test commit")
|
|
|
|
end
|
2017-04-20 04:56:41 -04:00
|
|
|
end
|