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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe HelpController do
include StubVersion
include DocUrlHelper
let(:user) { create(:user) }
shared_examples 'documentation pages local render' do
it 'renders HTML' do
aggregate_failures do
is_expected.to render_template('help/show')
expect(response.media_type).to eq 'text/html'
end
end
end
shared_examples 'documentation pages redirect' do |documentation_base_url|
let(:gitlab_version) { version }
before do
stub_version(gitlab_version, 'ignored_revision_value')
end
it 'redirects user to custom documentation url with a specified version' do
is_expected.to redirect_to(doc_url(documentation_base_url))
end
context 'when it is a pre-release' do
let(:gitlab_version) { '13.4.0-pre' }
it 'redirects user to custom documentation url without a version' do
is_expected.to redirect_to(doc_url_without_version(documentation_base_url))
end
end
end
before do
sign_in(user)
end
describe 'GET #index' do
context 'with absolute url' do
it 'keeps the URL absolute' do
stub_doc_file_read(content: "[API](/api/README.md)")
get :index
expect(assigns[:help_index]).to eq '[API](/api/README.md)'
end
end
context 'with relative url' do
it 'prefixes it with /help/' do
stub_doc_file_read(content: "[API](api/README.md)")
get :index
expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
end
end
context 'when url is an external link' do
it 'does not change it' do
stub_doc_file_read(content: "[external](https://some.external.link)")
get :index
expect(assigns[:help_index]).to eq '[external](https://some.external.link)'
end
end
context 'when relative url with external on same line' do
it 'prefix it with /help/' do
stub_doc_file_read(content: "[API](api/README.md) [external](https://some.external.link)")
get :index
expect(assigns[:help_index]).to eq '[API](/help/api/README.md) [external](https://some.external.link)'
end
end
context 'when relative url with http:// in query' do
it 'prefix it with /help/' do
stub_doc_file_read(content: "[API](api/README.md?go=https://example.com/)")
get :index
expect(assigns[:help_index]).to eq '[API](/help/api/README.md?go=https://example.com/)'
end
end
context 'when mailto URL' do
it 'do not change it' do
stub_doc_file_read(content: "[report bug](mailto:bugs@example.com)")
get :index
expect(assigns[:help_index]).to eq '[report bug](mailto:bugs@example.com)'
end
end
context 'when protocol-relative link' do
it 'do not change it' do
stub_doc_file_read(content: "[protocol-relative](//example.com)")
get :index
expect(assigns[:help_index]).to eq '[protocol-relative](//example.com)'
end
end
context 'restricted visibility set to public' do
before do
sign_out(user)
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
end
it 'redirects to sign_in path' do
get :index
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when two factor is required' do
before do
stub_two_factor_required
end
it 'does not redirect to two factor auth' do
get :index
expect(response).not_to redirect_to(profile_two_factor_auth_path)
end
end
context 'when requesting help index (underscore prefix test)' do
subject { get :index }
before do
stub_application_setting(help_page_documentation_base_url: '')
end
context 'and the doc/index.md file exists' do
it 'returns index.md' do
expect(subject).to be_successful
expect(assigns[:help_index]).to include('Explore the different areas of the documentation')
end
end
context 'but the doc/index.md file does not exist' do
it 'returns _index.md' do
stub_doc_file_read(content: '_index.md content', file_name: '_index.md')
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with(Rails.root.join('doc/index.md').to_s).and_return(false)
allow(File).to receive(:exist?).with(Rails.root.join('doc/_index.md').to_s).and_return(true)
expect(subject).to be_successful
expect(assigns[:help_index]).to eq '_index.md content'
end
end
end
context 'when requesting help index (frontmatter test)' do
subject { get :index }
before do
stub_application_setting(help_page_documentation_base_url: '')
stub_doc_file_read(content: content)
end
context 'and the doc/index.md file has the level 1 heading in frontmatter' do
let(:content) { "---\ntitle: Test heading\n---\n\nTest content" }
it 'returns content with title in Markdown' do
expect(subject).to be_successful
expect(assigns[:help_index]).to eq "# Test heading\n\nTest content"
end
end
context 'and the doc/index.md file has the level 1 heading in Markdown' do
let(:content) { "# Test heading\n\nTest content" }
it 'returns content with title in Markdown' do
expect(subject).to be_successful
expect(assigns[:help_index]).to eq "# Test heading\n\nTest content"
end
end
end
end
describe 'GET #drawers' do
subject { get :drawers, params: { markdown_file: path } }
context 'when requested file exists' do
let(:path) { 'user/ssh' }
let(:file_name) { "#{path}.md" }
before do
subject
end
it 'assigns variables', :aggregate_failures do
expect(assigns[:path]).not_to be_empty
expect(assigns[:clean_path]).not_to be_empty
end
it 'renders HTML', :aggregate_failures do
is_expected.to render_template('help/drawers')
expect(response.media_type).to eq 'text/html'
end
end
context 'when requested file is missing' do
let(:path) { 'foo/bar' }
it 'renders not found' do
subject
expect(response).to be_not_found
end
end
end
describe 'GET #show' do
context 'for Markdown formats' do
subject { get :show, params: { path: path }, format: :md }
let(:path) { 'user/ssh' }
context 'when requested file exists' do
before do
stub_doc_file_read(file_name: 'user/ssh.md', content: fixture_file('blockquote_fence_legacy_after.md'))
stub_application_setting(help_page_documentation_base_url: '')
subject
end
it 'assigns to @markdown' do
expect(assigns[:markdown]).not_to be_empty
end
it_behaves_like 'documentation pages local render'
context 'when two factor is required' do
before do
stub_two_factor_required
end
it 'does not redirect to two factor auth' do
expect(response).not_to redirect_to(profile_two_factor_auth_path)
end
end
end
context 'when a custom help_page_documentation_url is set in database' do
before do
stub_application_setting(help_page_documentation_base_url: 'https://in-db.gitlab.com')
end
it_behaves_like 'documentation pages redirect', 'https://in-db.gitlab.com'
end
context 'when a custom help_page_documentation_url is set in configuration file' do
let(:host) { 'https://in-yaml.gitlab.com' }
let(:docs_enabled) { true }
before do
allow(Settings).to receive(:gitlab_docs) { double(enabled: docs_enabled, host: host) }
end
it_behaves_like 'documentation pages redirect', 'https://in-yaml.gitlab.com'
context 'when gitlab_docs is disabled' do
let(:docs_enabled) { false }
it_behaves_like 'documentation pages redirect', 'https://docs.gitlab.com'
end
context 'when host is missing' do
let(:host) { nil }
it_behaves_like 'documentation pages redirect', 'https://docs.gitlab.com'
end
end
context 'when help_page_documentation_url is set in both db and configuration file' do
before do
stub_application_setting(help_page_documentation_base_url: 'https://in-db.gitlab.com')
allow(Settings).to receive(:gitlab_docs) { double(enabled: true, host: 'https://in-yaml.gitlab.com') }
end
it_behaves_like 'documentation pages redirect', 'https://in-yaml.gitlab.com'
end
context 'when help_page_documentation_url has a trailing slash' do
before do
allow(Settings).to receive(:gitlab_docs) { double(enabled: true, host: 'https://in-yaml.gitlab.com/') }
end
it_behaves_like 'documentation pages redirect', 'https://in-yaml.gitlab.com'
end
context 'when requested file is missing' do
before do
stub_application_setting(help_page_documentation_base_url: '')
end
it 'renders not found' do
get :show, params: { path: 'foo/bar' }, format: :md
expect(response).to be_not_found
end
end
end
context 'for image formats' do
context 'when requested file exists' do
it 'renders the raw file' do
get :show, params: { path: 'user/img/markdown_logo' }, format: :png
aggregate_failures do
expect(response).to be_successful
expect(response.media_type).to eq 'image/png'
expect(response.headers['Content-Disposition']).to match(/^inline;/)
end
end
end
context 'when requested file is missing' do
it 'renders not found' do
get :show, params: { path: 'foo/bar' }, format: :png
expect(response).to be_not_found
end
end
end
context 'for other formats' do
it 'always renders not found' do
get :show, params: { path: 'user/ssh' }, format: :foo
expect(response).to be_not_found
end
end
context 'when requesting an index.md' do
let(:path) { 'index' }
subject { get :show, params: { path: path }, format: :md }
before do
stub_application_setting(help_page_documentation_base_url: '')
end
context 'and the index.md file exists' do
it 'returns an index.md file' do
expect(subject).to be_successful
expect(assigns[:markdown]).to include('Explore the different areas of the documentation')
end
end
context 'but the index.md file does not exist' do
it 'returns an _index.md file' do
stub_doc_file_read(content: '_index.md content', file_name: '_index.md')
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with(Rails.root.join('doc/index.md').to_s).and_return(false)
allow(File).to receive(:exist?).with(Rails.root.join('doc/_index.md').to_s).and_return(true)
expect(subject).to be_successful
expect(assigns[:markdown]).to eq '_index.md content'
end
end
end
context 'when requesting content' do
subject { get :show, params: { path: 'install/install_methods' }, format: :md }
before do
stub_application_setting(help_page_documentation_base_url: '')
stub_doc_file_read(content: content, file_name: 'install/install_methods.md')
end
context 'and the Markdown file has the level 1 heading in frontmatter' do
let(:content) { "---\ntitle: Test heading\n---\n\nTest content" }
it 'returns content with the level 1 heading in Markdown' do
expect(subject).to be_successful
expect(assigns[:markdown]).to eq "# Test heading\n\nTest content"
end
end
context 'and the Markdown file has the level 1 heading in Markdown' do
let(:content) { "# Test heading\n\nTest content" }
it 'returns content with the level 1 heading in Markdown' do
expect(subject).to be_successful
expect(assigns[:markdown]).to eq "# Test heading\n\nTest content"
end
end
end
end
describe 'GET #docs' do
subject { get :redirect_to_docs }
before do
stub_application_setting(help_page_documentation_base_url: custom_docs_url)
end
context 'with no custom docs URL configured' do
let(:custom_docs_url) { nil }
it 'redirects to docs.gitlab.com' do
subject
expect(response).to redirect_to('https://docs.gitlab.com')
end
end
context 'with a custom docs URL configured' do
let(:custom_docs_url) { 'https://foo.example.com' }
it 'redirects to the configured docs URL' do
subject
expect(response).to redirect_to(custom_docs_url)
end
end
end
def stub_two_factor_required
allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
allow(controller).to receive(:current_user_requires_two_factor?).and_return(true)
end
end
|