summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/file_type_detection_spec.rb
blob: 5e9b8988cc839484a3ab0c54e769b52cd012a1d8 (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
# frozen_string_literal: true
require 'rails_helper'

describe Gitlab::FileTypeDetection do
  def upload_fixture(filename)
    fixture_file_upload(File.join('spec', 'fixtures', filename))
  end

  describe '#image_or_video?' do
    context 'when class is an uploader' do
      let(:uploader) do
        example_uploader = Class.new(CarrierWave::Uploader::Base) do
          include Gitlab::FileTypeDetection

          storage :file
        end

        example_uploader.new
      end

      it 'returns true for an image file' do
        uploader.store!(upload_fixture('dk.png'))

        expect(uploader).to be_image_or_video
      end

      it 'returns true for a video file' do
        uploader.store!(upload_fixture('video_sample.mp4'))

        expect(uploader).to be_image_or_video
      end

      it 'returns false for other extensions' do
        uploader.store!(upload_fixture('doc_sample.txt'))

        expect(uploader).not_to be_image_or_video
      end

      it 'returns false if filename is blank' do
        uploader.store!(upload_fixture('dk.png'))

        allow(uploader).to receive(:filename).and_return(nil)

        expect(uploader).not_to be_image_or_video
      end
    end

    context 'when class is a regular class' do
      let(:custom_class) do
        custom_class = Class.new do
          include Gitlab::FileTypeDetection
        end

        custom_class.new
      end

      it 'returns true for an image file' do
        allow(custom_class).to receive(:filename).and_return('dk.png')

        expect(custom_class).to be_image_or_video
      end

      it 'returns true for a video file' do
        allow(custom_class).to receive(:filename).and_return('video_sample.mp4')

        expect(custom_class).to be_image_or_video
      end

      it 'returns false for other extensions' do
        allow(custom_class).to receive(:filename).and_return('doc_sample.txt')

        expect(custom_class).not_to be_image_or_video
      end

      it 'returns false if filename is blank' do
        allow(custom_class).to receive(:filename).and_return(nil)

        expect(custom_class).not_to be_image_or_video
      end
    end
  end
end