2018-09-04 06:39:08 -04:00
|
|
|
# frozen_string_literal: true
|
2022-08-18 05:11:27 -04:00
|
|
|
require 'fast_spec_helper'
|
2018-09-04 06:39:08 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::FileMarkdownLinkBuilder do
|
2018-09-04 06:39:08 -04:00
|
|
|
let(:custom_class) do
|
|
|
|
Class.new do
|
|
|
|
include Gitlab::FileMarkdownLinkBuilder
|
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(custom_class).to receive(:filename).and_return(filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'markdown_link' do
|
2022-08-01 23:09:33 -04:00
|
|
|
let(:url) { "/uploads/#{filename}" }
|
2018-09-04 06:39:08 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(custom_class).to receive(:secure_url).and_return(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file name has the character ]' do
|
|
|
|
let(:filename) { 'd]k.png' }
|
|
|
|
|
|
|
|
it 'escapes the character' do
|
|
|
|
expect(custom_class.markdown_link).to eq '![d\\]k](/uploads/d]k.png)'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-09 08:06:13 -04:00
|
|
|
context 'when file is an image' do
|
|
|
|
let(:filename) { 'my_image.png' }
|
2018-09-04 06:39:08 -04:00
|
|
|
|
|
|
|
it 'returns preview markdown link' do
|
2019-10-09 08:06:13 -04:00
|
|
|
expect(custom_class.markdown_link).to eq '![my_image](/uploads/my_image.png)'
|
2018-09-04 06:39:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-09 08:06:13 -04:00
|
|
|
context 'when file is video' do
|
|
|
|
let(:filename) { 'my_video.mp4' }
|
|
|
|
|
|
|
|
it 'returns preview markdown link' do
|
|
|
|
expect(custom_class.markdown_link).to eq '![my_video](/uploads/my_video.mp4)'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is audio' do
|
|
|
|
let(:filename) { 'my_audio.wav' }
|
|
|
|
|
|
|
|
it 'returns preview markdown link' do
|
|
|
|
expect(custom_class.markdown_link).to eq '![my_audio](/uploads/my_audio.wav)'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is not embeddable' do
|
|
|
|
let(:filename) { 'my_zip.zip' }
|
2018-09-04 06:39:08 -04:00
|
|
|
|
|
|
|
it 'returns markdown link' do
|
2019-10-09 08:06:13 -04:00
|
|
|
expect(custom_class.markdown_link).to eq '[my_zip.zip](/uploads/my_zip.zip)'
|
2018-09-04 06:39:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file name is blank' do
|
|
|
|
let(:filename) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(custom_class.markdown_link).to eq nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'mardown_name' do
|
2019-10-09 08:06:13 -04:00
|
|
|
context 'when file is an image' do
|
|
|
|
let(:filename) { 'my_image.png' }
|
|
|
|
|
|
|
|
it 'retrieves the name without the extension' do
|
|
|
|
expect(custom_class.markdown_name).to eq 'my_image'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is video' do
|
|
|
|
let(:filename) { 'my_video.mp4' }
|
|
|
|
|
|
|
|
it 'retrieves the name without the extension' do
|
|
|
|
expect(custom_class.markdown_name).to eq 'my_video'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is audio' do
|
|
|
|
let(:filename) { 'my_audio.wav' }
|
2018-09-04 06:39:08 -04:00
|
|
|
|
|
|
|
it 'retrieves the name without the extension' do
|
2019-10-09 08:06:13 -04:00
|
|
|
expect(custom_class.markdown_name).to eq 'my_audio'
|
2018-09-04 06:39:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-09 08:06:13 -04:00
|
|
|
context 'when file is not embeddable' do
|
|
|
|
let(:filename) { 'my_zip.zip' }
|
2018-09-04 06:39:08 -04:00
|
|
|
|
|
|
|
it 'retrieves the name with the extesion' do
|
2019-10-09 08:06:13 -04:00
|
|
|
expect(custom_class.markdown_name).to eq 'my_zip.zip'
|
2018-09-04 06:39:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file name is blank' do
|
|
|
|
let(:filename) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(custom_class.markdown_name).to eq nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|