2016-09-12 11:25:35 -04:00
|
|
|
|
# encoding: utf-8
|
2016-02-18 18:19:43 -05:00
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
describe Blob do
|
|
|
|
|
describe '.decorate' do
|
|
|
|
|
it 'returns NilClass when given nil' do
|
|
|
|
|
expect(described_class.decorate(nil)).to be_nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-09-12 11:25:35 -04:00
|
|
|
|
describe '#data' do
|
|
|
|
|
context 'using a binary blob' do
|
|
|
|
|
it 'returns the data as-is' do
|
|
|
|
|
data = "\n\xFF\xB9\xC3"
|
|
|
|
|
blob = described_class.new(double(binary?: true, data: data))
|
|
|
|
|
|
|
|
|
|
expect(blob.data).to eq(data)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'using a text blob' do
|
|
|
|
|
it 'converts the data to UTF-8' do
|
|
|
|
|
blob = described_class.new(double(binary?: false, data: "\n\xFF\xB9\xC3"))
|
|
|
|
|
|
|
|
|
|
expect(blob.data).to eq("\n<EFBFBD><EFBFBD><EFBFBD>")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-18 18:19:43 -05:00
|
|
|
|
describe '#svg?' do
|
|
|
|
|
it 'is falsey when not text' do
|
|
|
|
|
git_blob = double(text?: false)
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_svg
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'is falsey when no language is detected' do
|
|
|
|
|
git_blob = double(text?: true, language: nil)
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_svg
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it' is falsey when language is not SVG' do
|
|
|
|
|
git_blob = double(text?: true, language: double(name: 'XML'))
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_svg
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'is truthy when language is SVG' do
|
|
|
|
|
git_blob = double(text?: true, language: double(name: 'SVG'))
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).to be_svg
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-03 14:39:50 -04:00
|
|
|
|
describe '#pdf?' do
|
|
|
|
|
it 'is falsey when file extension is not .pdf' do
|
2017-04-10 17:23:19 -04:00
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: 'git_blob.txt')
|
2017-04-03 14:39:50 -04:00
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_pdf
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'is truthy when file extension is .pdf' do
|
2017-04-10 17:23:19 -04:00
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: 'git_blob.pdf')
|
2017-04-03 14:39:50 -04:00
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).to be_pdf
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-23 08:37:24 -04:00
|
|
|
|
describe '#ipython_notebook?' do
|
|
|
|
|
it 'is falsey when language is not Jupyter Notebook' do
|
|
|
|
|
git_blob = double(text?: true, language: double(name: 'JSON'))
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_ipython_notebook
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'is truthy when language is Jupyter Notebook' do
|
|
|
|
|
git_blob = double(text?: true, language: double(name: 'Jupyter Notebook'))
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).to be_ipython_notebook
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-29 01:16:59 -04:00
|
|
|
|
describe '#sketch?' do
|
|
|
|
|
it 'is falsey with image extension' do
|
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: "design.png")
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_sketch
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'is truthy with sketch extension' do
|
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: "design.sketch")
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).to be_sketch
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-07-25 11:08:36 -04:00
|
|
|
|
describe '#video?' do
|
|
|
|
|
it 'is falsey with image extension' do
|
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: 'image.png')
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_video
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
UploaderHelper::VIDEO_EXT.each do |ext|
|
|
|
|
|
it "is truthy when extension is .#{ext}" do
|
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: "video.#{ext}")
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).to be_video
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-06 06:02:24 -04:00
|
|
|
|
describe '#stl?' do
|
|
|
|
|
it 'is falsey with image extension' do
|
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: 'file.png')
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).not_to be_stl
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'is truthy with STL extension' do
|
|
|
|
|
git_blob = Gitlab::Git::Blob.new(name: 'file.stl')
|
|
|
|
|
|
|
|
|
|
expect(described_class.decorate(git_blob)).to be_stl
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-18 18:19:43 -05:00
|
|
|
|
describe '#to_partial_path' do
|
2017-03-07 12:57:30 -05:00
|
|
|
|
let(:project) { double(lfs_enabled?: true) }
|
|
|
|
|
|
2016-02-18 18:19:43 -05:00
|
|
|
|
def stubbed_blob(overrides = {})
|
|
|
|
|
overrides.reverse_merge!(
|
2017-04-03 14:39:50 -04:00
|
|
|
|
name: nil,
|
2016-02-18 18:19:43 -05:00
|
|
|
|
image?: false,
|
|
|
|
|
language: nil,
|
|
|
|
|
lfs_pointer?: false,
|
|
|
|
|
svg?: false,
|
2017-03-29 01:16:59 -04:00
|
|
|
|
text?: false,
|
2017-04-06 06:02:24 -04:00
|
|
|
|
binary?: false,
|
|
|
|
|
stl?: false
|
2016-02-18 18:19:43 -05:00
|
|
|
|
)
|
|
|
|
|
|
2017-04-10 17:23:19 -04:00
|
|
|
|
described_class.decorate(Gitlab::Git::Blob.new({})).tap do |blob|
|
2016-02-18 18:19:43 -05:00
|
|
|
|
allow(blob).to receive_messages(overrides)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-07 12:57:30 -05:00
|
|
|
|
it 'handles LFS pointers with LFS enabled' do
|
|
|
|
|
blob = stubbed_blob(lfs_pointer?: true, text?: true)
|
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'download'
|
|
|
|
|
end
|
2016-02-18 18:19:43 -05:00
|
|
|
|
|
2017-03-07 12:57:30 -05:00
|
|
|
|
it 'handles LFS pointers with LFS disabled' do
|
|
|
|
|
blob = stubbed_blob(lfs_pointer?: true, text?: true)
|
|
|
|
|
project = double(lfs_enabled?: false)
|
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'text'
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'handles SVGs' do
|
|
|
|
|
blob = stubbed_blob(text?: true, svg?: true)
|
2017-04-10 17:23:19 -04:00
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'svg'
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'handles images' do
|
|
|
|
|
blob = stubbed_blob(image?: true)
|
2017-03-07 12:57:30 -05:00
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'image'
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'handles text' do
|
2017-04-10 17:23:19 -04:00
|
|
|
|
blob = stubbed_blob(text?: true, name: 'test.txt')
|
2017-03-07 12:57:30 -05:00
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'text'
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'defaults to download' do
|
|
|
|
|
blob = stubbed_blob
|
2017-03-07 12:57:30 -05:00
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'download'
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|
2017-03-23 08:37:24 -04:00
|
|
|
|
|
2017-04-03 14:39:50 -04:00
|
|
|
|
it 'handles PDFs' do
|
|
|
|
|
blob = stubbed_blob(name: 'blob.pdf', pdf?: true)
|
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'pdf'
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-23 08:37:24 -04:00
|
|
|
|
it 'handles iPython notebooks' do
|
|
|
|
|
blob = stubbed_blob(text?: true, ipython_notebook?: true)
|
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'notebook'
|
|
|
|
|
end
|
2017-03-29 01:16:59 -04:00
|
|
|
|
|
|
|
|
|
it 'handles Sketch files' do
|
|
|
|
|
blob = stubbed_blob(text?: true, sketch?: true, binary?: true)
|
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'sketch'
|
|
|
|
|
end
|
2017-04-06 06:02:24 -04:00
|
|
|
|
|
|
|
|
|
it 'handles STLs' do
|
|
|
|
|
blob = stubbed_blob(text?: true, stl?: true)
|
|
|
|
|
expect(blob.to_partial_path(project)).to eq 'stl'
|
|
|
|
|
end
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|
2016-08-12 11:19:17 -04:00
|
|
|
|
|
|
|
|
|
describe '#size_within_svg_limits?' do
|
|
|
|
|
let(:blob) { described_class.decorate(double(:blob)) }
|
|
|
|
|
|
|
|
|
|
it 'returns true when the blob size is smaller than the SVG limit' do
|
|
|
|
|
expect(blob).to receive(:size).and_return(42)
|
|
|
|
|
|
|
|
|
|
expect(blob.size_within_svg_limits?).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true when the blob size is equal to the SVG limit' do
|
|
|
|
|
expect(blob).to receive(:size).and_return(Blob::MAXIMUM_SVG_SIZE)
|
|
|
|
|
|
|
|
|
|
expect(blob.size_within_svg_limits?).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns false when the blob size is larger than the SVG limit' do
|
|
|
|
|
expect(blob).to receive(:size).and_return(1.terabyte)
|
|
|
|
|
|
|
|
|
|
expect(blob.size_within_svg_limits?).to eq(false)
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-02-18 18:19:43 -05:00
|
|
|
|
end
|