2018-09-06 05:48:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
RSpec.describe BlobPresenter do
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2021-05-11 14:10:36 -04:00
|
|
|
let_it_be(:user) { project.owner }
|
2021-04-23 08:09:52 -04:00
|
|
|
|
|
|
|
let(:repository) { project.repository }
|
|
|
|
let(:blob) { repository.blob_at('HEAD', 'files/ruby/regex.rb') }
|
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
subject(:presenter) { described_class.new(blob, current_user: user) }
|
2021-04-23 08:09:52 -04:00
|
|
|
|
|
|
|
describe '#web_url' do
|
|
|
|
it { expect(presenter.web_url).to eq("http://localhost/#{project.full_path}/-/blob/#{blob.commit_id}/#{blob.path}") }
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#web_path' do
|
|
|
|
it { expect(presenter.web_path).to eq("/#{project.full_path}/-/blob/#{blob.commit_id}/#{blob.path}") }
|
|
|
|
end
|
2018-09-06 05:48:57 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#edit_blob_path' do
|
|
|
|
it { expect(presenter.edit_blob_path).to eq("/#{project.full_path}/-/edit/#{blob.commit_id}/#{blob.path}") }
|
|
|
|
end
|
2019-06-04 09:38:18 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#raw_path' do
|
|
|
|
it { expect(presenter.raw_path).to eq("/#{project.full_path}/-/raw/#{blob.commit_id}/#{blob.path}") }
|
|
|
|
end
|
2019-06-04 09:38:18 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#replace_path' do
|
|
|
|
it { expect(presenter.replace_path).to eq("/#{project.full_path}/-/create/#{blob.commit_id}/#{blob.path}") }
|
2019-06-04 09:38:18 -04:00
|
|
|
end
|
|
|
|
|
2021-11-26 07:12:49 -05:00
|
|
|
describe '#can_current_user_push_to_branch' do
|
|
|
|
let(:branch_exists) { true }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(project.repository).to receive(:branch_exists?).with(blob.commit_id).and_return(branch_exists)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_current_user_push_to_branch?).to eq(true) }
|
|
|
|
|
|
|
|
context 'current_user is nil' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it { expect(presenter.can_current_user_push_to_branch?).to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'branch does not exist' do
|
|
|
|
let(:branch_exists) { false }
|
|
|
|
|
|
|
|
it { expect(presenter.can_current_user_push_to_branch?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-12 04:10:03 -05:00
|
|
|
describe '#pipeline_editor_path' do
|
|
|
|
context 'when blob is .gitlab-ci.yml' do
|
|
|
|
before do
|
|
|
|
project.repository.create_file(user, '.gitlab-ci.yml', '',
|
|
|
|
message: 'Add a ci file',
|
|
|
|
branch_name: 'main')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:blob) { repository.blob_at('main', '.gitlab-ci.yml') }
|
|
|
|
|
|
|
|
it { expect(presenter.pipeline_editor_path).to eq("/#{project.full_path}/-/ci/editor?branch_name=#{blob.commit_id}") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
describe '#ide_edit_path' do
|
|
|
|
it { expect(presenter.ide_edit_path).to eq("/-/ide/project/#{project.full_path}/edit/HEAD/-/files/ruby/regex.rb") }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fork_and_edit_path' do
|
|
|
|
it 'generates expected URI + query' do
|
|
|
|
uri = URI.parse(presenter.fork_and_edit_path)
|
|
|
|
query = Rack::Utils.parse_query(uri.query)
|
|
|
|
|
|
|
|
expect(uri.path).to eq("/#{project.full_path}/-/forks")
|
|
|
|
expect(query).to include('continue[to]' => presenter.edit_blob_path, 'namespace_key' => user.namespace_id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'current_user is nil' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it { expect(presenter.fork_and_edit_path).to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ide_fork_and_edit_path' do
|
|
|
|
it 'generates expected URI + query' do
|
|
|
|
uri = URI.parse(presenter.ide_fork_and_edit_path)
|
|
|
|
query = Rack::Utils.parse_query(uri.query)
|
|
|
|
|
|
|
|
expect(uri.path).to eq("/#{project.full_path}/-/forks")
|
|
|
|
expect(query).to include('continue[to]' => presenter.ide_edit_path, 'namespace_key' => user.namespace_id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'current_user is nil' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it { expect(presenter.ide_fork_and_edit_path).to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
context 'given a Gitlab::Graphql::Representation::TreeEntry' do
|
|
|
|
let(:blob) { Gitlab::Graphql::Representation::TreeEntry.new(super(), repository) }
|
2020-07-23 11:09:28 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#web_url' do
|
|
|
|
it { expect(presenter.web_url).to eq("http://localhost/#{project.full_path}/-/blob/#{blob.commit_id}/#{blob.path}") }
|
|
|
|
end
|
2020-07-23 11:09:28 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#web_path' do
|
|
|
|
it { expect(presenter.web_path).to eq("/#{project.full_path}/-/blob/#{blob.commit_id}/#{blob.path}") }
|
|
|
|
end
|
2020-07-23 11:09:28 -04:00
|
|
|
end
|
|
|
|
|
2018-09-06 05:48:57 -04:00
|
|
|
describe '#highlight' do
|
2021-04-23 08:09:52 -04:00
|
|
|
let(:git_blob) { blob.__getobj__ }
|
2018-09-06 05:48:57 -04:00
|
|
|
|
|
|
|
it 'returns highlighted content' do
|
2019-08-08 20:13:09 -04:00
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: nil)
|
2018-09-06 05:48:57 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
presenter.highlight
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|
|
|
|
|
2018-09-11 00:37:44 -04:00
|
|
|
it 'returns plain content when :plain is true' do
|
2019-08-08 20:13:09 -04:00
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: true, language: nil)
|
2018-09-06 05:48:57 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
presenter.highlight(plain: true)
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|
|
|
|
|
2019-09-01 23:19:32 -04:00
|
|
|
context '"to" param is present' do
|
|
|
|
before do
|
|
|
|
allow(git_blob)
|
|
|
|
.to receive(:data)
|
|
|
|
.and_return("line one\nline two\nline 3")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns limited highlighted content' do
|
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', "line one\n", plain: nil, language: nil)
|
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
presenter.highlight(to: 1)
|
2019-09-01 23:19:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-06 05:48:57 -04:00
|
|
|
context 'gitlab-language contains a match' do
|
|
|
|
before do
|
|
|
|
allow(blob).to receive(:language_from_gitattributes).and_return('ruby')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes language to inner call' do
|
2019-08-08 20:13:09 -04:00
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: 'ruby')
|
2018-09-06 05:48:57 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
presenter.highlight
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-07 08:10:27 -04:00
|
|
|
|
2021-11-05 20:13:26 -04:00
|
|
|
describe '#highlight_transformed' do
|
|
|
|
context 'when blob is ipynb' do
|
|
|
|
let(:blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') }
|
|
|
|
let(:git_blob) { blob.__getobj__ }
|
|
|
|
|
2021-11-11 22:12:11 -05:00
|
|
|
before do
|
|
|
|
allow(git_blob).to receive(:transformed_for_diff).and_return(true)
|
|
|
|
end
|
|
|
|
|
2021-11-05 20:13:26 -04:00
|
|
|
it 'uses md as the transformed language' do
|
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ipython/markdown-table.ipynb', anything, plain: nil, language: 'md')
|
|
|
|
|
|
|
|
presenter.highlight_transformed
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms the blob' do
|
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ipython/markdown-table.ipynb', include("%%"), plain: nil, language: 'md')
|
|
|
|
|
|
|
|
presenter.highlight_transformed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when blob is other file type' do
|
|
|
|
let(:git_blob) { blob.__getobj__ }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(git_blob)
|
|
|
|
.to receive(:data)
|
|
|
|
.and_return("line one\nline two\nline 3")
|
|
|
|
|
|
|
|
allow(blob).to receive(:language_from_gitattributes).and_return('ruby')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not transform the file' do
|
|
|
|
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: 'ruby')
|
|
|
|
|
|
|
|
presenter.highlight_transformed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
describe '#raw_plain_data' do
|
2021-05-07 08:10:27 -04:00
|
|
|
let(:blob) { repository.blob_at('HEAD', file) }
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
context 'when blob is text' do
|
|
|
|
let(:file) { 'files/ruby/popen.rb' }
|
|
|
|
|
|
|
|
it 'does not include html in the content' do
|
|
|
|
expect(presenter.raw_plain_data.include?('</span>')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#plain_data' do
|
|
|
|
let(:blob) { repository.blob_at('HEAD', file) }
|
2021-05-07 08:10:27 -04:00
|
|
|
|
|
|
|
context 'when blob is binary' do
|
|
|
|
let(:file) { 'files/images/logo-black.png' }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2021-06-28 17:10:13 -04:00
|
|
|
expect(presenter.plain_data).to be_nil
|
2021-05-07 08:10:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when blob is markup' do
|
|
|
|
let(:file) { 'README.md' }
|
|
|
|
|
|
|
|
it 'returns plain content' do
|
2021-06-28 17:10:13 -04:00
|
|
|
expect(presenter.plain_data).to include('<span id="LC1" class="line" lang="markdown">')
|
2021-05-07 08:10:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when blob has syntax' do
|
|
|
|
let(:file) { 'files/ruby/regex.rb' }
|
|
|
|
|
|
|
|
it 'returns highlighted syntax content' do
|
2021-06-28 17:10:13 -04:00
|
|
|
expect(presenter.plain_data)
|
2021-05-07 08:10:27 -04:00
|
|
|
.to include '<span id="LC1" class="line" lang="ruby"><span class="k">module</span> <span class="nn">Gitlab</span>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when blob has plain data' do
|
|
|
|
let(:file) { 'LICENSE' }
|
|
|
|
|
|
|
|
it 'returns plain text highlighted content' do
|
2021-06-28 17:10:13 -04:00
|
|
|
expect(presenter.plain_data).to include('<span id="LC1" class="line" lang="plaintext">The MIT License (MIT)</span>')
|
2021-05-07 08:10:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|