blob_spec.rb 6.0 KB
Newer Older
1
# encoding: utf-8
2 3 4 5 6 7 8 9 10
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

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  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���")
      end
    end
  end

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  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

S
Sam Rose 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69
  describe '#pdf?' do
    it 'is falsey when file extension is not .pdf' do
      git_blob = double(name: 'git_blob.txt')

      expect(described_class.decorate(git_blob)).not_to be_pdf
    end

    it 'is truthy when file extension is .pdf' do
      git_blob = double(name: 'git_blob.pdf')

      expect(described_class.decorate(git_blob)).to be_pdf
    end
  end

P
Phil Hughes 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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

J
Jacob Schatz 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  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

P
Phil Hughes 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
  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

128
  describe '#to_partial_path' do
129 130
    let(:project) { double(lfs_enabled?: true) }

131 132
    def stubbed_blob(overrides = {})
      overrides.reverse_merge!(
S
Sam Rose 已提交
133
        name: nil,
134 135 136 137
        image?: false,
        language: nil,
        lfs_pointer?: false,
        svg?: false,
J
Jacob Schatz 已提交
138
        text?: false,
P
Phil Hughes 已提交
139 140
        binary?: false,
        stl?: false
141 142 143 144 145 146 147
      )

      described_class.decorate(double).tap do |blob|
        allow(blob).to receive_messages(overrides)
      end
    end

148 149 150 151
    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
152

153 154 155 156
    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'
157 158 159 160
    end

    it 'handles SVGs' do
      blob = stubbed_blob(text?: true, svg?: true)
161
      expect(blob.to_partial_path(project)).to eq 'image'
162 163 164 165
    end

    it 'handles images' do
      blob = stubbed_blob(image?: true)
166
      expect(blob.to_partial_path(project)).to eq 'image'
167 168 169 170
    end

    it 'handles text' do
      blob = stubbed_blob(text?: true)
171
      expect(blob.to_partial_path(project)).to eq 'text'
172 173 174 175
    end

    it 'defaults to download' do
      blob = stubbed_blob
176
      expect(blob.to_partial_path(project)).to eq 'download'
177
    end
P
Phil Hughes 已提交
178

S
Sam Rose 已提交
179 180 181 182 183
    it 'handles PDFs' do
      blob = stubbed_blob(name: 'blob.pdf', pdf?: true)
      expect(blob.to_partial_path(project)).to eq 'pdf'
    end

P
Phil Hughes 已提交
184 185 186 187
    it 'handles iPython notebooks' do
      blob = stubbed_blob(text?: true, ipython_notebook?: true)
      expect(blob.to_partial_path(project)).to eq 'notebook'
    end
J
Jacob Schatz 已提交
188 189 190 191 192

    it 'handles Sketch files' do
      blob = stubbed_blob(text?: true, sketch?: true, binary?: true)
      expect(blob.to_partial_path(project)).to eq 'sketch'
    end
P
Phil Hughes 已提交
193 194 195 196 197

    it 'handles STLs' do
      blob = stubbed_blob(text?: true, stl?: true)
      expect(blob.to_partial_path(project)).to eq 'stl'
    end
198
  end
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  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
221
end