BigW Consortium Gitlab

project_wiki_spec.rb 6.32 KB
Newer Older
1 2
require "spec_helper"

3
describe ProjectWiki do
4
  let(:project) { create(:empty_project) }
5 6 7
  let(:repository) { project.repository }
  let(:user) { project.owner }
  let(:gitlab_shell) { Gitlab::Shell.new }
8
  let(:project_wiki) { ProjectWiki.new(project, user) }
9

10 11
  subject { project_wiki }
  before { project_wiki.wiki }
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  describe "#path_with_namespace" do
    it "returns the project path with namespace with the .wiki extension" do
      subject.path_with_namespace.should == project.path_with_namespace + ".wiki"
    end
  end

  describe "#url_to_repo" do
    it "returns the correct ssh url to the repo" do
      subject.url_to_repo.should == gitlab_shell.url_to_repo(subject.path_with_namespace)
    end
  end

  describe "#ssh_url_to_repo" do
    it "equals #url_to_repo" do
      subject.ssh_url_to_repo.should == subject.url_to_repo
    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"
      subject.http_url_to_repo.should == repo_http_url
    end
  end

  describe "#wiki" do
    it "contains a Gollum::Wiki instance" do
      subject.wiki.should be_a Gollum::Wiki
    end

    it "creates a new wiki repo if one does not yet exist" do
Dmitriy Zaporozhets committed
45
      project_wiki.create_page("index", "test content").should be_true
46 47 48
    end

    it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
Dmitriy Zaporozhets committed
49 50
      project_wiki.stub(:init_repo).and_return(false)
      expect { project_wiki.send(:create_repo!) }.to raise_exception(ProjectWiki::CouldNotCreateWikiError)
51 52 53
    end
  end

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  describe "#empty?" do
    context "when the wiki repository is empty" do
      before do
        Gitlab::Shell.any_instance.stub(:add_repository) do
          create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
        end
        project.stub(:path_with_namespace).and_return("non-existant")
      end

      its(:empty?) { should be_true }
    end

    context "when the wiki has pages" do
      before do
        create_page("index", "This is an awesome new Gollum Wiki")
      end

      its(:empty?) { should be_false }
    end
  end

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  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
      @pages.first.should be_a WikiPage
    end

    it "returns the correct number of pages" do
      @pages.count.should == 1
    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")
      page.title.should == "index page"
    end

    it "returns nil if the page does not exist" do
      subject.find_page("non-existant").should == nil
    end

    it "can find a page by slug" do
      page = subject.find_page("index-page")
      page.title.should == "index page"
    end

    it "returns a WikiPage instance" do
      page = subject.find_page("index page")
      page.should be_a WikiPage
    end
  end

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  describe '#find_file' do
    before do
      file = Gollum::File.new(subject.wiki)
      Gollum::Wiki.any_instance.
                   stub(:file).with('image.jpg', 'master', true).
                   and_return(file)
      Gollum::File.any_instance.
                   stub(:mime_type).
                   and_return('image/jpeg')
      Gollum::Wiki.any_instance.
                   stub(:file).with('non-existant', 'master', true).
                   and_return(nil)
    end

    after do
      Gollum::Wiki.any_instance.unstub(:file)
      Gollum::File.any_instance.unstub(:mime_type)
    end

    it 'returns the latest version of the file if it exists' do
      file = subject.find_file('image.jpg')
      file.mime_type.should == 'image/jpeg'
    end

    it 'returns nil if the page does not exist' do
      subject.find_file('non-existant').should == nil
    end

    it 'returns a Gollum::File instance' do
      file = subject.find_file('image.jpg')
      file.should be_a Gollum::File
    end
  end

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  describe "#create_page" do
    after do
      destroy_page(subject.pages.first.page)
    end

    it "creates a new wiki page" do
      subject.create_page("test page", "this is content").should_not == false
      subject.pages.count.should == 1
    end

    it "returns false when a duplicate page exists" do
      subject.create_page("test page", "content")
      subject.create_page("test page", "content").should == false
    end

    it "stores an error message when a duplicate page exists" do
      2.times { subject.create_page("test page", "content") }
      subject.error_message.should =~ /Duplicate page:/
    end

    it "sets the correct commit message" do
      subject.create_page("test page", "some content", :markdown, "commit message")
      subject.pages.first.page.version.message.should == "commit message"
    end
  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
      @page.raw_data.should == "some other content"
    end

    it "sets the correct commit message" do
      @page.version.message.should == "updated page"
    end
  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)
      subject.pages.count.should == 0
    end
  end

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  private

  def create_temp_repo(path)
    FileUtils.mkdir_p path
    system(*%W(git init --quiet --bare -- #{path}))
  end

  def remove_temp_repo(path)
    FileUtils.rm_rf path
  end

  def commit_details
    commit = {name: user.name, email: user.email, message: "test commit"}
  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
238
end