-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuploads_spec.cr
More file actions
450 lines (371 loc) · 15.7 KB
/
uploads_spec.cr
File metadata and controls
450 lines (371 loc) · 15.7 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
require "../helper"
module PlaceOS::Api
describe Uploads do
::Spec.before_each do
Model::Storage.clear
end
it "should support pagination on list of uploads request" do
s = Model::Generator.storage.save!
Model::Generator.upload(file_name: "some_file", storage_id: s.id).save!
Model::Generator.upload(file_name: "some_file2", storage_id: s.id).save!
Model::Generator.upload(file_name: "my_file", storage_id: s.id).save!
Model::Generator.upload(file_name: "my_file2", storage_id: s.id).save!
params = HTTP::Params.encode({
"limit" => "1",
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "4"
result.headers["Content-Range"].should eq "items 0-0/4"
result.headers["Link"]?.should_not be_nil
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(1)
params = HTTP::Params.encode({
"file_search" => "my_file",
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "2"
result.headers["Content-Range"].should eq "items 0-1/2"
result.headers["Link"]?.should be_nil
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(2)
end
it "should support tag filtering on list of uploads" do
s = Model::Generator.storage.save!
Model::Generator.upload(file_name: "some_file", storage_id: s.id).save!
Model::Generator.upload(file_name: "some_file2", storage_id: s.id).save!
tagged = Model::Generator.upload(file_name: "my_file", storage_id: s.id)
tagged.tags = ["staff", "email1@domain.com"]
tagged.save!
tagged = Model::Generator.upload(file_name: "my_file2", storage_id: s.id)
tagged.tags = ["staff", "email2@domain.com"]
tagged.save!
params = HTTP::Params.encode({
"tags" => "staff",
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "2"
result.headers["Content-Range"].should eq "items 0-1/2"
result.headers["Link"]?.should be_nil
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(2)
params = HTTP::Params.encode({
"tags" => "staff,email1@domain.com",
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "1"
result.headers["Content-Range"].should eq "items 0-0/1"
result.headers["Link"]?.should be_nil
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(1)
params = HTTP::Params.encode({
"file_search" => "my_file",
"tags" => "staff",
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "2"
result.headers["Content-Range"].should eq "items 0-1/2"
result.headers["Link"]?.should be_nil
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(2)
end
it "new should return the Storage Provider" do
Model::Generator.storage.save!
params = HTTP::Params.encode({
"file_name" => "some_file_name.jpg",
"file_size" => "500",
"file_mime" => "image/jpeg",
})
resp = client.get("#{Uploads.base_route}/new?#{params}",
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
JSON.parse(resp.body).as_h["residence"].should eq("AmazonS3")
end
it "should handle storage allowed list on get call" do
s = Model::Generator.storage
s.ext_filter << "jpg"
s.save!
params = HTTP::Params.encode({
"file_name" => "some_file_name.png",
"file_size" => "500",
"file_mime" => "image/png",
})
resp = client.get("#{Uploads.base_route}/new?#{params}",
headers: Spec::Authentication.headers)
resp.status_code.should eq(400)
JSON.parse(resp.body).as_h["error"].as_s.should eq("File extension not allowed")
end
it "should handle storage allowed list on post call" do
s = Model::Generator.storage
s.ext_filter << ".png"
s.save!
params = {
"file_name" => "some_file_name.jpg",
"file_size" => "7000000",
"file_id" => "some_file_md5_hash",
"file_mime" => "image/jpeg",
}
resp = client.post(Uploads.base_route,
body: params.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(400)
JSON.parse(resp.body).as_h["error"].as_s.should eq("File extension not allowed")
end
it "post should return the pre-signed signature" do
Model::Generator.storage.save!
params = {
"file_name" => "some_file_name.jpg",
"file_size" => "500",
"file_id" => "some_file_md5_hash",
"file_mime" => "image/jpeg",
"cache_etag" => "12345",
}
resp = client.post(Uploads.base_route,
body: params.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("direct_upload")
sig = info["signature"].as_h
sig["verb"].as_s.should eq("PUT")
sig["url"].as_s.should_not be_nil
model = Model::Upload.find?(info["upload_id"].as_s)
model.should_not be_nil
raise "will never raise" unless model
model.cache_etag.should eq "12345"
end
it "post should return the pre-signed signature for multi-part" do
Model::Generator.storage.save!
params = {
"file_name" => "some_file_name.jpg",
"file_size" => "7000000",
"file_id" => "some_file_md5_hash",
"file_mime" => "image/jpeg",
}
resp = client.post(Uploads.base_route,
body: params.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("chunked_upload")
sig = info["signature"].as_h
sig["verb"].as_s.should eq("POST")
sig["url"].as_s.should_not be_nil
Model::Upload.find?(info["upload_id"].as_s).should_not be_nil
end
it "should handle upload visibility" do
Model::Generator.storage.save!
params = {
"file_name" => "some_file_name.jpg",
"file_size" => "500",
"file_id" => "some_file_md5_hash",
"file_mime" => "image/jpeg",
"public" => false,
"permissions" => "admin",
}
resp = client.post(Uploads.base_route,
body: params.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("direct_upload")
sig = info["signature"].as_h
sig["verb"].as_s.should eq("PUT")
sig["url"].as_s.should_not be_nil
upload = Model::Upload.find?(info["upload_id"].as_s)
upload.should_not be_nil
model = upload.not_nil!
model.public.should be_false
model.permissions.should eq Model::Upload::Permissions::Admin
resp = client.get("#{Uploads.base_route}/#{model.id}/url",
headers: Spec::Authentication.headers)
resp.status_code.should eq(303)
resp = client.get("#{Uploads.base_route}/#{model.id}/url",
headers: Spec::Authentication.headers(sys_admin: false))
resp.status_code.should eq(403)
end
it "should properly handle azure storage for direct uploads" do
storage = Model::Generator.storage(type: PlaceOS::Model::Storage::Type::Azure)
storage.access_key = "myteststorage"
storage.access_secret = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
storage.save!
params = {
"file_name" => "some_file_name.jpg",
"file_size" => "500",
"file_id" => "some_file_md5_hash",
"file_mime" => "image/jpeg",
"public" => false,
"permissions" => "admin",
}
resp = client.post(Uploads.base_route,
body: params.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("direct_upload")
sig = info["signature"].as_h
sig["verb"].as_s.should eq("PUT")
sig["url"].as_s.should_not be_nil
upload = Model::Upload.find?(info["upload_id"].as_s)
upload.should_not be_nil
end
it "should properly handle azure storage for chunked uploads" do
storage = Model::Generator.storage(type: PlaceOS::Model::Storage::Type::Azure)
storage.access_key = "myteststorage"
storage.access_secret = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
storage.save!
params = {
"file_name" => "some_file_name.jpg",
"file_size" => (258 * 1024 * 1024).to_s,
"file_id" => "some_file_md5_hash",
"file_mime" => "image/jpeg",
"public" => false,
"permissions" => "admin",
}
resp = client.post(Uploads.base_route,
body: params.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("chunked_upload")
info["residence"].should eq("AzureStorage")
sig = info["signature"].as_h
sig["verb"].as_s.should eq("PUT")
sig["url"].as_s.size.should eq(0)
upload = Model::Upload.find!(info["upload_id"].as_s)
part_number = "1"
params = {
"part" => part_number,
"file_id" => "some_file_md5_hash",
}
pinfo = Uploads::PartInfo.new(md5: params["file_id"], part: 1)
uinfo = Uploads::UpdateInfo.new(resumable_id: "some-random-resumable-id", part_data: [pinfo], part_list: [1])
resp = client.patch(
path: "#{Uploads.base_route}/#{upload.id}?#{HTTP::Params.encode(params)}",
body: uinfo.to_json,
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("part_upload")
sig = info["signature"].as_h
sig["verb"].as_s.should eq("PUT")
sig["url"].as_s.size.should be > 0
uri = URI.parse(sig["url"].as_s)
uri.host.should eq(sprintf("%s.blob.core.windows.net", "myteststorage"))
qparams = URI::Params.parse(uri.query || "")
# Azure encodes the part as Base64(part.rjust(6, '0'))
expected_block_id = Base64.strict_encode(part_number.rjust(6, '0'))
qparams["blockid"].should eq(expected_block_id)
params = {
"part" => "finish",
}
resp = client.get(
path: "#{Uploads.base_route}/#{upload.id}/edit?#{HTTP::Params.encode(params)}",
headers: Spec::Authentication.headers)
resp.status_code.should eq(200)
info = JSON.parse(resp.body).as_h
info["type"].should eq("finish")
info["body"].should_not be_nil
end
it "should list uploads from a specific storage when storage_id is provided" do
authority = Model::Authority.find_by_domain("localhost").not_nil!
# Create two storages for the same authority
storage1 = Model::Generator.storage(authority_id: authority.id)
storage1.is_default = true
storage1.save!
storage2 = Model::Generator.storage(authority_id: authority.id)
storage2.is_default = false
storage2.save!
# Create uploads in both storages
Model::Generator.upload(file_name: "file_in_storage1", storage_id: storage1.id).save!
Model::Generator.upload(file_name: "file_in_storage2_a", storage_id: storage2.id).save!
Model::Generator.upload(file_name: "file_in_storage2_b", storage_id: storage2.id).save!
# List uploads from storage2
params = HTTP::Params.encode({
"storage_id" => storage2.id.as(String),
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "2"
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(2)
uploads.all? { |u| u.storage_id == storage2.id }.should be_true
end
it "should list uploads from default storage when storage_id is not provided" do
authority = Model::Authority.find_by_domain("localhost").not_nil!
# Create two storages
storage1 = Model::Generator.storage(authority_id: authority.id)
storage1.is_default = true
storage1.save!
storage2 = Model::Generator.storage(authority_id: authority.id)
storage2.is_default = false
storage2.save!
# Create uploads in both storages
Model::Generator.upload(file_name: "default_file", storage_id: storage1.id).save!
Model::Generator.upload(file_name: "other_file", storage_id: storage2.id).save!
# List uploads without storage_id (should use default)
result = client.get(Uploads.base_route,
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "1"
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(1)
uploads.first.storage_id.should eq(storage1.id)
uploads.first.file_name.should eq("default_file")
end
it "should return 404 when storage_id does not exist" do
params = HTTP::Params.encode({
"storage_id" => "storage-nonexistent",
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.status_code.should eq(404)
JSON.parse(result.body).as_h["error"].as_s.should contain("Storage not found")
end
it "should return 403 when storage_id belongs to different authority" do
authority = Model::Authority.find_by_domain("localhost").not_nil!
# Create storage for a different authority
other_authority = Model::Generator.authority
other_authority.domain = "other.example.com"
other_authority.save!
other_storage = Model::Generator.storage(authority_id: other_authority.id)
other_storage.save!
# Try to list uploads from other authority's storage
params = HTTP::Params.encode({
"storage_id" => other_storage.id.as(String),
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.status_code.should eq(403)
end
it "should allow access to global storage (authority_id is nil)" do
authority = Model::Authority.find_by_domain("localhost").not_nil!
# Create a global storage (no authority_id)
global_storage = Model::Generator.storage(authority_id: nil)
global_storage.save!
# Create upload in global storage
Model::Generator.upload(file_name: "global_file", storage_id: global_storage.id).save!
# List uploads from global storage
params = HTTP::Params.encode({
"storage_id" => global_storage.id.as(String),
})
result = client.get("#{Uploads.base_route}/?#{params}",
headers: Spec::Authentication.headers)
result.success?.should be_true
result.headers["X-Total-Count"].should eq "1"
uploads = Array(Model::Upload).from_json(result.body)
uploads.size.should eq(1)
uploads.first.file_name.should eq("global_file")
end
end
end