diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-15 03:09:11 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-15 03:09:11 +0000 |
commit | b71a496c7a3e109f7c85ad7ac453e6f7bf7cda45 (patch) | |
tree | 0a76fc00ef860bd369dcaa3f136ee36275eb47f5 /spec/lib | |
parent | c2041156b8b3063d6cf29b324416e8469e588923 (diff) | |
download | gitlab-ce-b71a496c7a3e109f7c85ad7ac453e6f7bf7cda45.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/middleware/multipart_spec.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb | 274 |
2 files changed, 285 insertions, 0 deletions
diff --git a/spec/lib/gitlab/middleware/multipart_spec.rb b/spec/lib/gitlab/middleware/multipart_spec.rb index 1d3ad3afb56..ec153e25d44 100644 --- a/spec/lib/gitlab/middleware/multipart_spec.rb +++ b/spec/lib/gitlab/middleware/multipart_spec.rb @@ -89,6 +89,17 @@ describe Gitlab::Middleware::Multipart do end end + it 'allows files in the job artifact upload path' do + with_tmp_dir('artifacts') do |dir, env| + expect(JobArtifactUploader).to receive(:workhorse_upload_path).and_return(File.join(dir, 'artifacts')) + expect(app).to receive(:call) do |env| + expect(get_params(env)['file']).to be_a(::UploadedFile) + end + + middleware.call(env) + end + end + it 'allows symlinks for uploads dir' do Tempfile.open('two-levels') do |tempfile| symlinked_dir = '/some/dir/uploads' diff --git a/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb b/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb new file mode 100644 index 00000000000..c606ba11b9c --- /dev/null +++ b/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb @@ -0,0 +1,274 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::WikiPages::FrontMatterParser do + subject(:parser) { described_class.new(raw_content, gate) } + + let(:content) { 'This is the content' } + let(:end_divider) { '---' } + let(:gate) { double('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 => false) + stub_feature_flags(Gitlab::WikiPages::FrontMatterParser::FEATURE_FLAG => { + enabled: true, + thing: 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: :not_mapping) } + 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 |