summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/lfs/client_spec.rb
blob: 0f9637e8ca41e8640abe617226064c064d6b947c (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Lfs::Client do
  let(:base_url) { "https://example.com" }
  let(:username) { 'user' }
  let(:password) { 'password' }
  let(:credentials) { { user: username, password: password } }
  let(:git_lfs_content_type) { 'application/vnd.git-lfs+json' }
  let(:git_lfs_user_agent) { "GitLab #{Gitlab::VERSION} LFS client" }

  let(:basic_auth_headers) do
    { 'Authorization' => "Basic #{Base64.strict_encode64("#{username}:#{password}")}" }
  end

  let(:upload_action) do
    {
      "href" => "#{base_url}/some/file",
      "header" => {
        "Key" => "value"
      }
    }
  end

  let(:verify_action) do
    {
      "href" => "#{base_url}/some/file/verify",
      "header" => {
        "Key" => "value"
      }
    }
  end

  let(:authorized_upload_action) { upload_action.tap { |action| action['header']['Authorization'] = 'foo' } }
  let(:authorized_verify_action) { verify_action.tap { |action| action['header']['Authorization'] = 'foo' } }

  subject(:lfs_client) { described_class.new(base_url, credentials: credentials) }

  describe '#batch' do
    let_it_be(:objects) { create_list(:lfs_object, 3) }

    context 'server returns 200 OK' do
      it 'makes a successful batch request' do
        stub = stub_batch(
          objects: objects,
          headers: basic_auth_headers
        ).to_return(
          status: 200,
          body: { 'objects' => 'anything', 'transfer' => 'basic' }.to_json,
          headers: { 'Content-Type' => git_lfs_content_type }
        )

        result = lfs_client.batch!('upload', objects)

        expect(stub).to have_been_requested
        expect(result).to eq('objects' => 'anything', 'transfer' => 'basic')
      end
    end

    context 'server returns 400 error' do
      it 'raises an error' do
        stub_batch(objects: objects, headers: basic_auth_headers).to_return(status: 400)

        expect { lfs_client.batch!('upload', objects) }.to raise_error(/Failed/)
      end
    end

    context 'server returns 500 error' do
      it 'raises an error' do
        stub_batch(objects: objects, headers: basic_auth_headers).to_return(status: 400)

        expect { lfs_client.batch!('upload', objects) }.to raise_error(/Failed/)
      end
    end

    context 'server returns an exotic transfer method' do
      it 'raises an error' do
        stub_batch(
          objects: objects,
          headers: basic_auth_headers
        ).to_return(
          status: 200,
          body: { 'transfer' => 'carrier-pigeon' }.to_json,
          headers: { 'Content-Type' => git_lfs_content_type }
        )

        expect { lfs_client.batch!('upload', objects) }.to raise_error(/Unsupported transfer/)
      end
    end

    def stub_batch(objects:, headers:, operation: 'upload', transfer: 'basic')
      objects = objects.as_json(only: [:oid, :size])
      body = { operation: operation, 'transfers': [transfer], objects: objects }.to_json

      headers = {
        'Accept' => git_lfs_content_type,
        'Content-Type' => git_lfs_content_type,
        'User-Agent' => git_lfs_user_agent
      }.merge(headers)

      stub_request(:post, base_url + '/info/lfs/objects/batch').with(body: body, headers: headers)
    end
  end

  describe "#upload" do
    let_it_be(:object) { create(:lfs_object) }

    context 'server returns 200 OK to an authenticated request' do
      it "makes an HTTP PUT with expected parameters" do
        stub_upload(object: object, headers: upload_action['header']).to_return(status: 200)

        lfs_client.upload!(object, upload_action, authenticated: true)
      end
    end

    context 'server returns 200 OK to an unauthenticated request' do
      it "makes an HTTP PUT with expected parameters" do
        stub = stub_upload(
          object: object,
          headers: basic_auth_headers.merge(upload_action['header'])
        ).to_return(status: 200)

        lfs_client.upload!(object, upload_action, authenticated: false)

        expect(stub).to have_been_requested
      end
    end

    context 'request is not marked as authenticated but includes an authorization header' do
      it 'prefers the provided authorization header' do
        stub = stub_upload(
          object: object,
          headers: authorized_upload_action['header']
        ).to_return(status: 200)

        lfs_client.upload!(object, authorized_upload_action, authenticated: false)

        expect(stub).to have_been_requested
      end
    end

    context 'LFS object has no file' do
      let(:object) { LfsObject.new }

      it 'makes an HTTP PUT with expected parameters' do
        stub = stub_upload(
          object: object,
          headers: upload_action['header']
        ).to_return(status: 200)

        lfs_client.upload!(object, upload_action, authenticated: true)

        expect(stub).to have_been_requested
      end
    end

    context 'server returns 400 error' do
      it 'raises an error' do
        stub_upload(object: object, headers: upload_action['header']).to_return(status: 400)

        expect { lfs_client.upload!(object, upload_action, authenticated: true) }.to raise_error(/Failed/)
      end
    end

    context 'server returns 500 error' do
      it 'raises an error' do
        stub_upload(object: object, headers: upload_action['header']).to_return(status: 500)

        expect { lfs_client.upload!(object, upload_action, authenticated: true) }.to raise_error(/Failed/)
      end
    end

    def stub_upload(object:, headers:)
      headers = {
        'Content-Type' => 'application/octet-stream',
        'Content-Length' => object.size.to_s,
        'User-Agent' => git_lfs_user_agent
      }.merge(headers)

      stub_request(:put, upload_action['href']).with(
        body: object.file.read,
        headers: headers.merge('Content-Length' => object.size.to_s)
      )
    end
  end

  describe "#verify" do
    let_it_be(:object) { create(:lfs_object) }

    context 'server returns 200 OK to an authenticated request' do
      it "makes an HTTP POST with expected parameters" do
        stub_verify(object: object, headers: verify_action['header']).to_return(status: 200)

        lfs_client.verify!(object, verify_action, authenticated: true)
      end
    end

    context 'server returns 200 OK to an unauthenticated request' do
      it "makes an HTTP POST with expected parameters" do
        stub = stub_verify(
          object: object,
          headers: basic_auth_headers.merge(upload_action['header'])
        ).to_return(status: 200)

        lfs_client.verify!(object, verify_action, authenticated: false)

        expect(stub).to have_been_requested
      end
    end

    context 'request is not marked as authenticated but includes an authorization header' do
      it 'prefers the provided authorization header' do
        stub = stub_verify(
          object: object,
          headers: authorized_verify_action['header']
        ).to_return(status: 200)

        lfs_client.verify!(object, authorized_verify_action, authenticated: false)

        expect(stub).to have_been_requested
      end
    end

    context 'server returns 400 error' do
      it 'raises an error' do
        stub_verify(object: object, headers: verify_action['header']).to_return(status: 400)

        expect { lfs_client.verify!(object, verify_action, authenticated: true) }.to raise_error(/Failed/)
      end
    end

    context 'server returns 500 error' do
      it 'raises an error' do
        stub_verify(object: object, headers: verify_action['header']).to_return(status: 500)

        expect { lfs_client.verify!(object, verify_action, authenticated: true) }.to raise_error(/Failed/)
      end
    end

    def stub_verify(object:, headers:)
      headers = {
        'Accept' => git_lfs_content_type,
        'Content-Type' => git_lfs_content_type,
        'User-Agent' => git_lfs_user_agent
      }.merge(headers)

      stub_request(:post, verify_action['href']).with(
        body: object.to_json(only: [:oid, :size]),
        headers: headers
      )
    end
  end
end