summaryrefslogtreecommitdiff
path: root/spec/services/projects/lfs_pointers/lfs_download_service_spec.rb
blob: 6af5bfc7689b562b94ff8c1b58a82ea565cea992 (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
require 'spec_helper'

describe Projects::LfsPointers::LfsDownloadService do
  let(:project) { create(:project) }
  let(:oid) { '9e548e25631dd9ce6b43afd6359ab76da2819d6a5b474e66118c7819e1d8b3e8' }
  let(:download_link) { "http://gitlab.com/#{oid}" }
  let(:lfs_content) do
    <<~HEREDOC
    whatever
    HEREDOC
  end

  subject { described_class.new(project) }

  before do
    allow(project).to receive(:lfs_enabled?).and_return(true)
    WebMock.stub_request(:get, download_link).to_return(body: lfs_content)
  end

  describe '#execute' do
    context 'when file download succeeds' do
      it 'a new lfs object is created' do
        expect { subject.execute(oid, download_link) }.to change { LfsObject.count }.from(0).to(1)
      end

      it 'has the same oid' do
        subject.execute(oid, download_link)

        expect(LfsObject.first.oid).to eq oid
      end

      it 'stores the content' do
        subject.execute(oid, download_link)

        expect(File.read(LfsObject.first.file.file.file)).to eq lfs_content
      end
    end

    context 'when file download fails' do
      it 'no lfs object is created' do
        expect { subject.execute(oid, download_link) }.to change { LfsObject.count }
      end
    end

    context 'when credentials present' do
      let(:download_link_with_credentials) { "http://user:password@gitlab.com/#{oid}" }

      before do
        WebMock.stub_request(:get, download_link).with(headers: { 'Authorization' => 'Basic dXNlcjpwYXNzd29yZA==' }).to_return(body: lfs_content)
      end

      it 'the request adds authorization headers' do
        subject.execute(oid, download_link_with_credentials)
      end
    end

    context 'when an lfs object with the same oid already exists'  do
      before do
        create(:lfs_object, oid: 'oid')
      end

      it 'does not download the file' do
        expect(subject).not_to receive(:download_and_save_file)

        subject.execute('oid', download_link)
      end
    end
  end
end