2015-09-11 14:41:01 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe BlobHelper do
|
2016-09-13 17:45:13 +05:30
|
|
|
include TreeHelper
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
describe '#highlight' do
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'wraps highlighted content' do
|
|
|
|
expect(helper.highlight('test.rb', '52')).to eq(%q[<pre class="code highlight"><code><span id="LC1" class="line" lang="ruby"><span class="mi">52</span></span></code></pre>])
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'handles plain version' do
|
|
|
|
expect(helper.highlight('test.rb', '52', plain: true)).to eq(%q[<pre class="code highlight"><code><span id="LC1" class="line" lang="">52</span></code></pre>])
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe "#sanitize_svg_data" do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:input_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'unsanitized.svg') }
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:data) { File.read(input_svg_path) }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:expected_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'sanitized.svg') }
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:expected) { File.read(expected_svg_path) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'retains essential elements' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(sanitize_svg_data(data)).to eq(expected)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
describe "#edit_blob_link" do
|
|
|
|
let(:namespace) { create(:namespace, name: 'gitlab' )}
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:project) { create(:project, :repository, namespace: namespace) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
allow(self).to receive(:current_user).and_return(nil)
|
2016-09-13 17:45:13 +05:30
|
|
|
allow(self).to receive(:can_collaborate_with_project?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'verifies blob is text' do
|
|
|
|
expect(helper).not_to receive(:blob_text_viewable?)
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
button = edit_blob_button(project, 'refs/heads/master', 'README.md')
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
expect(button).to start_with('<button')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the passed blob instead retrieve from repository' do
|
|
|
|
blob = project.repository.blob_at('refs/heads/master', 'README.md')
|
|
|
|
|
|
|
|
expect(project.repository).not_to receive(:blob_at)
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
edit_blob_button(project, 'refs/heads/master', 'README.md', blob: blob)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a link with the proper route' do
|
2019-05-18 00:54:41 +05:30
|
|
|
stub_feature_flags(web_ide_default: false)
|
2018-03-27 19:54:05 +05:30
|
|
|
link = edit_blob_button(project, 'master', 'README.md')
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/edit/master/README.md")
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
it 'returns a link with a Web IDE route' do
|
|
|
|
link = edit_blob_button(project, 'master', 'README.md')
|
|
|
|
|
|
|
|
expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/-/ide/project/#{project.full_path}/edit/master/-/README.md")
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'returns a link with the passed link_opts on the expected route' do
|
2019-05-18 00:54:41 +05:30
|
|
|
stub_feature_flags(web_ide_default: false)
|
2018-03-27 19:54:05 +05:30
|
|
|
link = edit_blob_button(project, 'master', 'README.md', link_opts: { mr_id: 10 })
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/edit/master/README.md?mr_id=10")
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
context 'viewer related' do
|
|
|
|
include FakeBlobHelpers
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { build(:project, lfs_enabled: true) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:viewer_class) do
|
|
|
|
Class.new(BlobViewer::Base) do
|
2017-09-10 17:25:29 +05:30
|
|
|
include BlobViewer::ServerSide
|
|
|
|
|
|
|
|
self.collapse_limit = 1.megabyte
|
|
|
|
self.size_limit = 5.megabytes
|
2017-08-17 22:00:37 +05:30
|
|
|
self.type = :rich
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:viewer) { viewer_class.new(blob) }
|
|
|
|
let(:blob) { fake_blob }
|
|
|
|
|
|
|
|
describe '#blob_render_error_reason' do
|
|
|
|
context 'for error :too_large' do
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when the blob size is larger than the absolute size limit' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:blob) { fake_blob(size: 10.megabytes) }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
|
|
|
expect(helper.blob_render_error_reason(viewer)).to eq('it is larger than 5 MB')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when the blob size is larger than the size limit' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:blob) { fake_blob(size: 2.megabytes) }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
|
|
|
expect(helper.blob_render_error_reason(viewer)).to eq('it is larger than 1 MB')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for error :server_side_but_stored_externally' do
|
|
|
|
let(:blob) { fake_blob(lfs: true) }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
|
|
|
expect(helper.blob_render_error_reason(viewer)).to eq('it is stored in LFS')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#blob_render_error_options' do
|
|
|
|
before do
|
|
|
|
assign(:project, project)
|
|
|
|
assign(:blob, blob)
|
|
|
|
assign(:id, File.join('master', blob.path))
|
|
|
|
|
|
|
|
controller.params[:controller] = 'projects/blob'
|
|
|
|
controller.params[:action] = 'show'
|
|
|
|
controller.params[:namespace_id] = project.namespace.to_param
|
|
|
|
controller.params[:project_id] = project.to_param
|
|
|
|
controller.params[:id] = File.join('master', blob.path)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'for error :collapsed' do
|
|
|
|
let(:blob) { fake_blob(size: 2.megabytes) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'includes a "load it anyway" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/load it anyway/)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'for error :too_large' do
|
|
|
|
let(:blob) { fake_blob(size: 10.megabytes) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not include a "load it anyway" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/load it anyway/)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the viewer is rich' do
|
|
|
|
context 'the blob is rendered as text' do
|
|
|
|
let(:blob) { fake_blob(path: 'file.md', size: 2.megabytes) }
|
|
|
|
|
|
|
|
it 'includes a "view the source" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/view the source/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the blob is not rendered as text' do
|
|
|
|
let(:blob) { fake_blob(path: 'file.pdf', binary: true, size: 2.megabytes) }
|
|
|
|
|
|
|
|
it 'does not include a "view the source" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the viewer is not rich' do
|
|
|
|
before do
|
|
|
|
viewer_class.type = :simple
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:blob) { fake_blob(path: 'file.md', size: 2.megabytes) }
|
|
|
|
|
|
|
|
it 'does not include a "view the source" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a "download it" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/download it/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for error :server_side_but_stored_externally' do
|
|
|
|
let(:blob) { fake_blob(path: 'file.md', lfs: true) }
|
|
|
|
|
|
|
|
it 'does not include a "load it anyway" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/load it anyway/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include a "view the source" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a "download it" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/download it/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
describe '#ide_edit_path' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
old_script_name = Rails.application.routes.default_url_options[:script_name]
|
|
|
|
begin
|
|
|
|
example.run
|
|
|
|
ensure
|
|
|
|
Rails.application.routes.default_url_options[:script_name] = old_script_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns full IDE path' do
|
|
|
|
Rails.application.routes.default_url_options[:script_name] = nil
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(helper.ide_edit_path(project, "master", "")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns full IDE path with second -' do
|
|
|
|
Rails.application.routes.default_url_options[:script_name] = nil
|
|
|
|
|
|
|
|
expect(helper.ide_edit_path(project, "testing/slashes", "readme.md")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/slashes/-/readme.md")
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns IDE path without relative_url_root' do
|
|
|
|
Rails.application.routes.default_url_options[:script_name] = "/gitlab"
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(helper.ide_edit_path(project, "master", "")).to eq("/gitlab/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master")
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2019-05-18 00:54:41 +05:30
|
|
|
|
|
|
|
it 'escapes special characters' do
|
|
|
|
Rails.application.routes.default_url_options[:script_name] = nil
|
|
|
|
|
|
|
|
expect(helper.ide_edit_path(project, "testing/#hashes", "readme.md#test")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/#hashes/-/readme.md%23test")
|
|
|
|
expect(helper.ide_edit_path(project, "testing/#hashes", "src#/readme.md#test")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/#hashes/-/src%23/readme.md%23test")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not escape "/" character' do
|
|
|
|
Rails.application.routes.default_url_options[:script_name] = nil
|
|
|
|
|
|
|
|
expect(helper.ide_edit_path(project, "testing/slashes", "readme.md/")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/slashes/-/readme.md/")
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|