2020-01-22 07:08:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe SnippetBlobPresenter do
|
2020-02-12 13:09:21 -05:00
|
|
|
describe '#rich_data' do
|
2020-03-02 16:08:01 -05:00
|
|
|
before do
|
|
|
|
allow_next_instance_of(described_class) do |instance|
|
|
|
|
allow(instance).to receive(:current_user).and_return(nil)
|
|
|
|
end
|
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-02-12 13:09:21 -05:00
|
|
|
subject { described_class.new(snippet.blob).rich_data }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
context 'with PersonalSnippet' do
|
2020-08-18 08:10:16 -04:00
|
|
|
let(:snippet) { create(:personal_snippet, :repository) }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
context 'when blob is binary' do
|
|
|
|
it 'returns the HTML associated with the binary' do
|
|
|
|
allow(snippet).to receive(:blob).and_return(snippet.repository.blob_at('master', 'files/images/logo-black.png'))
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
expect(subject).to include('file-content image_file')
|
|
|
|
end
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
context 'with markdown format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test.md', content: '*foo*') }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
it 'returns rich markdown content' do
|
|
|
|
expected = <<~HTML
|
|
|
|
<div class="file-content md">
|
|
|
|
<p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p>
|
|
|
|
</div>
|
|
|
|
HTML
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
expect(subject).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
context 'with notebook format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test.ipynb') }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
it 'returns rich notebook content' do
|
2020-07-23 11:09:28 -04:00
|
|
|
expect(subject.strip).to eq %Q(<div class="file-content" data-endpoint="/-/snippets/#{snippet.id}/raw" id="js-notebook-viewer"></div>)
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with openapi format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'openapi.yml') }
|
|
|
|
|
|
|
|
it 'returns rich openapi content' do
|
2020-07-23 11:09:28 -04:00
|
|
|
expect(subject).to eq %Q(<div class="file-content" data-endpoint="/-/snippets/#{snippet.id}/raw" id="js-openapi-viewer"></div>\n)
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with svg format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test.svg') }
|
|
|
|
|
|
|
|
it 'returns rich svg content' do
|
|
|
|
result = Nokogiri::HTML::DocumentFragment.parse(subject)
|
|
|
|
image_tag = result.search('img').first
|
|
|
|
|
|
|
|
expect(image_tag.attr('src')).to include("data:#{snippet.blob.mime_type};base64")
|
|
|
|
expect(image_tag.attr('alt')).to eq('test.svg')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with other format' do
|
|
|
|
let(:snippet) { create(:personal_snippet, file_name: 'test') }
|
|
|
|
|
|
|
|
it 'does not return no rich content' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2020-02-11 13:08:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-12 13:09:21 -05:00
|
|
|
describe '#plain_data' do
|
2020-02-11 13:08:58 -05:00
|
|
|
let(:snippet) { build(:personal_snippet) }
|
|
|
|
|
2020-02-12 13:09:21 -05:00
|
|
|
subject { described_class.new(snippet.blob).plain_data }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
|
|
|
it 'returns nil when the snippet blob is binary' do
|
|
|
|
allow(snippet.blob).to receive(:binary?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns plain content when snippet file is markup' do
|
|
|
|
snippet.file_name = 'test.md'
|
|
|
|
snippet.content = '*foo*'
|
|
|
|
|
2020-02-12 13:09:21 -05:00
|
|
|
expect(subject).to eq '<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>'
|
2020-02-11 13:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
it 'returns highlighted syntax content' do
|
2020-02-11 13:08:58 -05:00
|
|
|
snippet.file_name = 'test.rb'
|
|
|
|
snippet.content = 'class Foo;end'
|
|
|
|
|
|
|
|
expect(subject)
|
2020-03-02 16:08:01 -05:00
|
|
|
.to eq '<span id="LC1" class="line" lang="ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span><span class="k">end</span></span>'
|
2020-02-11 13:08:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns plain text highlighted content' do
|
|
|
|
snippet.file_name = 'test'
|
|
|
|
snippet.content = 'foo'
|
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
expect(subject).to eq '<span id="LC1" class="line" lang="plaintext">foo</span>'
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
describe 'route helpers' do
|
2020-07-06 14:09:13 -04:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) }
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, :repository, project: project, author: user) }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-07-06 14:09:13 -04:00
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
describe '#raw_path' do
|
|
|
|
subject { described_class.new(snippet.blobs.first, current_user: user).raw_path }
|
|
|
|
|
|
|
|
it_behaves_like 'snippet blob raw path'
|
|
|
|
|
|
|
|
context 'with snippet_multiple_files feature disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(snippet_multiple_files: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
|
|
|
it 'returns the raw path' do
|
|
|
|
expect(subject).to eq "/#{snippet.project.full_path}/-/snippets/#{snippet.id}/raw"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
it 'returns the raw path' do
|
|
|
|
expect(subject).to eq "/-/snippets/#{snippet.id}/raw"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#raw_url' do
|
|
|
|
subject { described_class.new(snippet.blobs.first, current_user: user).raw_url }
|
2020-07-06 14:09:13 -04:00
|
|
|
|
|
|
|
before do
|
2020-08-18 08:10:16 -04:00
|
|
|
stub_default_url_options(host: 'test.host')
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
it_behaves_like 'snippet blob raw url'
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
context 'with snippet_multiple_files feature disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(snippet_multiple_files: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
|
|
|
it 'returns the raw project snippet url' do
|
|
|
|
expect(subject).to eq("http://test.host/#{project_snippet.project.full_path}/-/snippets/#{project_snippet.id}/raw")
|
|
|
|
end
|
2020-07-06 14:09:13 -04:00
|
|
|
end
|
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
2020-07-06 14:09:13 -04:00
|
|
|
|
2020-08-18 08:10:16 -04:00
|
|
|
it 'returns the raw personal snippet url' do
|
|
|
|
expect(subject).to eq("http://test.host/-/snippets/#{personal_snippet.id}/raw")
|
|
|
|
end
|
2020-07-06 14:09:13 -04:00
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|