1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe UploadService do
6  describe 'File service' do
7    before do
8      @user = create(:user)
9      @project = create(:project, creator_id: @user.id, namespace: @user.namespace)
10    end
11
12    context 'for valid gif file' do
13      before do
14        gif = fixture_file_upload('spec/fixtures/banana_sample.gif', 'image/gif')
15        @link_to_file = upload_file(@project, gif)
16      end
17
18      it { expect(@link_to_file).to have_key(:alt) }
19      it { expect(@link_to_file).to have_key(:url) }
20      it { expect(@link_to_file).to have_value('banana_sample') }
21      it { expect(@link_to_file[:url]).to match('banana_sample.gif') }
22    end
23
24    context 'for valid png file' do
25      before do
26        png = fixture_file_upload('spec/fixtures/dk.png',
27          'image/png')
28        @link_to_file = upload_file(@project, png)
29      end
30
31      it { expect(@link_to_file).to have_key(:alt) }
32      it { expect(@link_to_file).to have_key(:url) }
33      it { expect(@link_to_file).to have_value('dk') }
34      it { expect(@link_to_file[:url]).to match('dk.png') }
35    end
36
37    context 'for valid jpg file' do
38      before do
39        jpg = fixture_file_upload('spec/fixtures/rails_sample.jpg', 'image/jpg')
40        @link_to_file = upload_file(@project, jpg)
41      end
42
43      it { expect(@link_to_file).to have_key(:alt) }
44      it { expect(@link_to_file).to have_key(:url) }
45      it { expect(@link_to_file).to have_value('rails_sample') }
46      it { expect(@link_to_file[:url]).to match('rails_sample.jpg') }
47    end
48
49    context 'for txt file' do
50      before do
51        txt = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
52        @link_to_file = upload_file(@project, txt)
53      end
54
55      it { expect(@link_to_file).to have_key(:alt) }
56      it { expect(@link_to_file).to have_key(:url) }
57      it { expect(@link_to_file).to have_value('doc_sample.txt') }
58      it { expect(@link_to_file[:url]).to match('doc_sample.txt') }
59    end
60
61    context 'for too large a file' do
62      before do
63        txt = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
64        allow(txt).to receive(:size) { 1000.megabytes.to_i }
65        @link_to_file = upload_file(@project, txt)
66      end
67
68      it { expect(@link_to_file).to eq({}) }
69    end
70
71    describe '#override_max_attachment_size' do
72      let(:txt) { fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain') }
73      let(:service) { described_class.new(@project, txt, FileUploader) }
74
75      subject { service.execute.to_h }
76
77      before do
78        allow(txt).to receive(:size) { 100.megabytes.to_i }
79      end
80
81      it 'allows the upload' do
82        service.override_max_attachment_size = 101.megabytes
83
84        expect(subject.keys).to eq(%i(alt url markdown))
85      end
86
87      it 'disallows the upload' do
88        service.override_max_attachment_size = 99.megabytes
89
90        expect(subject).to eq({})
91      end
92    end
93  end
94
95  def upload_file(project, file)
96    described_class.new(project, file, FileUploader).execute.to_h
97  end
98end
99