BigW Consortium Gitlab

wiki_page_spec.rb 4.67 KB
Newer Older
1 2 3
require "spec_helper"

describe WikiPage do
4
  let(:project) { create(:empty_project) }
5
  let(:user) { project.owner }
6
  let(:wiki) { ProjectWiki.new(project, user) }
7 8 9 10 11 12 13 14 15 16 17 18

  subject { WikiPage.new(wiki) }

  describe "#initialize" do
    context "when initialized with an existing gollum page" do
      before do
        create_page("test page", "test content")
        @page = wiki.wiki.paged("test page")
        @wiki_page = WikiPage.new(wiki, @page, true)
      end

      it "sets the slug attribute" do
19
        expect(@wiki_page.slug).to eq("test-page")
20 21 22
      end

      it "sets the title attribute" do
23
        expect(@wiki_page.title).to eq("test page")
24 25 26
      end

      it "sets the formatted content attribute" do
27
        expect(@wiki_page.content).to eq("test content")
28 29 30
      end

      it "sets the format attribute" do
31
        expect(@wiki_page.format).to eq(:markdown)
32 33 34
      end

      it "sets the message attribute" do
35
        expect(@wiki_page.message).to eq("test commit")
36 37 38
      end

      it "sets the version attribute" do
39
        expect(@wiki_page.version).to be_a Gollum::Git::Commit
40 41 42 43 44 45
      end
    end
  end

  describe "validations" do
    before do
46
      subject.attributes = { title: 'title', content: 'content' }
47 48 49 50
    end

    it "validates presence of title" do
      subject.attributes.delete(:title)
51
      expect(subject.valid?).to be_falsey
52 53 54 55
    end

    it "validates presence of content" do
      subject.attributes.delete(:content)
56
      expect(subject.valid?).to be_falsey
57 58 59 60
    end
  end

  before do
61
    @wiki_attr = { title: "Index", content: "Home Page", format: "markdown" }
62 63 64 65 66 67 68 69 70 71
  end

  describe "#create" do
    after do
      destroy_page("Index")
    end

    context "with valid attributes" do
      it "saves the wiki page" do
        subject.create(@wiki_attr)
72
        expect(wiki.find_page("Index")).not_to be_nil
73 74 75
      end

      it "returns true" do
76
        expect(subject.create(@wiki_attr)).to eq(true)
77 78 79 80
      end
    end
  end

81 82 83 84
  describe "dot in the title" do
    let(:title) { 'Index v1.2.3' }

    before do
85
      @wiki_attr = { title: title, content: "Home Page", format: "markdown" }
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
    end

    describe "#create" do
      after do
        destroy_page(title)
      end

      context "with valid attributes" do
        it "saves the wiki page" do
          subject.create(@wiki_attr)
          expect(wiki.find_page(title)).not_to be_nil
        end

        it "returns true" do
          expect(subject.create(@wiki_attr)).to eq(true)
        end
      end
    end

    describe "#update" do
      before do
        create_page(title, "content")
        @page = wiki.find_page(title)
      end

      it "updates the content of the page" do
        @page.update("new content")
        @page = wiki.find_page(title)
      end

      it "returns true" do
        expect(@page.update("more content")).to be_truthy
      end
    end
  end

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  describe "#update" do
    before do
      create_page("Update", "content")
      @page = wiki.find_page("Update")
    end

    after do
      destroy_page("Update")
    end

    context "with valid attributes" do
      it "updates the content of the page" do
        @page.update("new content")
        @page = wiki.find_page("Update")
      end

      it "returns true" do
139
        expect(@page.update("more content")).to be_truthy
140 141 142 143 144 145 146 147 148 149 150 151
      end
    end
  end

  describe "#destroy" do
    before do
      create_page("Delete Page", "content")
      @page = wiki.find_page("Delete Page")
    end

    it "should delete the page" do
      @page.delete
152
      expect(wiki.pages).to be_empty
153 154 155
    end

    it "should return true" do
156
      expect(@page.delete).to eq(true)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    end
  end

  describe "#versions" do
    before do
      create_page("Update", "content")
      @page = wiki.find_page("Update")
    end

    after do
      destroy_page("Update")
    end

    it "returns an array of all commits for the page" do
      3.times { |i| @page.update("content #{i}") }
172
      expect(@page.versions.count).to eq(4)
173 174 175
    end
  end

176 177 178 179 180 181 182 183 184 185 186 187
  describe "#title" do
    before do
      create_page("Title", "content")
      @page = wiki.find_page("Title")
    end

    after do
      destroy_page("Title")
    end

    it "should be replace a hyphen to a space" do
      @page.title = "Import-existing-repositories-into-GitLab"
188
      expect(@page.title).to eq("Import existing repositories into GitLab")
189 190 191
    end
  end

192 193 194 195 196 197 198
  private

  def remove_temp_repo(path)
    FileUtils.rm_rf path
  end

  def commit_details
199
    commit = { name: user.name, email: user.email, message: "test commit" }
200 201 202 203 204 205 206 207 208 209
  end

  def create_page(name, content)
    wiki.wiki.write_page(name, :markdown, content, commit_details)
  end

  def destroy_page(title)
    page = wiki.wiki.paged(title)
    wiki.wiki.delete_page(page, commit_details)
  end
210
end