debian-mirror-gitlab/spec/models/blob_viewer/base_spec.rb

152 lines
3.9 KiB
Ruby
Raw Normal View History

2019-05-18 00:54:41 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe BlobViewer::Base do
2017-08-17 22:00:37 +05:30
include FakeBlobHelpers
2017-09-10 17:25:29 +05:30
let(:project) { build(:project) }
2017-08-17 22:00:37 +05:30
let(:viewer_class) do
Class.new(described_class) do
2017-09-10 17:25:29 +05:30
include BlobViewer::ServerSide
2017-08-17 22:00:37 +05:30
self.extensions = %w(pdf)
2017-09-10 17:25:29 +05:30
self.binary = true
self.collapse_limit = 1.megabyte
self.size_limit = 5.megabytes
2017-08-17 22:00:37 +05:30
end
end
let(:viewer) { viewer_class.new(blob) }
describe '.can_render?' do
context 'when the extension is supported' do
2017-09-10 17:25:29 +05:30
context 'when the binaryness matches' do
let(:blob) { fake_blob(path: 'file.pdf', binary: true) }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'returns true' do
expect(viewer_class.can_render?(blob)).to be_truthy
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context 'when the binaryness does not match' do
let(:blob) { fake_blob(path: 'file.pdf', binary: false) }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'returns false' do
expect(viewer_class.can_render?(blob)).to be_falsey
end
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when the file type is supported' do
before do
viewer_class.file_types = %i(license)
viewer_class.binary = false
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'when the binaryness matches' do
let(:blob) { fake_blob(path: 'LICENSE', binary: false) }
it 'returns true' do
expect(viewer_class.can_render?(blob)).to be_truthy
end
end
context 'when the binaryness does not match' do
let(:blob) { fake_blob(path: 'LICENSE', binary: true) }
it 'returns false' do
expect(viewer_class.can_render?(blob)).to be_falsey
end
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when the extension and file type are not supported' do
let(:blob) { fake_blob(path: 'file.txt') }
2017-08-17 22:00:37 +05:30
it 'returns false' do
2017-09-10 17:25:29 +05:30
expect(viewer_class.can_render?(blob)).to be_falsey
2017-08-17 22:00:37 +05:30
end
end
end
2017-09-10 17:25:29 +05:30
describe '#collapsed?' do
context 'when the blob size is larger than the collapse limit' do
let(:blob) { fake_blob(path: 'file.pdf', size: 2.megabytes) }
2017-08-17 22:00:37 +05:30
it 'returns true' do
2017-09-10 17:25:29 +05:30
expect(viewer.collapsed?).to be_truthy
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when the blob size is smaller than the collapse limit' do
let(:blob) { fake_blob(path: 'file.pdf', size: 10.kilobytes) }
2017-08-17 22:00:37 +05:30
it 'returns false' do
2017-09-10 17:25:29 +05:30
expect(viewer.collapsed?).to be_falsey
2017-08-17 22:00:37 +05:30
end
end
end
2017-09-10 17:25:29 +05:30
describe '#too_large?' do
context 'when the blob size is larger than the size limit' do
let(:blob) { fake_blob(path: 'file.pdf', size: 10.megabytes) }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'returns true' do
expect(viewer.too_large?).to be_truthy
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when the blob size is smaller than the size limit' do
let(:blob) { fake_blob(path: 'file.pdf', size: 2.megabytes) }
2017-08-17 22:00:37 +05:30
it 'returns false' do
2017-09-10 17:25:29 +05:30
expect(viewer.too_large?).to be_falsey
2017-08-17 22:00:37 +05:30
end
end
end
describe '#render_error' do
2017-09-10 17:25:29 +05:30
context 'when the blob is expanded' do
2017-08-17 22:00:37 +05:30
before do
2017-09-10 17:25:29 +05:30
blob.expand!
2017-08-17 22:00:37 +05:30
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(path: 'file.pdf', size: 10.megabytes) }
it 'returns :too_large' do
expect(viewer.render_error).to eq(:too_large)
end
end
2017-09-10 17:25:29 +05:30
context 'when the blob size is smaller than the size limit' do
2017-08-17 22:00:37 +05:30
let(:blob) { fake_blob(path: 'file.pdf', size: 2.megabytes) }
it 'returns nil' do
expect(viewer.render_error).to be_nil
end
end
end
2017-09-10 17:25:29 +05:30
context 'when not expanded' do
context 'when the blob size is larger than the collapse limit' do
2017-08-17 22:00:37 +05:30
let(:blob) { fake_blob(path: 'file.pdf', size: 2.megabytes) }
2017-09-10 17:25:29 +05:30
it 'returns :collapsed' do
expect(viewer.render_error).to eq(:collapsed)
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when the blob size is smaller than the collapse limit' do
2017-08-17 22:00:37 +05:30
let(:blob) { fake_blob(path: 'file.pdf', size: 10.kilobytes) }
it 'returns nil' do
expect(viewer.render_error).to be_nil
end
end
end
end
end