summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/wikis_controller_spec.rb
blob: b974d927856b6b143c68fc2a7bdd04656bdae137 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
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
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
216
217
218
219
220
221
222
require 'spec_helper'

describe Projects::WikisController do
  let(:project) { create(:project, :public, :repository) }
  let(:user) { project.owner }
  let(:project_wiki) { ProjectWiki.new(project, user) }
  let(:wiki) { project_wiki.wiki }
  let(:wiki_title) { 'page-title-test' }

  before do
    create_page(wiki_title, 'hello world')

    sign_in(user)
  end

  after do
    destroy_page(wiki_title)
  end

  describe 'GET #show' do
    render_views

    subject { get :show, namespace_id: project.namespace, project_id: project, id: wiki_title }

    it 'limits the retrieved pages for the sidebar' do
      expect(controller).to receive(:load_wiki).and_return(project_wiki)

      # empty? call
      expect(project_wiki).to receive(:pages).with(limit: 1).and_call_original
      # Sidebar entries
      expect(project_wiki).to receive(:pages).with(limit: 15).and_call_original

      subject

      expect(response).to have_http_status(:ok)
      expect(response.body).to include(wiki_title)
    end

    context 'when page content encoding is invalid' do
      it 'sets flash error' do
        allow(controller).to receive(:valid_encoding?).and_return(false)

        subject

        expect(response).to have_http_status(:ok)
        expect(flash[:notice]).to eq 'The content of this page is not encoded in UTF-8. Edits can only be made via the Git repository.'
      end
    end

    context 'when page is a file' do
      include WikiHelpers

      let(:path) { upload_file_to_wiki(project, user, file_name) }

      subject { get :show, namespace_id: project.namespace, project_id: project, id: path }

      context 'when file is an image' do
        let(:file_name) { 'dk.png' }

        context 'when feature flag workhorse_set_content_type is' do
          before do
            stub_feature_flags(workhorse_set_content_type: flag_value)

            subject
          end

          context 'enabled' do
            let(:flag_value) { true }

            it 'delivers the image' do
              expect(response.headers['Content-Type']).to eq('image/png')
              expect(response.headers['Content-Disposition']).to match(/^inline/)
              expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
            end

            context 'when file is a svg' do
              let(:file_name) { 'unsanitized.svg' }

              it 'delivers the image' do
                expect(response.headers['Content-Type']).to eq('image/svg+xml')
                expect(response.headers['Content-Disposition']).to match(/^attachment/)
                expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
              end
            end
          end

          context 'disabled' do
            let(:flag_value) { false }

            it 'renders the content inline' do
              expect(response.headers['Content-Type']).to eq('image/png')
              expect(response.headers['Content-Disposition']).to match(/^inline/)
              expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq nil
            end

            context 'when file is a svg' do
              let(:file_name) { 'unsanitized.svg' }

              it 'renders the content as an attachment' do
                expect(response.headers['Content-Type']).to eq('image/svg+xml')
                expect(response.headers['Content-Disposition']).to match(/^attachment/)
                expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq nil
              end
            end
          end
        end
      end

      context 'when file is a pdf' do
        let(:file_name) { 'git-cheat-sheet.pdf' }

        context 'when feature flag workhorse_set_content_type is' do
          before do
            stub_feature_flags(workhorse_set_content_type: flag_value)

            subject
          end

          context 'enabled' do
            let(:flag_value) { true }

            it 'sets the content type to sets the content response headers' do
              expect(response.headers['Content-Type']).to eq 'application/octet-stream'
              expect(response.headers['Content-Disposition']).to match(/^inline/)
              expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
            end
          end

          context 'disabled' do
            let(:flag_value) { false }

            it 'sets the content response headers' do
              expect(response.headers['Content-Type']).to eq 'application/octet-stream'
              expect(response.headers['Content-Disposition']).to match(/^inline/)
              expect(response.headers[Gitlab::Workhorse::DETECT_HEADER]).to eq nil
            end
          end
        end
      end
    end
  end

  describe 'POST #preview_markdown' do
    it 'renders json in a correct format' do
      post :preview_markdown, namespace_id: project.namespace, project_id: project, id: 'page/path', text: '*Markdown* text'

      expect(JSON.parse(response.body).keys).to match_array(%w(body references))
    end
  end

  describe 'GET #edit' do
    subject { get(:edit, namespace_id: project.namespace, project_id: project, id: wiki_title) }

    context 'when page content encoding is invalid' do
      it 'redirects to show' do
        allow(controller).to receive(:valid_encoding?).and_return(false)

        subject

        expect(response).to redirect_to(project_wiki_path(project, project_wiki.pages.first))
      end
    end

    context 'when page content encoding is valid' do
      render_views

      it 'shows the edit page' do
        subject

        expect(response).to have_http_status(:ok)
        expect(response.body).to include('Edit Page')
      end
    end
  end

  describe 'PATCH #update' do
    let(:new_title) { 'New title' }
    let(:new_content) { 'New content' }
    subject do
      patch(:update,
            namespace_id: project.namespace,
            project_id: project,
            id: wiki_title,
            wiki: { title: new_title, content: new_content })
    end

    context 'when page content encoding is invalid' do
      it 'redirects to show' do
        allow(controller).to receive(:valid_encoding?).and_return(false)

        subject
        expect(response).to redirect_to(project_wiki_path(project, project_wiki.pages.first))
      end
    end

    context 'when page content encoding is valid' do
      render_views

      it 'updates the page' do
        subject

        wiki_page = project_wiki.pages.first

        expect(wiki_page.title).to eq new_title
        expect(wiki_page.content).to eq new_content
      end
    end
  end

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

  def commit_details(name)
    Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "created page #{name}")
  end

  def destroy_page(title, dir = '')
    page = wiki.page(title: title, dir: dir)
    project_wiki.delete_page(page, "test commit")
  end
end