BigW Consortium Gitlab

blob_spec.rb 2.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
require 'spec_helper'

describe ContainerRegistry::Blob do
  let(:digest) { 'sha256:0123456789012345' }
  let(:config) do
    {
      'digest' => digest,
      'mediaType' => 'binary',
      'size' => 1000
    }
  end
12
  let(:token) { 'authorization-token' }
13
  
14
  let(:registry) { ContainerRegistry::Registry.new('http://example.com', token: token) }
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
  let(:repository) { registry.repository('group/test') }
  let(:blob) { repository.blob(config) }

  it { expect(blob).to respond_to(:repository) }
  it { expect(blob).to delegate_method(:registry).to(:repository) }
  it { expect(blob).to delegate_method(:client).to(:repository) }

  context '#path' do
    subject { blob.path }

    it { is_expected.to eq('example.com/group/test@sha256:0123456789012345') }
  end

  context '#digest' do
    subject { blob.digest }

    it { is_expected.to eq(digest) }
  end

  context '#type' do
    subject { blob.type }

    it { is_expected.to eq('binary') }
  end

  context '#revision' do
    subject { blob.revision }

    it { is_expected.to eq('0123456789012345') }
  end

  context '#short_revision' do
    subject { blob.short_revision }

    it { is_expected.to eq('012345678') }
  end

  context '#delete' do
    before do
      stub_request(:delete, 'http://example.com/v2/group/test/blobs/sha256:0123456789012345').
        to_return(status: 200)
    end

    subject { blob.delete }

    it { is_expected.to be_truthy }
  end
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

  context '#data' do
    let(:data) { '{"key":"value"}' }

    subject { blob.data }

    context 'when locally stored' do
      before do
        stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:0123456789012345').
          to_return(
            status: 200,
            headers: { 'Content-Type' => 'application/json' },
            body: data)
      end

      it { is_expected.to eq(data) }
    end

    context 'when externally stored' do
      before do
        stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:0123456789012345').
          with(headers: { 'Authorization' => "bearer #{token}" }).
          to_return(
            status: 307,
            headers: { 'Location' => location })
      end

      context 'for a valid address' do
        let(:location) { 'http://external.com/blob/file' }

        before do
          stub_request(:get, location).
            with(headers: { 'Authorization' => nil }).
            to_return(
              status: 200,
              headers: { 'Content-Type' => 'application/json' },
              body: data)
        end

        it { is_expected.to eq(data) }
      end

      context 'for invalid file' do
        let(:location) { 'file:///etc/passwd' }

        it { expect{ subject }.to raise_error(ArgumentError, 'invalid address') }
      end
    end
  end
111
end