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

D
Douwe Maan 已提交
3
describe ProjectWiki, models: true 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

  describe "#path_with_namespace" do
    it "returns the project path with namespace with the .wiki extension" do
15
      expect(subject.path_with_namespace).to eq(project.path_with_namespace + ".wiki")
16 17 18 19 20
    end
  end

  describe "#url_to_repo" do
    it "returns the correct ssh url to the repo" do
21
      expect(subject.url_to_repo).to eq(gitlab_shell.url_to_repo(subject.path_with_namespace))
22 23 24 25 26
    end
  end

  describe "#ssh_url_to_repo" do
    it "equals #url_to_repo" do
27
      expect(subject.ssh_url_to_repo).to eq(subject.url_to_repo)
28 29 30 31 32 33 34
    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"
35
      expect(subject.http_url_to_repo).to eq(repo_http_url)
36 37 38
    end
  end

39 40
  describe "#wiki_base_path" do
    it "returns the wiki base path" do
41 42 43
      gitlab_url = Gitlab.config.gitlab.url
      wiki_base_path = "#{gitlab_url}/#{project.path_with_namespace}/wikis"

44 45 46 47
      expect(subject.wiki_base_path).to eq(wiki_base_path)
    end
  end

48 49
  describe "#wiki" do
    it "contains a Gollum::Wiki instance" do
50
      expect(subject.wiki).to be_a Gollum::Wiki
51 52 53
    end

    it "creates a new wiki repo if one does not yet exist" do
54
      expect(project_wiki.create_page("index", "test content")).to be_truthy
55 56 57
    end

    it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
58
      allow(project_wiki).to receive(:init_repo).and_return(false)
D
Dmitriy Zaporozhets 已提交
59
      expect { project_wiki.send(:create_repo!) }.to raise_exception(ProjectWiki::CouldNotCreateWikiError)
60 61 62
    end
  end

J
Jacob Vosmaer 已提交
63 64 65
  describe "#empty?" do
    context "when the wiki repository is empty" do
      before do
66
        allow_any_instance_of(Gitlab::Shell).to receive(:add_repository) do
J
Jacob Vosmaer 已提交
67 68
          create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
        end
69
        allow(project).to receive(:path_with_namespace).and_return("non-existant")
J
Jacob Vosmaer 已提交
70 71
      end

72 73 74 75
      describe '#empty?' do
        subject { super().empty? }
        it { is_expected.to be_truthy }
      end
J
Jacob Vosmaer 已提交
76 77 78 79
    end

    context "when the wiki has pages" do
      before do
80
        project_wiki.create_page("index", "This is an awesome new Gollum Wiki")
J
Jacob Vosmaer 已提交
81 82
      end

83 84 85 86
      describe '#empty?' do
        subject { super().empty? }
        it { is_expected.to be_falsey }
      end
J
Jacob Vosmaer 已提交
87 88 89
    end
  end

90 91 92 93 94 95 96 97 98 99 100
  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
101
      expect(@pages.first).to be_a WikiPage
102 103 104
    end

    it "returns the correct number of pages" do
105
      expect(@pages.count).to eq(1)
106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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")
120
      expect(page.title).to eq("index page")
121 122 123
    end

    it "returns nil if the page does not exist" do
124
      expect(subject.find_page("non-existant")).to eq(nil)
125 126 127 128
    end

    it "can find a page by slug" do
      page = subject.find_page("index-page")
129
      expect(page.title).to eq("index page")
130 131 132 133
    end

    it "returns a WikiPage instance" do
      page = subject.find_page("index page")
134
      expect(page).to be_a WikiPage
135 136 137
    end
  end

138 139 140
  describe '#find_file' do
    before do
      file = Gollum::File.new(subject.wiki)
141 142
      allow_any_instance_of(Gollum::Wiki).
                   to receive(:file).with('image.jpg', 'master', true).
143
                   and_return(file)
144 145
      allow_any_instance_of(Gollum::File).
                   to receive(:mime_type).
146
                   and_return('image/jpeg')
147 148
      allow_any_instance_of(Gollum::Wiki).
                   to receive(:file).with('non-existant', 'master', true).
149 150 151 152
                   and_return(nil)
    end

    after do
153 154
      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
155 156 157 158
    end

    it 'returns the latest version of the file if it exists' do
      file = subject.find_file('image.jpg')
159
      expect(file.mime_type).to eq('image/jpeg')
160 161 162
    end

    it 'returns nil if the page does not exist' do
163
      expect(subject.find_file('non-existant')).to eq(nil)
164 165 166 167
    end

    it 'returns a Gollum::File instance' do
      file = subject.find_file('image.jpg')
168
      expect(file).to be_a Gollum::File
169 170 171
    end
  end

172 173 174 175 176 177
  describe "#create_page" do
    after do
      destroy_page(subject.pages.first.page)
    end

    it "creates a new wiki page" do
178 179
      expect(subject.create_page("test page", "this is content")).not_to eq(false)
      expect(subject.pages.count).to eq(1)
180 181 182 183
    end

    it "returns false when a duplicate page exists" do
      subject.create_page("test page", "content")
184
      expect(subject.create_page("test page", "content")).to eq(false)
185 186 187 188
    end

    it "stores an error message when a duplicate page exists" do
      2.times { subject.create_page("test page", "content") }
189
      expect(subject.error_message).to match(/Duplicate page:/)
190 191 192 193
    end

    it "sets the correct commit message" do
      subject.create_page("test page", "some content", :markdown, "commit message")
194
      expect(subject.pages.first.page.version.message).to eq("commit message")
195
    end
196 197 198 199 200 201

    it 'updates project activity' do
      expect(subject).to receive(:update_project_activity)

      subject.create_page('Test Page', 'This is content')
    end
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  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
217
      expect(@page.raw_data).to eq("some other content")
218 219 220
    end

    it "sets the correct commit message" do
221
      expect(@page.version.message).to eq("updated page")
222
    end
223 224 225 226 227 228

    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
229 230 231 232 233 234 235 236 237 238
  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)
239
      expect(subject.pages.count).to eq(0)
240
    end
241 242 243 244 245 246

    it 'updates project activity' do
      expect(subject).to receive(:update_project_activity)

      subject.delete_page(@page)
    end
247 248
  end

249 250 251 252 253 254 255 256 257 258 259 260
  describe '#create_repo!' do
    it 'creates a repository' do
      expect(subject).to receive(:init_repo).
        with(subject.path_with_namespace).
        and_return(true)

      expect(subject.repository).to receive(:after_create)

      expect(subject.create_repo!).to be_an_instance_of(Gollum::Wiki)
    end
  end

261 262 263 264
  private

  def create_temp_repo(path)
    FileUtils.mkdir_p path
265
    system(*%W(#{Gitlab.config.git.bin_path} init --quiet --bare -- #{path}))
266 267 268 269 270 271 272
  end

  def remove_temp_repo(path)
    FileUtils.rm_rf path
  end

  def commit_details
273
    { name: user.name, email: user.email, message: "test commit" }
274 275 276 277 278 279 280 281 282
  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
283
end