upload_service_spec.rb 2.34 KB
Newer Older
1 2
require 'spec_helper'

3
describe UploadService do
4 5
  describe 'File service' do
    before do
6
      @user = create(:user)
7
      @project = create(:project, creator_id: @user.id, namespace: @user.namespace)
8 9 10 11
    end

    context 'for valid gif file' do
      before do
12
        gif = fixture_file_upload('spec/fixtures/banana_sample.gif', 'image/gif')
13
        @link_to_file = upload_file(@project, gif)
14 15
      end

Douwe Maan's avatar
Douwe Maan committed
16 17
      it { expect(@link_to_file).to have_key(:alt) }
      it { expect(@link_to_file).to have_key(:url) }
18
      it { expect(@link_to_file).to have_value('banana_sample') }
Douwe Maan's avatar
Douwe Maan committed
19
      it { expect(@link_to_file[:url]).to match('banana_sample.gif') }
20 21
    end

22
    context 'for valid png file' do
23
      before do
24
        png = fixture_file_upload('spec/fixtures/dk.png',
25
          'image/png')
26
        @link_to_file = upload_file(@project, png)
27 28
      end

Douwe Maan's avatar
Douwe Maan committed
29 30
      it { expect(@link_to_file).to have_key(:alt) }
      it { expect(@link_to_file).to have_key(:url) }
31
      it { expect(@link_to_file).to have_value('dk') }
Douwe Maan's avatar
Douwe Maan committed
32
      it { expect(@link_to_file[:url]).to match('dk.png') }
33 34
    end

35
    context 'for valid jpg file' do
36
      before do
37
        jpg = fixture_file_upload('spec/fixtures/rails_sample.jpg', 'image/jpg')
38
        @link_to_file = upload_file(@project, jpg)
39 40
      end

Douwe Maan's avatar
Douwe Maan committed
41 42
      it { expect(@link_to_file).to have_key(:alt) }
      it { expect(@link_to_file).to have_key(:url) }
43
      it { expect(@link_to_file).to have_value('rails_sample') }
Douwe Maan's avatar
Douwe Maan committed
44
      it { expect(@link_to_file[:url]).to match('rails_sample.jpg') }
45 46 47 48
    end

    context 'for txt file' do
      before do
49
        txt = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
50
        @link_to_file = upload_file(@project, txt)
51 52
      end

Douwe Maan's avatar
Douwe Maan committed
53 54
      it { expect(@link_to_file).to have_key(:alt) }
      it { expect(@link_to_file).to have_key(:url) }
55
      it { expect(@link_to_file).to have_value('doc_sample.txt') }
Douwe Maan's avatar
Douwe Maan committed
56
      it { expect(@link_to_file[:url]).to match('doc_sample.txt') }
57
    end
58 59 60

    context 'for too large a file' do
      before do
61
        txt = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
62
        allow(txt).to receive(:size) { 1000.megabytes.to_i }
63
        @link_to_file = upload_file(@project, txt)
64 65
      end

Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
66
      it { expect(@link_to_file).to eq({}) }
67
    end
68 69
  end

70
  def upload_file(project, file)
71
    described_class.new(project, file, FileUploader).execute.to_h
72 73
  end
end