summaryrefslogtreecommitdiff
path: root/spec/services/dependency_proxy/find_or_create_blob_service_spec.rb
blob: 5f7afdf699a388f937cd772f0e1bf6e4e0d7d168 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe DependencyProxy::FindOrCreateBlobService do
  include DependencyProxyHelpers

  let_it_be_with_reload(:blob) { create(:dependency_proxy_blob) }

  let(:group) { blob.group }
  let(:image) { 'alpine' }
  let(:tag)   { '3.9' }
  let(:token) { Digest::SHA256.hexdigest('123') }
  let(:blob_sha) { '40bd001563085fc35165329ea1ff5c5ecbdbbeef' }

  subject { described_class.new(group, image, token, blob_sha).execute }

  before do
    stub_registry_auth(image, token)
  end

  shared_examples 'downloads the remote blob' do
    it 'downloads blob from remote registry if there is no cached one' do
      expect(subject[:status]).to eq(:success)
      expect(subject[:blob]).to be_a(DependencyProxy::Blob)
      expect(subject[:blob]).to be_persisted
      expect(subject[:from_cache]).to eq false
    end
  end

  context 'no cache' do
    before do
      stub_blob_download(image, blob_sha)
    end

    it_behaves_like 'downloads the remote blob'
  end

  context 'cached blob' do
    let(:blob_sha) { blob.file_name.sub('.gz', '') }

    it 'uses cached blob instead of downloading one' do
      expect { subject }.to change { blob.reload.read_at }

      expect(subject[:status]).to eq(:success)
      expect(subject[:blob]).to be_a(DependencyProxy::Blob)
      expect(subject[:blob]).to eq(blob)
      expect(subject[:from_cache]).to eq true
    end

    context 'when the cached blob is expired' do
      before do
        blob.update_column(:status, DependencyProxy::Blob.statuses[:expired])
        stub_blob_download(image, blob_sha)
      end

      it_behaves_like 'downloads the remote blob'
    end
  end

  context 'no such blob exists remotely' do
    before do
      stub_blob_download(image, blob_sha, 404)
    end

    it 'returns error message and http status' do
      expect(subject[:status]).to eq(:error)
      expect(subject[:message]).to eq('Failed to download the blob')
      expect(subject[:http_status]).to eq(404)
    end
  end
end