2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
RSpec.describe Gitlab::StaticSiteEditor::Config::GeneratedConfig do
|
|
|
|
subject(:config) { described_class.new(repository, ref, path, return_url) }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:namespace) { create(:namespace, name: 'namespace') }
|
2020-07-28 23:09:34 +05:30
|
|
|
let_it_be(:root_group) { create(:group, name: 'group') }
|
|
|
|
let_it_be(:subgroup) { create(:group, name: 'subgroup', parent: root_group) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:project) { create(:project, :public, :repository, name: 'project', namespace: namespace) }
|
2020-07-28 23:09:34 +05:30
|
|
|
let_it_be(:project_with_subgroup) { create(:project, :public, :repository, name: 'project', group: subgroup) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:repository) { project.repository }
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:ref) { 'master' }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:path) { 'README.md' }
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:return_url) { 'http://example.com' }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
describe '#data' do
|
|
|
|
subject { config.data }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it 'returns data for the frontend component' do
|
2020-11-24 15:15:51 +05:30
|
|
|
is_expected
|
|
|
|
.to match({
|
|
|
|
branch: 'master',
|
|
|
|
commit_id: repository.commit.id,
|
|
|
|
namespace: 'namespace',
|
|
|
|
path: 'README.md',
|
|
|
|
project: 'project',
|
|
|
|
project_id: project.id,
|
|
|
|
return_url: 'http://example.com',
|
2021-01-03 14:25:43 +05:30
|
|
|
is_supported_content: true,
|
2020-11-24 15:15:51 +05:30
|
|
|
base_url: '/namespace/project/-/sse/master%2FREADME.md',
|
|
|
|
merge_requests_illustration_path: %r{illustrations/merge_requests}
|
|
|
|
})
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'when namespace is a subgroup' do
|
|
|
|
let(:repository) { project_with_subgroup.repository }
|
|
|
|
|
|
|
|
it 'returns data for the frontend component' do
|
|
|
|
is_expected.to include(
|
|
|
|
namespace: 'group/subgroup',
|
|
|
|
project: 'project',
|
|
|
|
base_url: '/group/subgroup/project/-/sse/master%2FREADME.md'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file has .md.erb extension' do
|
|
|
|
before do
|
|
|
|
repository.create_file(
|
|
|
|
project.creator,
|
2020-11-24 15:15:51 +05:30
|
|
|
path,
|
2020-07-28 23:09:34 +05:30
|
|
|
'',
|
|
|
|
message: 'message',
|
|
|
|
branch_name: 'master'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:path) { 'README.md.erb' }
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it { is_expected.to include(is_supported_content: true) }
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when file path is nested' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:path) { 'lib/README.md' }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
it { is_expected.to include(base_url: '/namespace/project/-/sse/master%2Flib%2FREADME.md') }
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'when branch is not master' do
|
|
|
|
let(:ref) { 'my-branch' }
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it { is_expected.to include(is_supported_content: false) }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file does not have a markdown extension' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:path) { 'README.txt' }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it { is_expected.to include(is_supported_content: false) }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file does not have an extension' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:path) { 'README' }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it { is_expected.to include(is_supported_content: false) }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file does not exist' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:path) { 'UNKNOWN.md' }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it { is_expected.to include(is_supported_content: false) }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is empty' do
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:repository) { create(:project_empty_repo).repository }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it { is_expected.to include(is_supported_content: false) }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2020-05-30 21:06:31 +05:30
|
|
|
|
|
|
|
context 'when return_url is not a valid URL' do
|
|
|
|
let(:return_url) { 'example.com' }
|
|
|
|
|
|
|
|
it { is_expected.to include(return_url: nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when return_url has a javascript scheme' do
|
|
|
|
let(:return_url) { 'javascript:alert(document.domain)' }
|
|
|
|
|
|
|
|
it { is_expected.to include(return_url: nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when return_url is missing' do
|
|
|
|
let(:return_url) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to include(return_url: nil) }
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
context 'when a commit for the ref cannot be found' do
|
|
|
|
let(:ref) { 'nonexistent-ref' }
|
|
|
|
|
|
|
|
it { is_expected.to include(commit_id: nil) }
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|