2014-09-02 18:07:02 +05:30
|
|
|
require "spec_helper"
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
describe ProjectWiki, models: true do
|
2014-09-02 18:07:02 +05:30
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
let(:repository) { project.repository }
|
|
|
|
let(:user) { project.owner }
|
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
|
|
|
let(:project_wiki) { ProjectWiki.new(project, user) }
|
|
|
|
|
|
|
|
subject { project_wiki }
|
|
|
|
before { project_wiki.wiki }
|
|
|
|
|
|
|
|
describe "#path_with_namespace" do
|
|
|
|
it "returns the project path with namespace with the .wiki extension" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.path_with_namespace).to eq(project.path_with_namespace + ".wiki")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#url_to_repo" do
|
|
|
|
it "returns the correct ssh url to the repo" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.url_to_repo).to eq(gitlab_shell.url_to_repo(subject.path_with_namespace))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#ssh_url_to_repo" do
|
|
|
|
it "equals #url_to_repo" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.ssh_url_to_repo).to eq(subject.url_to_repo)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#http_url_to_repo" do
|
|
|
|
it "provides the full http url to the repo" do
|
|
|
|
gitlab_url = Gitlab.config.gitlab.url
|
|
|
|
repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git"
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.http_url_to_repo).to eq(repo_http_url)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
describe "#wiki_base_path" do
|
|
|
|
it "returns the wiki base path" do
|
|
|
|
wiki_base_path = "/#{project.path_with_namespace}/wikis"
|
|
|
|
expect(subject.wiki_base_path).to eq(wiki_base_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
describe "#wiki" do
|
|
|
|
it "contains a Gollum::Wiki instance" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.wiki).to be_a Gollum::Wiki
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new wiki repo if one does not yet exist" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(project_wiki.create_page("index", "test content")).to be_truthy
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
|
2015-04-26 12:48:37 +05:30
|
|
|
allow(project_wiki).to receive(:init_repo).and_return(false)
|
2014-09-02 18:07:02 +05:30
|
|
|
expect { project_wiki.send(:create_repo!) }.to raise_exception(ProjectWiki::CouldNotCreateWikiError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#empty?" do
|
|
|
|
context "when the wiki repository is empty" do
|
|
|
|
before do
|
2015-04-26 12:48:37 +05:30
|
|
|
allow_any_instance_of(Gitlab::Shell).to receive(:add_repository) do
|
2014-09-02 18:07:02 +05:30
|
|
|
create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
allow(project).to receive(:path_with_namespace).and_return("non-existant")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
describe '#empty?' do
|
|
|
|
subject { super().empty? }
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context "when the wiki has pages" do
|
|
|
|
before do
|
2015-04-26 12:48:37 +05:30
|
|
|
project_wiki.create_page("index", "This is an awesome new Gollum Wiki")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
describe '#empty?' do
|
|
|
|
subject { super().empty? }
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#pages" do
|
|
|
|
before do
|
|
|
|
create_page("index", "This is an awesome new Gollum Wiki")
|
|
|
|
@pages = subject.pages
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page(@pages.first.page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array of WikiPage instances" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(@pages.first).to be_a WikiPage
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct number of pages" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(@pages.count).to eq(1)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#find_page" do
|
|
|
|
before do
|
|
|
|
create_page("index page", "This is an awesome Gollum Wiki")
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page(subject.pages.first.page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the latest version of the page if it exists" do
|
|
|
|
page = subject.find_page("index page")
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(page.title).to eq("index page")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil if the page does not exist" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.find_page("non-existant")).to eq(nil)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "can find a page by slug" do
|
|
|
|
page = subject.find_page("index-page")
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(page.title).to eq("index page")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a WikiPage instance" do
|
|
|
|
page = subject.find_page("index page")
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(page).to be_a WikiPage
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#find_file' do
|
|
|
|
before do
|
|
|
|
file = Gollum::File.new(subject.wiki)
|
2015-04-26 12:48:37 +05:30
|
|
|
allow_any_instance_of(Gollum::Wiki).
|
|
|
|
to receive(:file).with('image.jpg', 'master', true).
|
2014-09-02 18:07:02 +05:30
|
|
|
and_return(file)
|
2015-04-26 12:48:37 +05:30
|
|
|
allow_any_instance_of(Gollum::File).
|
|
|
|
to receive(:mime_type).
|
2014-09-02 18:07:02 +05:30
|
|
|
and_return('image/jpeg')
|
2015-04-26 12:48:37 +05:30
|
|
|
allow_any_instance_of(Gollum::Wiki).
|
|
|
|
to receive(:file).with('non-existant', 'master', true).
|
2014-09-02 18:07:02 +05:30
|
|
|
and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2015-04-26 12:48:37 +05:30
|
|
|
allow_any_instance_of(Gollum::Wiki).to receive(:file).and_call_original
|
|
|
|
allow_any_instance_of(Gollum::File).to receive(:mime_type).and_call_original
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the latest version of the file if it exists' do
|
|
|
|
file = subject.find_file('image.jpg')
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(file.mime_type).to eq('image/jpeg')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if the page does not exist' do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.find_file('non-existant')).to eq(nil)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Gollum::File instance' do
|
|
|
|
file = subject.find_file('image.jpg')
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(file).to be_a Gollum::File
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#create_page" do
|
|
|
|
after do
|
|
|
|
destroy_page(subject.pages.first.page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new wiki page" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.create_page("test page", "this is content")).not_to eq(false)
|
|
|
|
expect(subject.pages.count).to eq(1)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when a duplicate page exists" do
|
|
|
|
subject.create_page("test page", "content")
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.create_page("test page", "content")).to eq(false)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "stores an error message when a duplicate page exists" do
|
|
|
|
2.times { subject.create_page("test page", "content") }
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.error_message).to match(/Duplicate page:/)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the correct commit message" do
|
|
|
|
subject.create_page("test page", "some content", :markdown, "commit message")
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.pages.first.page.version.message).to eq("commit message")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
|
|
|
it 'updates project activity' do
|
|
|
|
expect(subject).to receive(:update_project_activity)
|
|
|
|
|
|
|
|
subject.create_page('Test Page', 'This is content')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe "#update_page" do
|
|
|
|
before do
|
|
|
|
create_page("update-page", "some content")
|
|
|
|
@gollum_page = subject.wiki.paged("update-page")
|
|
|
|
subject.update_page(@gollum_page, "some other content", :markdown, "updated page")
|
|
|
|
@page = subject.pages.first.page
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page(@page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the content of the page" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(@page.raw_data).to eq("some other content")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the correct commit message" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(@page.version.message).to eq("updated page")
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
|
|
|
it 'updates project activity' do
|
|
|
|
expect(subject).to receive(:update_project_activity)
|
|
|
|
|
|
|
|
subject.update_page(@gollum_page, 'Yet more content', :markdown, 'Updated page again')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe "#delete_page" do
|
|
|
|
before do
|
|
|
|
create_page("index", "some content")
|
|
|
|
@page = subject.wiki.paged("index")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the page" do
|
|
|
|
subject.delete_page(@page)
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(subject.pages.count).to eq(0)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
|
|
|
it 'updates project activity' do
|
|
|
|
expect(subject).to receive(:update_project_activity)
|
|
|
|
|
|
|
|
subject.delete_page(@page)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_temp_repo(path)
|
|
|
|
FileUtils.mkdir_p path
|
2015-11-26 14:37:03 +05:30
|
|
|
system(*%W(#{Gitlab.config.git.bin_path} init --quiet --bare -- #{path}))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def remove_temp_repo(path)
|
|
|
|
FileUtils.rm_rf path
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_details
|
2015-10-24 18:46:33 +05:30
|
|
|
{ name: user.name, email: user.email, message: "test commit" }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_page(name, content)
|
|
|
|
subject.wiki.write_page(name, :markdown, content, commit_details)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_page(page)
|
|
|
|
subject.wiki.delete_page(page, commit_details)
|
|
|
|
end
|
|
|
|
end
|