summaryrefslogtreecommitdiff
path: root/spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb
blob: 0799a33f85660982c889b54c6dd5ce93ab71cdcb (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Projects::LfsPointers::LfsObjectDownloadListService do
  let(:import_url) { 'http://www.gitlab.com/demo/repo.git' }
  let(:default_endpoint) { "#{import_url}/info/lfs/objects/batch"}
  let(:group) { create(:group, lfs_enabled: true)}
  let!(:project) { create(:project, namespace: group, import_url: import_url, lfs_enabled: true) }
  let!(:lfs_objects_project) { create_list(:lfs_objects_project, 2, project: project) }
  let!(:existing_lfs_objects) { LfsObject.pluck(:oid, :size).to_h }
  let(:oids) { { 'oid1' => 123, 'oid2' => 125 } }
  let(:oid_download_links) { { 'oid1' => "#{import_url}/gitlab-lfs/objects/oid1", 'oid2' => "#{import_url}/gitlab-lfs/objects/oid2" } }
  let(:all_oids) { existing_lfs_objects.merge(oids) }
  let(:remote_uri) { URI.parse(lfs_endpoint) }

  subject { described_class.new(project) }

  before do
    allow(project.repository).to receive(:lfsconfig_for).and_return(nil)
    allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
    allow_any_instance_of(Projects::LfsPointers::LfsListService).to receive(:execute).and_return(all_oids)
  end

  describe '#execute' do
    context 'when no lfs pointer is linked' do
      before do
        allow_any_instance_of(Projects::LfsPointers::LfsDownloadLinkListService).to receive(:execute).and_return(oid_download_links)
        expect(Projects::LfsPointers::LfsDownloadLinkListService).to receive(:new).with(project, remote_uri: URI.parse(default_endpoint)).and_call_original
      end

      it 'retrieves all lfs pointers in the project repository' do
        expect_any_instance_of(Projects::LfsPointers::LfsListService).to receive(:execute)

        subject.execute
      end

      it 'retrieves the download links of non existent objects' do
        expect_any_instance_of(Projects::LfsPointers::LfsDownloadLinkListService).to receive(:execute).with(all_oids)

        subject.execute
      end
    end

    context 'when lfsconfig file exists' do
      before do
        allow(project.repository).to receive(:lfsconfig_for).and_return("[lfs]\n\turl = #{lfs_endpoint}\n")
      end

      context 'when url points to the same import url host' do
        let(:lfs_endpoint) { "#{import_url}/different_endpoint" }
        let(:service) { double }

        before do
          allow(service).to receive(:execute)
        end

        it 'downloads lfs object using the new endpoint' do
          expect(Projects::LfsPointers::LfsDownloadLinkListService).to receive(:new).with(project, remote_uri: remote_uri).and_return(service)

          subject.execute
        end

        context 'when import url has credentials' do
          let(:import_url) { 'http://user:password@www.gitlab.com/demo/repo.git'}

          it 'adds the credentials to the new endpoint' do
            expect(Projects::LfsPointers::LfsDownloadLinkListService)
              .to receive(:new).with(project, remote_uri: URI.parse("http://user:password@www.gitlab.com/demo/repo.git/different_endpoint"))
              .and_return(service)

            subject.execute
          end

          context 'when url has its own credentials' do
            let(:lfs_endpoint) { "http://user1:password1@www.gitlab.com/demo/repo.git/different_endpoint" }

            it 'does not add the import url credentials' do
              expect(Projects::LfsPointers::LfsDownloadLinkListService)
                .to receive(:new).with(project, remote_uri: remote_uri)
                .and_return(service)

              subject.execute
            end
          end
        end
      end

      context 'when url points to a third party service' do
        let(:lfs_endpoint) { 'http://third_party_service.com/info/lfs/objects/' }

        it 'disables lfs from the project' do
          expect(project.lfs_enabled?).to be_truthy

          subject.execute

          expect(project.lfs_enabled?).to be_falsey
        end

        it 'does not download anything' do
          expect_any_instance_of(Projects::LfsPointers::LfsListService).not_to receive(:execute)

          subject.execute
        end
      end
    end
  end

  describe '#default_endpoint_uri' do
    let(:import_url) { 'http://www.gitlab.com/demo/repo' }

    it 'adds suffix .git if the url does not have it' do
      expect(subject.send(:default_endpoint_uri).path).to match(/repo.git/)
    end
  end
end