user_updates_wiki_page_spec.rb 6.65 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
describe 'User updates wiki page' do
6
  let(:user) { create(:user) }
7

8 9 10 11 12 13
  before do
    project.add_maintainer(user)
    sign_in(user)
  end

  context 'when wiki is empty' do
14
    before do
15 16
      visit(project_wikis_path(project))
      click_link "Create your first page"
17 18
    end

19 20
    context 'in a user namespace' do
      let(:project) { create(:project, :wiki_repo) }
21

22 23 24
      it 'redirects back to the home edit page' do
        page.within(:css, '.wiki-form .form-actions') do
          click_on('Cancel')
25
        end
26

27 28
        expect(current_path).to eq project_wiki_path(project, :home)
      end
29

30
      it 'updates a page that has a path', :js do
31 32 33 34 35 36
        fill_in(:wiki_title, with: 'one/two/three-test')

        page.within '.wiki-form' do
          fill_in(:wiki_content, with: 'wiki content')
          click_on('Create page')
        end
37

38
        expect(current_path).to include('one/two/three-test')
39
        expect(find('.wiki-pages')).to have_content('three')
40

41
        first(:link, text: 'three').click
42

43
        expect(find('.nav-text')).to have_content('three')
44

45
        click_on('Edit')
46

47 48
        expect(current_path).to include('one/two/three-test')
        expect(page).to have_content('Edit Page')
49

50 51
        fill_in('Content', with: 'Updated Wiki Content')
        click_on('Save changes')
52

53
        expect(page).to have_content('Updated Wiki Content')
54
      end
55

56 57 58
      it_behaves_like 'wiki file attachments'
    end
  end
59

60 61 62
  context 'when wiki is not empty' do
    let(:project_wiki) { create(:project_wiki, project: project, user: project.creator) }
    let!(:wiki_page) { create(:wiki_page, wiki: project_wiki, attrs: { title: 'home', content: 'Home page' }) }
Dongqing Hu's avatar
Dongqing Hu committed
63

64 65
    before do
      visit(project_wikis_path(project))
blackst0ne's avatar
blackst0ne committed
66

67 68
      click_link('Edit')
    end
Dongqing Hu's avatar
Dongqing Hu committed
69

70 71
    context 'in a user namespace' do
      let(:project) { create(:project, :wiki_repo) }
Dongqing Hu's avatar
Dongqing Hu committed
72

73
      it 'updates a page', :js do
74
        # Commit message field should have correct value.
75
        expect(page).to have_field('wiki[message]', with: 'Update home')
Dongqing Hu's avatar
Dongqing Hu committed
76

77
        fill_in(:wiki_content, with: 'My awesome wiki!')
78
        click_button('Save changes')
79

80 81 82 83
        expect(page).to have_content('Home')
        expect(page).to have_content("Last edited by #{user.name}")
        expect(page).to have_content('My awesome wiki!')
      end
84

85
      it 'updates the commit message as the title is changed', :js do
86
        fill_in(:wiki_title, with: 'Wiki title')
87

88
        expect(page).to have_field('wiki[message]', with: 'Update Wiki title')
89 90 91
      end

      it 'does not allow XSS', :js do
92
        fill_in(:wiki_title, with: '<script>')
93

94
        expect(page).to have_field('wiki[message]', with: 'Update &lt;script&gt;')
95 96
      end

97
      it 'shows a validation error message' do
98
        fill_in(:wiki_content, with: '')
99
        click_button('Save changes')
100

101 102 103 104
        expect(page).to have_selector('.wiki-form')
        expect(page).to have_content('Edit Page')
        expect(page).to have_content('The form contains the following error:')
        expect(page).to have_content("Content can't be blank")
105
        expect(find('textarea#wiki_content').value).to eq('')
106
      end
107

108
      it 'shows the emoji autocompletion dropdown', :js do
109 110
        find('#wiki_content').native.send_keys('')
        fill_in(:wiki_content, with: ':')
111

112 113
        expect(page).to have_selector('.atwho-view')
      end
114

115 116
      it 'shows the error message' do
        wiki_page.update(content: 'Update')
117

118
        click_button('Save changes')
119

120 121
        expect(page).to have_content('Someone edited the page the same time you did.')
      end
122

123 124 125
      it 'updates a page' do
        fill_in('Content', with: 'Updated Wiki Content')
        click_on('Save changes')
126

127 128
        expect(page).to have_content('Updated Wiki Content')
      end
129

130 131 132
      it 'cancels editing of a page' do
        page.within(:css, '.wiki-form .form-actions') do
          click_on('Cancel')
133
        end
134

135
        expect(current_path).to eq(project_wiki_path(project, wiki_page))
136
      end
137

138 139
      it_behaves_like 'wiki file attachments'
    end
140

141 142
    context 'in a group namespace' do
      let(:project) { create(:project, :wiki_repo, namespace: create(:group, :public)) }
blackst0ne's avatar
blackst0ne committed
143

144
      it 'updates a page', :js do
145
        # Commit message field should have correct value.
146
        expect(page).to have_field('wiki[message]', with: 'Update home')
147

148
        fill_in(:wiki_content, with: 'My awesome wiki!')
149

150
        click_button('Save changes')
151

152 153 154
        expect(page).to have_content('Home')
        expect(page).to have_content("Last edited by #{user.name}")
        expect(page).to have_content('My awesome wiki!')
155
      end
156

157 158 159
      it_behaves_like 'wiki file attachments'
    end
  end
160

161 162 163 164 165 166
  context 'when the page is in a subdir' do
    let!(:project) { create(:project, :wiki_repo) }
    let(:project_wiki) { create(:project_wiki, project: project, user: project.creator) }
    let(:page_name) { 'page_name' }
    let(:page_dir) { "foo/bar/#{page_name}" }
    let!(:wiki_page) { create(:wiki_page, wiki: project_wiki, attrs: { title: page_dir, content: 'Home page' }) }
167

168 169 170
    before do
      visit(project_wiki_edit_path(project, wiki_page))
    end
171

172 173
    it 'moves the page to the root folder' do
      fill_in(:wiki_title, with: "/#{page_name}")
174

175
      click_button('Save changes')
176

177 178
      expect(current_path).to eq(project_wiki_path(project, page_name))
    end
179

180
    it 'moves the page to other dir' do
181 182 183 184 185
      new_page_dir = "foo1/bar1/#{page_name}"

      fill_in(:wiki_title, with: new_page_dir)

      click_button('Save changes')
186

187
      expect(current_path).to eq(project_wiki_path(project, new_page_dir))
188
    end
189

190 191
    it 'remains in the same place if title has not changed' do
      original_path = project_wiki_path(project, wiki_page)
192 193 194 195

      fill_in(:wiki_title, with: page_name)

      click_button('Save changes')
196

197 198
      expect(current_path).to eq(original_path)
    end
199

200
    it 'can be moved to a different dir with a different name' do
201 202 203
      new_page_dir = "foo1/bar1/new_page_name"

      fill_in(:wiki_title, with: new_page_dir)
204

205 206 207
      click_button('Save changes')

      expect(current_path).to eq(project_wiki_path(project, new_page_dir))
208
    end
209

210 211
    it 'can be renamed and moved to the root folder' do
      new_name = 'new_page_name'
212

213 214 215
      fill_in(:wiki_title, with: "/#{new_name}")

      click_button('Save changes')
216

217 218
      expect(current_path).to eq(project_wiki_path(project, new_name))
    end
219

220 221
    it 'squishes the title before creating the page' do
      new_page_dir = "  foo1 /  bar1  /  #{page_name}  "
222

223 224 225
      fill_in(:wiki_title, with: new_page_dir)

      click_button('Save changes')
226

227
      expect(current_path).to eq(project_wiki_path(project, "foo1/bar1/#{page_name}"))
228
    end
229

230
    it_behaves_like 'wiki file attachments'
231
  end
232
end