summaryrefslogtreecommitdiff
path: root/spec/models/blob_spec.rb
blob: cee20234e1f8fae94929c46f503880627921d638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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

  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

  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

  describe '#to_partial_path' do
    def stubbed_blob(overrides = {})
      overrides.reverse_merge!(
        image?: false,
        language: nil,
        lfs_pointer?: false,
        svg?: false,
        text?: false
      )

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

    it 'handles LFS pointers' do
      blob = stubbed_blob(lfs_pointer?: true)

      expect(blob.to_partial_path).to eq 'download'
    end

    it 'handles SVGs' do
      blob = stubbed_blob(text?: true, svg?: true)

      expect(blob.to_partial_path).to eq 'image'
    end

    it 'handles images' do
      blob = stubbed_blob(image?: true)

      expect(blob.to_partial_path).to eq 'image'
    end

    it 'handles text' do
      blob = stubbed_blob(text?: true)

      expect(blob.to_partial_path).to eq 'text'
    end

    it 'defaults to download' do
      blob = stubbed_blob

      expect(blob.to_partial_path).to eq 'download'
    end
  end

  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
end