-
Notifications
You must be signed in to change notification settings - Fork 26
/
gcs_test.rb
405 lines (318 loc) · 12.4 KB
/
gcs_test.rb
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
require_relative "test_helper"
require 'securerandom'
require "shrine/storage/linter"
require "date"
require "http"
describe Shrine::Storage::GoogleCloudStorage do
def gcs(options = {})
options[:bucket] ||= ENV.fetch("GCS_BUCKET")
Shrine::Storage::GoogleCloudStorage.new(**options)
end
def generate_test_filename
SecureRandom.hex
end
def service_account
{
private_key: "-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANW9uQf69ivd+txc
v5iMYkTVkGcQIereNz/lYeuZv2s7OZ4I9pdebleUmpxPn/CopRxS3O7JrXBPzAMK
i0tFs/dnn5Ny6AIzZvo1eMSptoKmcHswoYTP9ftTG7cDa8/12woEFu+fX9ob4isF
IaKvbD8kEhcyynWPUH3pP1g0ssUPAgMBAAECgYAtQhgM5Yn8resxf/4d2hPwyVvj
Rto3tkfyoqqCTbLnjMndeb5lPNyWdOPsFzwhpEQZ5D3d3hx4fJ0RQ8lM7fx2EiwD
+gjiOzLu9Fy+9XiVPbqIR20R63sHlA2jmzTuno9TLRdi+YyBS3XUVjckSE9mqTNO
RDmDgRbwQURjsgFH2QJBAP9oNQrNQI2Q+b7ufA8pnf2ZWBX0ASFz99Y1WVR+ip7o
3pByI7EMK3g9h3Ua3yEU+g5WVb/1Zaj6Bx7p4lRL540CQQDWPMC2yXO9wnsw1Nyy
1gtD18hcPBwDbIoQIK+J/AOLtSryKrCAEvOnLsxU1krYAj6ZP5MwrruLmnTLUXCE
+ZoLAkEA2kJuGZX/VTsQAbcBc1+oMOCLIu+Ky9CzeW3LseYVhekQ0TWJBLKWr0E9
cbiN91JawkfLLaiCwJ0x2pwaGtlmvQJAK8PFapHEvyMXn2Ycn7vyGS3flFgDMP/f
RGQo9/svjj64QzhNThyRAbohq8MLDw2GVDAUlYFcdqxa553/amrC+QJBAOYsfZTF
0ICBD2rt+ukhgSmZq/4oWxlM505kDh4z+x2oT3nFSi+fce5IWuNswR2qTRhjIAj8
wXh0ExlzwgD2xJ0=
-----END PRIVATE KEY-----",
client_email: 'test-shrine@test.google',
}
end
before do
@gcs = gcs
@shrine = Class.new(Shrine)
@shrine.storages = { gcs: @gcs }
@uploader = @shrine.new(:gcs)
end
after do
@gcs.clear!
end
it "passes the linter" do
Shrine::Storage::Linter.new(gcs).call(-> { image })
end
it "passes the linter with prefix" do
Shrine::Storage::Linter.new(gcs(prefix: 'pre')).call(-> { image })
end
describe "default_acl" do
it "does set default acl when uploading a new object" do
filename = generate_test_filename
gcs = gcs(default_acl: 'publicRead')
gcs.upload(image, filename)
response = HTTP.head(gcs.url(filename))
assert_equal 200, response.code
assert @gcs.exists?(filename)
end
it "does set default acl when copying an object" do
filename1 = generate_test_filename
filename2 = generate_test_filename
gcs = gcs(default_acl: 'publicRead')
object = @uploader.upload(image, location: filename1)
gcs.upload(object, filename2)
# filename1 needs to not be readable
response = HTTP.head(gcs.url(filename1))
assert_equal 403, response.code
# filename2 needs to be readable
response = HTTP.head(gcs.url(filename2))
assert_equal 200, response.code
assert @gcs.exists?(filename1)
end
it "allows for the acl to be set on an object level" do
filename = generate_test_filename
gcs = gcs(default_acl: 'private')
gcs.upload(image, filename, acl: 'publicRead')
response = HTTP.head(gcs.url(filename))
assert_equal 200, response.code
assert @gcs.exists?(filename)
end
end
describe "public" do
it "makes new objects public" do
filename = generate_test_filename
gcs = gcs(public: true)
gcs.upload(image, filename)
response = HTTP.head(gcs.url(filename))
assert_equal 200, response.code
assert @gcs.exists?(filename)
end
it "is not settable along with default_acl" do
assert_raises Shrine::Error do
gcs(public: true, default_acl: "test")
end
end
end
describe "object_options" do
it "does set the Cache-Control header when uploading a new object" do
filename = generate_test_filename
cache_control = 'public, max-age=7200'
gcs = gcs(default_acl: 'publicRead', object_options: { cache_control: cache_control })
gcs.upload(image, filename, content_type: 'image/jpeg')
assert @gcs.exists?(filename)
response = HTTP.head(gcs.url(filename))
assert_equal 200, response.code
assert_equal cache_control, response.headers["Cache-Control"]
assert_equal "image/jpeg", response.headers["Content-Type"]
end
it "does set the configured Cache-Control header when copying an object" do
filename1 = generate_test_filename
filename2 = generate_test_filename
cache_control = 'public, max-age=7200'
gcs = gcs(default_acl: 'publicRead', object_options: { cache_control: cache_control })
object = @uploader.upload(image, location: filename1)
gcs.upload(object, filename2)
# filename2 needs to have the correct Cache-Control header
response = HTTP.head(gcs.url(filename2))
assert_equal 200, response.code
assert_equal cache_control, response.headers["Cache-Control"]
end
end
describe "#clear!" do
it "does not empty the whole bucket when a prefix is set" do
filename = generate_test_filename
prefix = generate_test_filename
gcs_with_prefix = gcs(prefix: prefix)
@gcs.upload(image, filename)
@gcs.upload(image, prefix) # to ensure a file with the prefix name is not deleted
gcs_with_prefix.clear!
assert @gcs.exists?(filename)
assert @gcs.exists?(prefix)
end
it "removes only files for which block returns true" do
filename1 = generate_test_filename
filename2 = generate_test_filename
@gcs.upload(image, filename1)
@gcs.upload(image, filename2)
@gcs.clear! { |file| file.name == filename1 }
assert !@gcs.exists?(filename1)
assert @gcs.exists?(filename2)
end
end
describe "#presign" do
it "signs a PUT url with a signing key and issuer" do
filename = generate_test_filename
gcs = gcs()
gcs.upload(image, filename)
sa = service_account
Time.stub :now, Time.at(1486649900) do
presign = gcs.presign(
filename,
signing_key: sa[:private_key],
issuer: sa[:client_email],
)
assert_equal :put, presign[:method]
assert presign[:url].start_with? "https://storage.googleapis.com/#{gcs.bucket}/#{filename}?"
assert presign[:url].include? "Expires=1486650200"
assert presign[:url].include? "GoogleAccessId=test-shrine%40test.google"
assert presign[:url].include? "Signature=" # each tester's discovered signature will be different
assert_equal({}, presign[:headers])
end
end
it "generated a signed url for a non-existing object" do
gcs = gcs()
Time.stub :now, Time.at(1486649900) do
presign = gcs.presign('nonexisting')
assert_equal :put, presign[:method]
assert presign[:url].include? "https://storage.googleapis.com/#{gcs.bucket}/nonexisting?"
assert presign[:url].include? "Expires=1486650200"
assert presign[:url].include? "Signature=" # each tester's discovered signature will be different
assert_equal({}, presign[:headers])
end
end
it "signs a PUT url with discovered credentials" do
filename = generate_test_filename
gcs = gcs()
gcs.upload(image, filename)
Time.stub :now, Time.at(1486649900) do
presign = gcs.presign(filename)
assert_equal :put, presign[:method]
assert presign[:url].include? "https://storage.googleapis.com/#{gcs.bucket}/#{filename}?"
assert presign[:url].include? "Expires=1486650200"
assert presign[:url].include? "Signature=" # each tester's discovered signature will be different
assert_equal({}, presign[:headers])
end
end
it "adds headers for header parameters" do
filename = generate_test_filename
gcs = gcs()
content = "text"
md5 = Digest::MD5.base64digest(content)
presign = gcs.presign(filename,
content_type: 'text/plain',
content_md5: md5,
headers: {
'x-goog-acl' => 'private',
'x-goog-meta-foo' => 'bar,baz'
},
)
expected_headers = {
'Content-Type' => 'text/plain',
'Content-MD5' => md5,
'x-goog-acl' => 'private',
'x-goog-meta-foo' => 'bar,baz',
}
assert_equal expected_headers, presign[:headers]
# upload succeeds
response = HTTP.headers(presign[:headers]).put(presign[:url], body: content)
assert_equal 200, response.code
assert_equal content, HTTP.get(gcs.url(filename, expires: 60)).to_s
end
end
describe "#url" do
describe "signed" do
it "url with `expires` signs a GET url with discovered credentials" do
filename = generate_test_filename
gcs = gcs()
gcs.upload(image, filename)
Time.stub :now, Time.at(1486649900) do
presigned_url = gcs.url(filename, expires: 300)
assert presigned_url.include? "https://storage.googleapis.com/#{gcs.bucket}/#{filename}?"
assert presigned_url.include? "Expires=1486650200"
assert presigned_url.include? "Signature=" # each tester's discovered signature will be different
end
end
it "url with `expires` signs a GET url with discovered credentials and specified host" do
filename = generate_test_filename
host = "123.mycdn.net"
gcs = gcs(host: host)
gcs.upload(image, filename)
Time.stub :now, Time.at(1486649900) do
presigned_url = gcs.url(filename, expires: 300)
assert presigned_url.include? "https://#{host}/#{filename}?"
assert presigned_url.include? "Expires=1486650200"
assert presigned_url.include? "Signature=" # each tester's discovered signature will be different
end
end
end
it "provides a storage.googleapis.com url by default" do
filename = generate_test_filename
gcs = gcs()
url = gcs.url(filename)
assert_equal("https://storage.googleapis.com/#{gcs.bucket}/#{filename}", url)
end
it "encodes the filename in the URL" do
base_filename = generate_test_filename
filename = base_filename + "#test"
gcs = gcs()
url = gcs.url(filename)
assert_equal("https://storage.googleapis.com/#{gcs.bucket}/#{base_filename}%23test", url)
end
it "correctly generates URLs when there is a space in filename" do
base_filename = generate_test_filename
filename = base_filename + " test"
gcs = gcs()
url = gcs.url(filename)
assert_equal("https://storage.googleapis.com/#{gcs.bucket}/#{base_filename}%20test", url)
end
it "accepts :host for specifying CDN links" do
filename = generate_test_filename
host = "123.mycdn.net"
gcs = gcs(host: host)
url = gcs.url(filename)
assert_equal("https://123.mycdn.net/#{filename}", url)
end
end
describe '#upload' do
it 'uploads a io stream to google cloud storage' do
io = StringIO.new("data")
file_key = random_key
@gcs.upload(io, file_key)
assert @gcs.exists?(file_key)
end
it 'uploads Rails file objects' do
io = StringIO.new("data")
file = RailsFile.new(io)
file_key = random_key
@gcs.upload(file, file_key)
assert @gcs.exists?(file_key)
end
it 'copies an existing uploaded file' do
cache_control = 'public, max-age=7200'
gcs = gcs(default_acl: 'publicRead', object_options: { cache_control: cache_control })
gcs.upload(image, 'test.jpg', content_type: 'image/jpeg', cache_control: cache_control)
uploaded_file = @shrine.uploaded_file(id: 'test.jpg', storage: 'gcs')
gcs.upload(uploaded_file, 'test1.jpg')
assert gcs.exists?('test1.jpg')
response = HTTP.head(gcs.url('test1.jpg'))
assert_equal 200, response.code
# Let's ensure it correctly copies the metadata as well
assert_equal cache_control, response.headers["Cache-Control"]
assert_equal "image/jpeg", response.headers["Content-Type"]
end
end
describe "#open" do
it "returns an IO-like object around the file content" do
filename = generate_test_filename
gcs.upload(fakeio(filename), filename)
io = gcs.open(filename)
assert_equal(32, io.size)
assert_equal(filename, io.read)
assert_instance_of(Google::Cloud::Storage::File, io.data[:file])
end
it "accepts :rewindable" do
gcs.upload(fakeio, 'foo')
io = gcs.open('foo', rewindable: false)
assert_raises(IOError) { io.rewind }
end
it "accepts additional download options" do
gcs.upload(fakeio('file'), 'foo')
io = gcs.open('foo', range: 1..2)
assert_equal('il', io.read)
end
it "raises a Shrine::FileNotFound exception when a file does not exists" do
assert_raises(Shrine::FileNotFound) { gcs.open('foo')}
end
end
end