2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 23:08:05 -04:00
|
|
|
RSpec.describe HelpController do
|
2020-10-07 20:08:30 -04:00
|
|
|
include StubVersion
|
2022-05-30 08:08:23 -04:00
|
|
|
include DocUrlHelper
|
2020-10-07 20:08:30 -04:00
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
shared_examples 'documentation pages local render' do
|
|
|
|
it 'renders HTML' do
|
|
|
|
aggregate_failures do
|
2022-05-30 08:08:23 -04:00
|
|
|
is_expected.to render_template('help/show')
|
2021-02-11 16:09:00 -05:00
|
|
|
expect(response.media_type).to eq 'text/html'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'documentation pages redirect' do |documentation_base_url|
|
2022-05-30 08:08:23 -04:00
|
|
|
let(:gitlab_version) { version }
|
2021-02-11 16:09:00 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_version(gitlab_version, 'ignored_revision_value')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects user to custom documentation url with a specified version' do
|
2022-05-30 08:08:23 -04:00
|
|
|
is_expected.to redirect_to(doc_url(documentation_base_url))
|
2021-02-11 16:09:00 -05:00
|
|
|
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
|
2022-05-30 08:08:23 -04:00
|
|
|
is_expected.to redirect_to(doc_url_without_version(documentation_base_url))
|
2021-02-11 16:09:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2016-11-10 00:57:46 -05:00
|
|
|
describe 'GET #index' do
|
2016-12-01 06:07:52 -05:00
|
|
|
context 'with absolute url' do
|
|
|
|
it 'keeps the URL absolute' do
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[API](/api/README.md)")
|
2016-12-01 06:07:52 -05:00
|
|
|
|
2016-11-10 00:57:46 -05:00
|
|
|
get :index
|
2016-12-01 06:07:52 -05:00
|
|
|
|
|
|
|
expect(assigns[:help_index]).to eq '[API](/api/README.md)'
|
2016-11-10 00:57:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-01 06:07:52 -05:00
|
|
|
context 'with relative url' do
|
|
|
|
it 'prefixes it with /help/' do
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[API](api/README.md)")
|
2016-12-01 06:07:52 -05:00
|
|
|
|
2016-11-10 00:57:46 -05:00
|
|
|
get :index
|
2016-12-01 06:07:52 -05:00
|
|
|
|
|
|
|
expect(assigns[:help_index]).to eq '[API](/help/api/README.md)'
|
2016-11-10 00:57:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-18 06:57:25 -05:00
|
|
|
context 'when url is an external link' do
|
2016-12-01 06:07:52 -05:00
|
|
|
it 'does not change it' do
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[external](https://some.external.link)")
|
2016-12-01 06:07:52 -05:00
|
|
|
|
2016-11-10 00:57:46 -05:00
|
|
|
get :index
|
2016-12-01 06:07:52 -05:00
|
|
|
|
2016-11-18 06:57:25 -05:00
|
|
|
expect(assigns[:help_index]).to eq '[external](https://some.external.link)'
|
2016-11-10 00:57:46 -05:00
|
|
|
end
|
|
|
|
end
|
2019-02-11 16:20:39 -05:00
|
|
|
|
|
|
|
context 'when relative url with external on same line' do
|
|
|
|
it 'prefix it with /help/' do
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[API](api/README.md) [external](https://some.external.link)")
|
2019-02-11 16:20:39 -05:00
|
|
|
|
|
|
|
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
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[API](api/README.md?go=https://example.com/)")
|
2019-02-11 16:20:39 -05:00
|
|
|
|
|
|
|
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
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[report bug](mailto:bugs@example.com)")
|
2019-02-11 16:20:39 -05:00
|
|
|
|
|
|
|
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
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(content: "[protocol-relative](//example.com)")
|
2019-02-11 16:20:39 -05:00
|
|
|
|
|
|
|
get :index
|
|
|
|
|
|
|
|
expect(assigns[:help_index]).to eq '[protocol-relative](//example.com)'
|
|
|
|
end
|
|
|
|
end
|
2020-04-15 11:09:17 -04:00
|
|
|
|
|
|
|
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
|
2021-02-22 07:10:38 -05:00
|
|
|
|
|
|
|
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
|
2016-11-10 00:57:46 -05:00
|
|
|
end
|
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
describe 'GET #show' do
|
|
|
|
context 'for Markdown formats' do
|
2021-02-11 16:09:00 -05:00
|
|
|
subject { get :show, params: { path: path }, format: :md }
|
|
|
|
|
2022-03-31 02:08:34 -04:00
|
|
|
let(:path) { 'user/ssh' }
|
2021-02-11 16:09:00 -05:00
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
context 'when requested file exists' do
|
|
|
|
before do
|
2022-05-30 08:08:23 -04:00
|
|
|
stub_doc_file_read(file_name: 'user/ssh.md', content: fixture_file('blockquote_fence_after.md'))
|
2020-11-19 16:09:07 -05:00
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
subject
|
2015-04-15 12:45:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns to @markdown' do
|
|
|
|
expect(assigns[:markdown]).not_to be_empty
|
|
|
|
end
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
it_behaves_like 'documentation pages local render'
|
2021-02-22 07:10:38 -05:00
|
|
|
|
|
|
|
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
|
2020-10-07 20:08:30 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
context 'when a custom help_page_documentation_url is set in database' do
|
2020-10-07 20:08:30 -04:00
|
|
|
before do
|
2021-02-11 16:09:00 -05:00
|
|
|
stub_application_setting(help_page_documentation_base_url: 'https://in-db.gitlab.com')
|
2020-10-07 20:08:30 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
it_behaves_like 'documentation pages redirect', 'https://in-db.gitlab.com'
|
|
|
|
end
|
2020-10-07 20:08:30 -04:00
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
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 }
|
2020-10-07 20:08:30 -04:00
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
before do
|
|
|
|
allow(Settings).to receive(:gitlab_docs) { double(enabled: docs_enabled, host: host) }
|
2020-10-07 20:08:30 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
it_behaves_like 'documentation pages redirect', 'https://in-yaml.gitlab.com'
|
|
|
|
|
|
|
|
context 'when gitlab_docs is disabled' do
|
|
|
|
let(:docs_enabled) { false }
|
2020-10-07 20:08:30 -04:00
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
it_behaves_like 'documentation pages local render'
|
2020-10-07 20:08:30 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
context 'when host is missing' do
|
|
|
|
let(:host) { nil }
|
2020-10-07 20:08:30 -04:00
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
it_behaves_like 'documentation pages local render'
|
|
|
|
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') }
|
2020-10-07 20:08:30 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 16:09:00 -05:00
|
|
|
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/') }
|
2015-04-15 12:45:31 -04:00
|
|
|
end
|
2021-02-11 16:09:00 -05:00
|
|
|
|
|
|
|
it_behaves_like 'documentation pages redirect', 'https://in-yaml.gitlab.com'
|
2015-04-15 12:45:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requested file is missing' do
|
|
|
|
it 'renders not found' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { path: 'foo/bar' }, format: :md
|
2015-04-15 12:45:31 -04:00
|
|
|
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
|
2015-06-23 01:24:39 -04:00
|
|
|
get :show,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
2020-02-03 10:08:45 -05:00
|
|
|
path: 'user/img/markdown_logo'
|
2018-12-17 17:52:17 -05:00
|
|
|
},
|
2015-06-23 01:24:39 -04:00
|
|
|
format: :png
|
2020-10-07 20:08:30 -04:00
|
|
|
|
|
|
|
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
|
2015-04-15 12:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requested file is missing' do
|
|
|
|
it 'renders not found' do
|
2015-06-23 01:24:39 -04:00
|
|
|
get :show,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
path: 'foo/bar'
|
|
|
|
},
|
2015-06-23 01:24:39 -04:00
|
|
|
format: :png
|
2015-04-15 12:45:31 -04:00
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for other formats' do
|
|
|
|
it 'always renders not found' do
|
2015-06-23 01:24:39 -04:00
|
|
|
get :show,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
2022-03-31 02:08:34 -04:00
|
|
|
path: 'user/ssh'
|
2018-12-17 17:52:17 -05:00
|
|
|
},
|
2015-06-23 01:24:39 -04:00
|
|
|
format: :foo
|
2015-04-15 12:45:31 -04:00
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-19 13:17:14 -04:00
|
|
|
|
2021-02-22 07:10:38 -05:00
|
|
|
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
|
2015-04-15 12:45:31 -04:00
|
|
|
end
|