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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Markup::RenderingService do
describe '#execute' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) do
user = create(:user, username: 'gfm')
project.add_maintainer(user)
user
end
let_it_be(:context) { { project: project } }
let_it_be(:postprocess_context) { { current_user: user } }
let(:file_name) { nil }
let(:text) { 'Noël' }
subject do
described_class
.new(text, file_name: file_name, context: context, postprocess_context: postprocess_context)
.execute
end
context 'when text is missing' do
let(:text) { nil }
it 'returns an empty string' do
is_expected.to eq('')
end
end
context 'when file_name is missing' do
it 'returns html (rendered by Banzai)' do
expected_html = '<p data-sourcepos="1:1-1:5" dir="auto">Noël</p>'
expect(Banzai).to receive(:render).with(text, context) { expected_html }
is_expected.to eq(expected_html)
end
end
context 'when postprocess_context is missing' do
let(:file_name) { 'foo.txt' }
let(:postprocess_context) { nil }
it 'returns html (rendered by Banzai)' do
expected_html = '<pre class="plain-readme">Noël</pre>'
expect(Banzai).not_to receive(:post_process) { expected_html }
is_expected.to eq(expected_html)
end
end
context 'when rendered context is present' do
let(:rendered) { 'rendered text' }
let(:file_name) { 'foo.md' }
it 'returns an empty string' do
context[:rendered] = rendered
is_expected.to eq(rendered)
end
end
context 'when file is a markdown file' do
let(:file_name) { 'foo.md' }
it 'returns html (rendered by Banzai)' do
expected_html = '<p data-sourcepos="1:1-1:5" dir="auto">Noël</p>'
expect(Banzai).to receive(:render).with(text, context) { expected_html }
is_expected.to eq(expected_html)
end
context 'when renderer returns an error' do
before do
allow(Banzai).to receive(:render).and_raise(StandardError, "An error")
end
it 'returns html (rendered by ActionView:TextHelper)' do
is_expected.to eq('<p>Noël</p>')
end
it 'logs the error' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
instance_of(StandardError),
project_id: context[:project].id, file_name: 'foo.md'
)
subject
end
end
end
context 'when file is asciidoc file' do
let(:file_name) { 'foo.adoc' }
it 'returns html (rendered by Gitlab::Asciidoc)' do
expected_html = "<div>\n<p>Noël</p>\n</div>"
expect(Gitlab::Asciidoc).to receive(:render).with(text, context) { expected_html }
is_expected.to eq(expected_html)
end
end
context 'when file is a regular text file' do
let(:file_name) { 'foo.txt' }
let(:text) { 'Noël <form>' }
it 'returns html (rendered by ActionView::TagHelper)' do
expect(ActionController::Base.helpers).to receive(:content_tag).and_call_original
is_expected.to eq('<pre class="plain-readme">Noël <form></pre>')
end
end
context 'when file has an unknown type' do
let(:file_name) { 'foo.tex' }
it 'returns html (rendered by Gitlab::OtherMarkup)' do
expected_html = 'Noël'
expect(Gitlab::OtherMarkup).to receive(:render).with(file_name, text, context) { expected_html }
is_expected.to eq(expected_html)
end
end
context 'when rendering takes too long' do
let(:file_name) { 'foo.bar' }
before do
stub_const("Markup::RenderingService::RENDER_TIMEOUT", 0.1)
allow(Gitlab::OtherMarkup).to receive(:render) do
sleep(0.2)
text
end
end
it 'times out' do
expect(Gitlab::RenderTimeout).to receive(:timeout).and_call_original
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
instance_of(Timeout::Error),
project_id: context[:project].id, file_name: file_name
)
is_expected.to eq("<p>#{text}</p>")
end
context 'when markup_rendering_timeout is disabled' do
it 'waits until the execution completes' do
stub_feature_flags(markup_rendering_timeout: false)
expect(Gitlab::RenderTimeout).not_to receive(:timeout)
is_expected.to eq(text)
end
end
end
end
end
|