summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb
blob: 3152dc2ad2f4151eb4c4bd4208480826bb951184 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::WikiPages::FrontMatterParser do
  subject(:parser) { described_class.new(raw_content, gate) }

  let(:content) { 'This is the content' }
  let(:end_divider) { '---' }
  let(:gate) { stub_feature_flag_gate('Gate') }

  let(:with_front_matter) do
    <<~MD
    ---
    a: 1
    b: 2
    c:
     - foo
     - bar
    date: I am safe. Not actually a date
    #{end_divider}
    #{content}
    MD
  end

  def have_correct_front_matter
    include(a: 1, b: 2, c: %w(foo bar))
  end

  describe '#parse' do
    subject { parser.parse }

    context 'there is front matter' do
      let(:raw_content) { with_front_matter }

      it do
        is_expected.to have_attributes(
          front_matter: have_correct_front_matter,
          content: content + "\n",
          error: be_nil
        )
      end
    end

    context 'there is no content' do
      let(:raw_content) { '' }

      it do
        is_expected.to have_attributes(
          front_matter: {},
          content: raw_content,
          error: be_nil
        )
      end
    end

    context 'there is no front_matter' do
      let(:raw_content) { content }

      it { is_expected.to have_attributes(front_matter: be_empty, content: raw_content) }

      it { is_expected.to have_attributes(reason: :no_match) }
    end

    context 'the feature flag is disabled' do
      let(:raw_content) { with_front_matter }

      before do
        stub_feature_flags(Gitlab::WikiPages::FrontMatterParser::FEATURE_FLAG => false)
      end

      it { is_expected.to have_attributes(front_matter: be_empty, content: raw_content) }
    end

    context 'the feature flag is enabled for the gated object' do
      let(:raw_content) { with_front_matter }

      before do
        stub_feature_flags(Gitlab::WikiPages::FrontMatterParser::FEATURE_FLAG => gate)
      end

      it do
        is_expected.to have_attributes(
          front_matter: have_correct_front_matter,
          content: content + "\n",
          reason: be_nil
        )
      end
    end

    context 'the end divider is ...' do
      let(:end_divider) { '...' }
      let(:raw_content) { with_front_matter }

      it { is_expected.to have_attributes(front_matter: have_correct_front_matter) }
    end

    context 'the front-matter is not a mapping' do
      let(:raw_content) do
        <<~MD
        ---
        - thing one
        - thing two
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :not_mapping) }
    end

    context 'there is nothing in the front-matter block' do
      let(:raw_content) do
        <<~MD
        ---
        ---
        My content here
        MD
      end

      it { is_expected.to have_attributes(reason: :no_match) }
    end

    context 'there is a string in the YAML block' do
      let(:raw_content) do
        <<~MD
        ---
        This is a string
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :not_mapping) }
    end

    context 'there is dangerous YAML in the block' do
      let(:raw_content) do
        <<~MD
        ---
        date: 2010-02-11 11:02:57
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :parse_error, error: be_present) }
    end

    context 'there is acceptably long YAML in the front-matter block' do
      let(:raw_content) do
        key = 'title: '
        length = described_class::MAX_FRONT_MATTER_LENGTH - key.size

        <<~MD
        ---
        title: #{FFaker::Lorem.characters(length)}
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(front_matter: include(title: be_present)) }
    end

    context 'there is suspiciously long YAML in the front-matter block' do
      let(:raw_content) do
        <<~MD
        ---
        title: #{FFaker::Lorem.characters(described_class::MAX_FRONT_MATTER_LENGTH)}
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :too_long) }
    end

    context 'TOML front matter' do
      let(:raw_content) do
        <<~MD
        +++
        title = "My title"
        +++
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :not_yaml) }
    end

    context 'TOML style fences, advertised as YAML' do
      let(:raw_content) do
        <<~MD
        +++ yaml
        title: "My title"
        +++
        #{content}
        MD
      end

      it { is_expected.to have_attributes(front_matter: include(title: 'My title')) }
    end

    context 'YAML, advertised as something else' do
      let(:raw_content) do
        <<~MD
        --- toml
        title: My title
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :not_yaml) }
    end

    context 'there is text content in the YAML block, in comments' do
      let(:raw_content) do
        <<~MD
        ---
        # This is YAML
        #
        # It has comments though. Explaining things
        foo: 1

        ## It has headings

        headings:

         - heading one
         - heading two

        # And lists

        lists:

         - and lists
         - with things in them
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(front_matter: include(foo: 1)) }
    end

    context 'there is text content in the YAML block' do
      let(:raw_content) do
        <<~MD
        ---
        # This is not YAML

        In fact is looks like markdown

        ## It has headings

        Paragraphs

        - and lists
        - with things in them
        ---
        #{content}
        MD
      end

      it { is_expected.to have_attributes(reason: :not_mapping) }
    end
  end
end