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-09-24 08:09:37 -04:00
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :repository) }
|
|
|
|
|
|
|
|
let(:branch) { snippet.default_branch }
|
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
|
2020-02-12 13:09:21 -05:00
|
|
|
describe '#rich_data' do
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:data_endpoint_url) { "/-/snippets/#{snippet.id}/raw/#{branch}/#{file}" }
|
|
|
|
|
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
|
2020-09-24 08:09:37 -04:00
|
|
|
|
|
|
|
blob.name = File.basename(file)
|
|
|
|
blob.path = file
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
subject { described_class.new(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
|
|
|
context 'when blob is binary' do
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:file) { 'files/images/logo-black.png' }
|
|
|
|
let(:blob) { blob_at(file) }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
it 'returns the HTML associated with the binary' do
|
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
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:file) { 'README.md' }
|
|
|
|
let(:blob) { blob_at(file) }
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
it 'returns rich markdown content' do
|
2020-09-24 08:09:37 -04:00
|
|
|
expect(subject).to include('file-content md')
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-03-02 16:08:01 -05:00
|
|
|
context 'with notebook format' do
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:file) { '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-09-24 08:09:37 -04:00
|
|
|
expect(subject.strip).to eq %Q(<div class="file-content" data-endpoint="#{data_endpoint_url}" id="js-notebook-viewer"></div>)
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with openapi format' do
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:file) { 'openapi.yml' }
|
2020-03-02 16:08:01 -05:00
|
|
|
|
|
|
|
it 'returns rich openapi content' do
|
2020-09-24 08:09:37 -04:00
|
|
|
expect(subject).to eq %Q(<div class="file-content" data-endpoint="#{data_endpoint_url}" id="js-openapi-viewer"></div>\n)
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with svg format' do
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:file) { 'files/images/wm.svg' }
|
|
|
|
let(:blob) { blob_at(file) }
|
2020-03-02 16:08:01 -05:00
|
|
|
|
|
|
|
it 'returns rich svg content' do
|
|
|
|
result = Nokogiri::HTML::DocumentFragment.parse(subject)
|
|
|
|
image_tag = result.search('img').first
|
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
expect(image_tag.attr('src')).to include("data:#{blob.mime_type};base64")
|
|
|
|
expect(image_tag.attr('alt')).to eq(File.basename(file))
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with other format' do
|
2020-09-24 08:09:37 -04:00
|
|
|
let(:file) { 'test' }
|
2020-03-02 16:08:01 -05:00
|
|
|
|
|
|
|
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-09-24 08:09:37 -04:00
|
|
|
let(:blob) { blob_at(file) }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
subject { described_class.new(blob).plain_data }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
context 'when blob is binary' do
|
|
|
|
let(:file) { 'files/images/logo-black.png' }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
2020-02-11 13:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
context 'when blob is markup' do
|
|
|
|
let(:file) { 'README.md' }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
it 'returns plain content' do
|
|
|
|
expect(subject).to include('<span id="LC1" class="line" lang="markdown">')
|
|
|
|
end
|
2020-02-11 13:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
context 'when blob has syntax' do
|
|
|
|
let(:file) { 'files/ruby/regex.rb' }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
it 'returns highlighted syntax content' do
|
|
|
|
expect(subject)
|
|
|
|
.to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
|
|
|
|
end
|
2020-02-11 13:08:58 -05:00
|
|
|
end
|
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
context 'when blob has plain data' do
|
|
|
|
let(:file) { 'LICENSE' }
|
2020-02-11 13:08:58 -05:00
|
|
|
|
2020-09-24 08:09:37 -04:00
|
|
|
it 'returns plain text highlighted content' do
|
|
|
|
expect(subject).to include('<span id="LC1" class="line" lang="plaintext">The MIT License (MIT)</span>')
|
|
|
|
end
|
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-10-08 05:08:40 -04:00
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
|
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
|
2020-10-08 05:08:40 -04:00
|
|
|
subject { described_class.new(blob, current_user: user).raw_path }
|
2020-08-18 08:10:16 -04:00
|
|
|
|
|
|
|
it_behaves_like 'snippet blob raw path'
|
2020-10-08 05:08:40 -04:00
|
|
|
|
|
|
|
context 'with a snippet without a repository' do
|
|
|
|
let(:personal_snippet) { build(:personal_snippet, author: user, id: 1) }
|
|
|
|
let(:project_snippet) { build(:project_snippet, project: project, author: user, id: 1) }
|
|
|
|
let(:blob) { snippet.blob }
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
|
|
|
it 'returns the raw project snippet path' do
|
|
|
|
expect(subject).to eq("/#{project_snippet.project.full_path}/-/snippets/#{project_snippet.id}/raw")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
it 'returns the raw personal snippet path' do
|
|
|
|
expect(subject).to eq("/-/snippets/#{personal_snippet.id}/raw")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-18 08:10:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#raw_url' do
|
2020-10-08 05:08:40 -04:00
|
|
|
subject { described_class.new(blob, 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-10-08 05:08:40 -04:00
|
|
|
|
|
|
|
context 'with a snippet without a repository' do
|
|
|
|
let(:personal_snippet) { build(:personal_snippet, author: user, id: 1) }
|
|
|
|
let(:project_snippet) { build(:project_snippet, project: project, author: user, id: 1) }
|
|
|
|
let(:blob) { snippet.blob }
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
it 'returns the raw personal snippet url' do
|
|
|
|
expect(subject).to eq("http://test.host/-/snippets/#{personal_snippet.id}/raw")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
|
|
|
end
|
2020-09-24 08:09:37 -04:00
|
|
|
|
|
|
|
def blob_at(path)
|
|
|
|
snippet.repository.blob_at(branch, path)
|
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|