user_creates_wiki_page_spec.rb 9.61 KB
Newer Older
1
require "spec_helper"
2

3
describe "User creates wiki page" do
4
  let(:user) { create(:user) }
5 6
  let(:wiki) { ProjectWiki.new(project, user) }
  let(:project) { create(:project) }
7

8
  before do
9
    project.add_maintainer(user)
10

11
    sign_in(user)
12 13
  end

14
  context "when wiki is empty" do
15 16
    before do
      visit(project_wikis_path(project))
17 18

      click_link "Create your first page"
19 20
    end

21
    context "in a user namespace" do
22
      let(:project) { create(:project, :wiki_repo, namespace: user.namespace) }
23

24 25 26
      it "shows validation error message" do
        page.within(".wiki-form") do
          fill_in(:wiki_content, with: "")
27

28
          click_on("Create page")
29 30
        end

31 32 33 34
        expect(page).to have_content("The form contains the following error:").and have_content("Content can't be blank")

        page.within(".wiki-form") do
          fill_in(:wiki_content, with: "[link test](test)")
35

36
          click_on("Create page")
37 38
        end

39
        expect(page).to have_content("Home").and have_content("link test")
40

41
        click_link("link test")
42

43
        expect(page).to have_content("Create Page")
44 45
      end

46 47
      it "shows non-escaped link in the pages list", :js do
        click_link("New page")
48

49 50 51 52
        page.within("#modal-new-wiki") do
          fill_in(:new_wiki_path, with: "one/two/three-test")

          click_on("Create page")
53 54
        end

55 56 57 58
        page.within(".wiki-form") do
          fill_in(:wiki_content, with: "wiki content")

          click_on("Create page")
59 60
        end

61
        expect(current_path).to include("one/two/three-test")
62
        expect(page).to have_xpath("//a[@href='/#{project.full_path}/wikis/one/two/three-test']")
63 64
      end

65 66
      it "has `Create home` as a commit message" do
        expect(page).to have_field("wiki[message]", with: "Create home")
blackst0ne's avatar
blackst0ne committed
67 68
      end

69 70 71
      it "creates a page from the home page" do
        fill_in(:wiki_content, with: "[test](test)\n[GitLab API doc](api)\n[Rake tasks](raketasks)\n# Wiki header\n")
        fill_in(:wiki_message, with: "Adding links to wiki")
72

73 74
        page.within(".wiki-form") do
          click_button("Create page")
75
        end
76

77 78 79 80 81 82 83
        expect(current_path).to eq(project_wiki_path(project, "home"))
        expect(page).to have_content("test GitLab API doc Rake tasks Wiki header")
                   .and have_content("Home")
                   .and have_content("Last edited by #{user.name}")
                   .and have_header_with_correct_id_and_link(1, "Wiki header", "wiki-header")

        click_link("test")
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        expect(current_path).to eq(project_wiki_path(project, "test"))

        page.within(:css, ".nav-text") do
          expect(page).to have_content("Test").and have_content("Create Page")
        end

        click_link("Home")

        expect(current_path).to eq(project_wiki_path(project, "home"))

        click_link("GitLab API")

        expect(current_path).to eq(project_wiki_path(project, "api"))

        page.within(:css, ".nav-text") do
          expect(page).to have_content("Create").and have_content("Api")
101
        end
102

103 104 105 106 107 108 109 110 111 112 113
        click_link("Home")

        expect(current_path).to eq(project_wiki_path(project, "home"))

        click_link("Rake tasks")

        expect(current_path).to eq(project_wiki_path(project, "raketasks"))

        page.within(:css, ".nav-text") do
          expect(page).to have_content("Create").and have_content("Rake")
        end
114
      end
115

116 117
      it "creates ASCII wiki with LaTeX blocks", :js do
        stub_application_setting(plantuml_url: "http://localhost", plantuml_enabled: true)
118 119 120 121 122 123

        ascii_content = <<~MD
          :stem: latexmath

          [stem]
          ++++
124
          \\sqrt{4} = 2
125 126 127 128 129 130
          ++++

          another part

          [latexmath]
          ++++
131
          \\beta_x \\gamma
132 133 134 135 136
          ++++

          stem:[2+2] is 4
        MD

137 138
        find("#wiki_format option[value=asciidoc]").select_option

139
        fill_in(:wiki_content, with: ascii_content)
140

141 142
        page.within(".wiki-form") do
          click_button("Create page")
143 144
        end

145 146
        page.within ".wiki" do
          expect(page).to have_selector(".katex", count: 3).and have_content("2+2 is 4")
147 148
        end
      end
149 150
    end

151
    context "in a group namespace", :js do
152
      let(:project) { create(:project, :wiki_repo, namespace: create(:group, :public)) }
153

154 155
      it "has `Create home` as a commit message" do
        expect(page).to have_field("wiki[message]", with: "Create home")
156 157
      end

158 159 160 161 162
      it "creates a page from from the home page" do
        page.within(".wiki-form") do
          fill_in(:wiki_content, with: "My awesome wiki!")

          click_button("Create page")
163 164
        end

165 166 167
        expect(page).to have_content("Home")
                   .and have_content("Last edited by #{user.name}")
                   .and have_content("My awesome wiki!")
168
      end
169 170 171
    end
  end

172
  context "when wiki is not empty", :js do
173
    before do
174 175 176
      create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: 'Home page' })

      visit(project_wikis_path(project))
177 178
    end

179
    context "in a user namespace" do
180
      let(:project) { create(:project, :wiki_repo, namespace: user.namespace) }
181

182 183 184
      context "via the `new wiki page` page" do
        it "creates a page with a single word" do
          click_link("New page")
185

186 187 188 189
          page.within("#modal-new-wiki") do
            fill_in(:new_wiki_path, with: "foo")

            click_button("Create page")
190
          end
191

blackst0ne's avatar
blackst0ne committed
192
          # Commit message field should have correct value.
193 194 195 196
          expect(page).to have_field("wiki[message]", with: "Create foo")

          page.within(".wiki-form") do
            fill_in(:wiki_content, with: "My awesome wiki!")
blackst0ne's avatar
blackst0ne committed
197

198
            click_button("Create page")
199
          end
200

201 202 203
          expect(page).to have_content("Foo")
                     .and have_content("Last edited by #{user.name}")
                     .and have_content("My awesome wiki!")
204 205
        end

206 207
        it "creates a page with spaces in the name" do
          click_link("New page")
208

209 210 211 212
          page.within("#modal-new-wiki") do
            fill_in(:new_wiki_path, with: "Spaces in the name")

            click_button("Create page")
213
          end
214

blackst0ne's avatar
blackst0ne committed
215
          # Commit message field should have correct value.
216 217 218 219
          expect(page).to have_field("wiki[message]", with: "Create spaces in the name")

          page.within(".wiki-form") do
            fill_in(:wiki_content, with: "My awesome wiki!")
blackst0ne's avatar
blackst0ne committed
220

221
            click_button("Create page")
222
          end
223

224 225 226
          expect(page).to have_content("Spaces in the name")
                     .and have_content("Last edited by #{user.name}")
                     .and have_content("My awesome wiki!")
227 228
        end

229 230
        it "creates a page with hyphens in the name" do
          click_link("New page")
231

232 233 234 235
          page.within("#modal-new-wiki") do
            fill_in(:new_wiki_path, with: "hyphens-in-the-name")

            click_button("Create page")
236
          end
237

blackst0ne's avatar
blackst0ne committed
238
          # Commit message field should have correct value.
239 240 241 242
          expect(page).to have_field("wiki[message]", with: "Create hyphens in the name")

          page.within(".wiki-form") do
            fill_in(:wiki_content, with: "My awesome wiki!")
blackst0ne's avatar
blackst0ne committed
243

244
            click_button("Create page")
245
          end
246

247 248 249
          expect(page).to have_content("Hyphens in the name")
                     .and have_content("Last edited by #{user.name}")
                     .and have_content("My awesome wiki!")
250
        end
251
      end
252

253
      it "shows the emoji autocompletion dropdown" do
254
        click_link("New page")
255

256 257 258 259
        page.within("#modal-new-wiki") do
          fill_in(:new_wiki_path, with: "test-autocomplete")

          click_button("Create page")
260 261
        end

262 263 264
        page.within(".wiki-form") do
          find("#wiki_content").native.send_keys("")

265
          fill_in(:wiki_content, with: ":")
266 267
        end

268
        expect(page).to have_selector(".atwho-view")
269
      end
270 271
    end

272
    context "in a group namespace" do
273
      let(:project) { create(:project, :wiki_repo, namespace: create(:group, :public)) }
blackst0ne's avatar
blackst0ne committed
274

275 276 277
      context "via the `new wiki page` page" do
        it "creates a page" do
          click_link("New page")
278

279 280 281 282
          page.within("#modal-new-wiki") do
            fill_in(:new_wiki_path, with: "foo")

            click_button("Create page")
283
          end
284

285
          # Commit message field should have correct value.
286 287 288 289
          expect(page).to have_field("wiki[message]", with: "Create foo")

          page.within(".wiki-form") do
            fill_in(:wiki_content, with: "My awesome wiki!")
290

291
            click_button("Create page")
292
          end
blackst0ne's avatar
blackst0ne committed
293

294 295 296
          expect(page).to have_content("Foo")
                     .and have_content("Last edited by #{user.name}")
                     .and have_content("My awesome wiki!")
297
        end
298 299 300
      end
    end
  end
301

302 303
  describe 'sidebar feature' do
    context 'when there are some existing pages' do
304
      before do
305 306
        create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: 'home' })
        create(:wiki_page, wiki: wiki, attrs: { title: 'another', content: 'another' })
307 308
      end

309
      it 'renders a default sidebar when there is no customized sidebar' do
310 311
        visit(project_wikis_path(project))

312 313 314 315 316 317 318 319 320 321 322 323 324
        expect(page).to have_content('Another')
        expect(page).to have_content('More Pages')
      end

      context 'when there is a customized sidebar' do
        before do
          create(:wiki_page, wiki: wiki, attrs: { title: '_sidebar', content: 'My customized sidebar' })
        end

        it 'renders my customized sidebar instead of the default one' do
          visit(project_wikis_path(project))

          expect(page).to have_content('My customized sidebar')
325
          expect(page).to have_content('More Pages')
326 327
          expect(page).not_to have_content('Another')
        end
328 329 330
      end
    end
  end
331
end