-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_spec.rb
724 lines (603 loc) · 22.5 KB
/
api_spec.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
require_relative "spec_helper"
def check_dir_listing_content_type(content_type)
content_type.must_match(%r{application\/(ld\+)?json})
if content_type != "application/ld+json"
puts "WARNING: the content type \"#{content_type}\" works for directory listings, but the correct one to use is \"application/ld+json\"".yellow
end
end
describe "OPTIONS" do
describe "GET" do
it "returns a valid response" do
res = do_options_request CONFIG[:category]+"/foo", {
access_control_request_method: 'GET',
origin: 'https://unhosted.org',
referer: 'https://unhosted.org'
}
[200, 204].must_include res.code
res.headers[:access_control_allow_origin].must_match(/(\*|https:\/\/unhosted\.org)/)
res.headers[:access_control_expose_headers].must_include 'ETag'
res.headers[:access_control_allow_methods].must_include 'GET'
res.body.must_be_empty
['Authorization', 'Content-Type', 'Origin', 'If-Match', 'If-None-Match'].each do |header|
res.headers[:access_control_allow_headers].must_include header
end
end
end
describe "PUT and DELETE" do
it "returns a valid response" do
["PUT", "DELETE"].each do |method|
res = do_options_request CONFIG[:category]+"/foo", {
access_control_request_method: method,
origin: 'https://unhosted.org',
referer: 'https://unhosted.org'
}
[200, 204].must_include res.code
res.headers[:access_control_allow_origin].must_equal "https://unhosted.org"
res.headers[:access_control_expose_headers].must_include 'ETag'
res.headers[:access_control_allow_methods].must_include method
res.body.must_be_empty
['Authorization', 'Content-Type', 'Origin', 'If-Match', 'If-None-Match'].each do |header|
res.headers[:access_control_allow_headers].must_include header
end
end
end
end
end
describe "Requests" do
describe "PUT a JSON object" do
before do
@res = do_put_request("#{CONFIG[:category]}/test-object-simple.json",
'{"new": "object", "should_be": "large_enough", "to_trigger": "compression", "if_enabled": "on_server"}',
{ content_type: "application/json" })
end
it "works" do
[200, 201].must_include @res.code
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
end
end
describe "PUT with nested folder" do
before do
@res = do_put_request("#{CONFIG[:category]}/some-subdir/nested-folder-object.json",
'{"foo": "baz"}',
{ content_type: "application/json" })
end
it "works" do
[200, 201].must_include @res.code
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
end
end
describe "updating that JSON object" do
before do
@old_outer_listing_res = do_get_request("#{CONFIG[:category]}/")
@old_listing_res = do_get_request("#{CONFIG[:category]}/some-subdir/")
old_listing = JSON.parse @old_listing_res.body
@old_item_info = old_listing["items"]["nested-folder-object.json"]
@res = do_put_request("#{CONFIG[:category]}/some-subdir/nested-folder-object.json",
'{"foo": "bam"}',
{ content_type: "application/json" })
@item_etag = @res.headers[:etag]
@outer_listing_res = do_get_request("#{CONFIG[:category]}/")
@listing_res = do_get_request("#{CONFIG[:category]}/some-subdir/")
listing = JSON.parse @listing_res.body
@item_info = listing["items"]["nested-folder-object.json"]
end
it "works" do
# before runs for each block once, so we have to put all blocks into one
[200, 201].must_include @res.code
@item_etag.wont_be_nil
@item_etag.must_be_etag
# updates the file etags in the folder listing
@item_info["ETag"].must_equal @item_etag.delete('"')
@item_info["ETag"].wont_equal @old_item_info["ETag"]
# updates the folder etag
@listing_res.headers[:etag].wont_equal @old_listing_res.headers[:etag]
@outer_listing_res.headers[:etag].wont_equal @old_outer_listing_res.headers[:etag]
end
end
describe "PUT with same name as existing directory" do
it "returns a 409" do
do_put_request("#{CONFIG[:category]}/some-subdir", '', {content_type: "text/plain"}) do |res|
res.code.must_equal 409
end
end
end
describe "PUT with Content-Range" do
it "returns a 400" do
# https://tools.ietf.org/html/rfc7231#section-4.3.4
do_put_request("#{CONFIG[:category]}/some-subdir/nested-folder-object.json",
'sup', {content_range: "bytes 0-3/3", content_type: "text/plain"}) do |res|
res.code.must_equal 400
end
end
end
describe "PUT with same directory name as existing object" do
before do
do_put_request("#{CONFIG[:category]}/my-list", '', {content_type: "text/plain"})
end
it "returns a 409" do
do_put_request("#{CONFIG[:category]}/my-list/item", '', {content_type: "text/plain"}) do |res|
res.code.must_equal 409
end
end
end
describe "PUT with matching If-Match header" do
before do
@etag = do_head_request("#{CONFIG[:category]}/test-object-simple.json").headers[:etag]
do_put_request("#{CONFIG[:category]}/test-object-simple.json",
'{"new": "object", "should_be": "large_enough", "to_trigger": "compression", "if_enabled": "on_server"}',
{ content_type: "application/json", if_match: @etag }) do |response|
@res = response
end
end
it "updates the object" do
[200, 201].must_include @res.code
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
end
end
describe "PUT with non-matching If-Match header" do
before do
do_put_request("#{CONFIG[:category]}/test-object-simple.json",
'{"should": "not-happen"}',
{ content_type: "application/json", if_match: %Q("invalid") }) do |response|
@res = response
end
end
it "returns 412" do
@res.code.must_equal 412
end
end
describe "PUT with If-Match header to non-existing object" do
before do
do_put_request("#{CONFIG[:category]}/four-oh-four.json",
'{"should": "not-happen"}',
{ content_type: "application/json",
if_match: %Q("doesnotmatter") }) do |response|
@res = response
end
end
it "returns 412" do
@res.code.must_equal 412
end
end
describe "PUT with If-None-Match header to existing object" do
before do
do_put_request("#{CONFIG[:category]}/test-object-simple.json",
'{"should": "not-happen"}',
{ content_type: "application/json",
if_none_match: "*" }) do |response|
@res = response
end
end
it "returns 412" do
@res.code.must_equal 412
end
end
describe "PUT with If-None-Match header to non-existing object" do
before do
do_put_request("#{CONFIG[:category]}/test-object-simple2.json",
'{"new": "object", "should_be": "large_enough", "to_trigger": "compression", "if_enabled": "on_server"}',
{ content_type: "application/json",
if_none_match: "*" }) do |response|
@res = response
end
end
it "works" do
[200, 201].must_include @res.code
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
end
end
describe "GET a JSON object" do
before do
@res = do_get_request("#{CONFIG[:category]}/test-object-simple.json")
end
it "works" do
@res.code.must_equal 200
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
@res.headers[:content_type].must_equal "application/json"
@res.headers[:content_length].must_equal "102"
@res.headers[:cache_control].must_equal "no-cache"
@res.body.must_equal '{"new": "object", "should_be": "large_enough", "to_trigger": "compression", "if_enabled": "on_server"}'
end
end
describe "GET a JSON object while accepting compressed content" do
before do
@res = do_get_request("#{CONFIG[:category]}/test-object-simple.json",
{ accept_encoding: 'gzip, deflate, br' })
end
it "works" do
@res.code.must_equal 200
@res.headers[:content_encoding].must_be_nil
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
@res.headers[:content_type].must_equal "application/json"
@res.headers[:content_length].must_equal "102"
@res.headers[:cache_control].must_equal "no-cache"
@res.body.must_equal '{"new": "object", "should_be": "large_enough", "to_trigger": "compression", "if_enabled": "on_server"}'
end
end
describe "GET with If-None-Match header" do
before do
@etag = do_head_request("#{CONFIG[:category]}/test-object-simple.json").headers[:etag]
do_get_request("#{CONFIG[:category]}/test-object-simple.json", { if_none_match: @etag }) do |response|
@res = response
end
end
it "returns 304 with empty body when ETag matches" do
@res.code.must_equal 304
@res.body.must_be_empty
end
end
describe "GET with multiple ETags in If-None-Match header" do
before do
@etag = do_head_request("#{CONFIG[:category]}/test-object-simple.json").headers[:etag]
do_get_request("#{CONFIG[:category]}/test-object-simple.json",
{ if_none_match: %Q("r2d2c3po", #{@etag}) }) do |response|
@res = response
end
end
it "returns 304 when one ETag matches" do
@res.code.must_equal 304
@res.body.must_be_empty
end
end
describe "HEAD a JSON object" do
before do
@res = do_head_request("#{CONFIG[:category]}/test-object-simple.json")
end
it "works" do
[200, 204].must_include @res.code
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
@res.headers[:content_type].must_equal "application/json"
# Content-Length must match the correct length if present but it's optional
@res.headers[:content_length].must_equal "102" if @res.headers[:content_length]
@res.body.must_be_empty
end
end
describe "PUT a JPG image" do
before do
@res = do_put_request("#{CONFIG[:category]}/Capture d'écran.jpg",
File.open("fixtures/files/capture.jpg"),
{ content_type: "image/jpeg; charset=binary" })
end
it "works" do
[200, 201].must_include @res.code
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
end
end
describe "GET a JPG image" do
before do
@res = do_network_request("#{CONFIG[:category]}/Capture d'écran.jpg", method: :get, raw_response: true)
end
it "works" do
@res.code.must_equal 200
@res.headers[:etag].wont_be_nil
@res.headers[:etag].must_be_etag
@res.headers[:content_type].must_equal "image/jpeg; charset=binary"
@res.headers[:content_length].must_equal "28990"
@res.to_s.must_equal File.read("fixtures/files/capture.jpg")
end
end
describe "GET a non-existing object" do
it "returns a 404" do
do_get_request("#{CONFIG[:category]}/four-oh-four.html") do |response|
response.code.must_equal 404
end
end
end
describe "HEAD directory listing" do
before do
@res = do_head_request("#{CONFIG[:category]}/")
end
it "works" do
[200, 204].must_include @res.code
@res.headers[:etag].must_be_etag
check_dir_listing_content_type(@res.headers[:content_type])
@res.body.must_equal ""
end
end
describe "GET directory listing" do
before do
@res = do_get_request("#{CONFIG[:category]}/")
@listing = JSON.parse @res.body
end
it "works" do
@res.code.must_equal 200
@res.headers[:etag].must_be_etag
check_dir_listing_content_type(@res.headers[:content_type])
@listing["@context"].must_equal "http://remotestorage.io/spec/folder-description"
@listing["items"].each_pair do |key, value|
key.must_be_kind_of String
value["ETag"].must_be_kind_of String
if key[-1] == "/"
value.keys.must_equal ["ETag"]
else
value["Content-Length"].must_be_kind_of Integer
value["Content-Type"].must_be_kind_of String
end
end
end
it "contains the correct items" do
@listing["items"].length.must_equal 5
["Capture d'écran.jpg", "my-list", "some-subdir/",
"test-object-simple.json", "test-object-simple2.json"].each do |key|
@listing["items"].keys.must_include key
end
end
end
describe "PUT a JSON object to root dir" do
it "fails with normal token" do
res = do_put_request("thisisbadpractice.json", '{"new": "object"}',
{ content_type: "application/json" })
[401, 403].must_include res.code
end
it "works with root token" do
res = do_put_request("thisisbadpractice.json", '{"new": "object"}',
{ content_type: "application/json",
authorization: "Bearer #{CONFIG[:root_token]}"})
[200, 201].must_include res.code
res.headers[:etag].wont_be_nil
res.headers[:etag].must_be_etag
end
end
describe "HEAD directory listing of root dir" do
before do
@res = do_head_request("", {authorization: "Bearer #{CONFIG[:root_token]}"})
end
it "works" do
[200, 204].must_include @res.code
@res.headers[:etag].must_be_etag
check_dir_listing_content_type(@res.headers[:content_type])
@res.body.must_equal ""
end
end
describe "GET directory listing of root dir" do
before do
@res = do_get_request("", {authorization: "Bearer #{CONFIG[:root_token]}"})
@listing = JSON.parse @res.body
end
it "works" do
@res.code.must_equal 200
@res.headers[:etag].must_be_etag
check_dir_listing_content_type(@res.headers[:content_type])
@listing["@context"].must_equal "http://remotestorage.io/spec/folder-description"
@listing["items"].each_pair do |key, value|
key.must_be_kind_of String
value["ETag"].must_be_kind_of String
if key[-1] == "/"
value.keys.must_equal ["ETag"]
else
value["Content-Length"].must_be_kind_of Integer
value["Content-Type"].must_be_kind_of String
end
end
end
it "contains the correct items" do
@listing["items"].keys.must_include "#{CONFIG[:category]}/"
@listing["items"].keys.must_include "thisisbadpractice.json"
@listing["items"].count.must_equal 2
end
end
describe "DELETE object in root dir" do
it "works" do
res = do_delete_request("thisisbadpractice.json",
{authorization: "Bearer #{CONFIG[:root_token]}"})
res.code.must_equal 200
do_head_request("thisisbadpractice.json", {authorization: "Bearer #{CONFIG[:root_token]}"}) do |response|
response.code.must_equal 404
end
end
end
describe "GET directory listing with If-None-Match header" do
before do
@etag = do_head_request("#{CONFIG[:category]}/").headers[:etag]
do_get_request("#{CONFIG[:category]}/", { if_none_match: @etag }) do |response|
@res = response
end
end
it "returns 304 with empty body when ETag matches" do
@res.code.must_equal 304
@res.body.must_be_empty
end
end
describe "GET directory listing with multiple ETags in If-None-Match header" do
before do
@etag = do_head_request("#{CONFIG[:category]}/").headers[:etag]
do_get_request("#{CONFIG[:category]}/", { if_none_match: %Q("r2d2c3po", #{@etag}) }) do |response|
@res = response
end
end
it "returns 304 when one ETag matches" do
@res.code.must_equal 304
@res.body.must_be_empty
end
end
describe "GET empty directory listing" do
before do
@res = do_get_request("#{CONFIG[:category]}/does-not-exist/")
@listing = JSON.parse @res.body
end
it "works" do
@res.code.must_equal 200
@listing["@context"].must_equal "http://remotestorage.io/spec/folder-description"
@listing["items"].must_equal({})
end
end
describe "using base URL of a different user" do
it "should fail" do
["GET", "PUT", "DELETE"].each do |method|
res = do_network_request("#{CONFIG[:category]}/failwhale.png",
method: method,
base_url: CONFIG[:storage_base_url_other])
[401, 403].must_include res.code
end
end
end
describe "using a read-only token" do
describe "GET" do
it "works" do
res = do_get_request("#{CONFIG[:category]}/test-object-simple.json",
authorization: "Bearer #{CONFIG[:read_only_token]}")
res.code.must_equal 200
end
end
describe "HEAD" do
it "works" do
res = do_head_request("#{CONFIG[:category]}/test-object-simple.json",
authorization: "Bearer #{CONFIG[:read_only_token]}")
[200, 204].must_include res.code
res.body.must_be_empty
end
end
describe "PUT" do
it "fails" do
res = do_put_request("#{CONFIG[:category]}/test-object-simple-test.json",
'{"new": "object"}',
{ content_type: "application/json",
authorization: "Bearer #{CONFIG[:read_only_token]}" })
[401, 403].must_include res.code
end
end
describe "DELETE" do
it "fails" do
res = do_delete_request("#{CONFIG[:category]}/test-object-simple.json",
authorization: "Bearer #{CONFIG[:read_only_token]}")
[401, 403].must_include res.code
end
end
end
describe "in a public folder" do
describe "PUT with a read/write category token" do
it "works" do
res = do_put_request("public/#{CONFIG[:category]}/test-object-simple.json",
'{"new": "object"}',
{ content_type: "application/json" })
[200, 201].must_include res.code
end
end
describe "PUT with a read/write category token to wrong category" do
it "fails" do
res = do_put_request("public/othercategory/test-object-simple.json",
'{"new": "object"}',
{ content_type: "application/json" })
[401, 403].must_include res.code
end
end
describe "GET without a token" do
it "works" do
res = do_get_request("public/#{CONFIG[:category]}/test-object-simple.json",
authorization: nil)
res.code.must_equal 200
end
end
describe "HEAD without a token" do
it "works" do
res = do_head_request("public/#{CONFIG[:category]}/test-object-simple.json",
authorization: nil)
[200, 204].must_include res.code
res.body.must_be_empty
end
end
describe "PUT without a token" do
it "is not allowed" do
res = do_put_request("public/#{CONFIG[:category]}/test-object-simple-test.json",
'{"new": "object"}',
{ content_type: "application/json",
authorization: nil })
[401, 403].must_include res.code
end
end
describe "GET directory listing without a token" do
it "is not allowed" do
res = do_get_request("public/#{CONFIG[:category]}/", authorization: nil)
[401, 403].must_include res.code
end
it "doesn't expose if folder is empty" do
res = do_get_request("public/#{CONFIG[:category]}/", authorization: nil)
res2 = do_get_request("public/#{CONFIG[:category]}/foo/", authorization: nil)
res.code.must_equal res2.code
res.headers.must_equal res2.headers
res.body.must_equal res2.body
end
end
describe "GET directory listing with a read-write category token" do
it "works" do
res = do_get_request("public/#{CONFIG[:category]}/")
res.code.must_equal 200
end
end
describe "DELETE without a token" do
it "is not allowed" do
res = do_delete_request("public/#{CONFIG[:category]}/test-object-simple.json",
authorization: nil)
[401, 403].must_include res.code
end
end
describe "DELETE with a read/write category token" do
it "works" do
res = do_delete_request("public/#{CONFIG[:category]}/test-object-simple.json")
[200, 204].must_include res.code
end
end
end
describe "DELETE objects" do
it "works" do
[ "test-object-simple.json", "Capture d'écran.jpg",
"some-subdir/nested-folder-object.json", "my-list" ].each do |key|
res = do_delete_request("#{CONFIG[:category]}/#{key}")
[200, 204].must_include res.code
do_head_request("#{CONFIG[:category]}/#{key}") do |response|
response.code.must_equal 404
end
end
end
end
describe "DELETE a non-existing object" do
it "returns a 404" do
do_delete_request("#{CONFIG[:category]}/four-oh-four.html") do |response|
response.code.must_equal 404
end
end
end
describe "DELETE with non-matching If-Match header" do
before do
do_delete_request("#{CONFIG[:category]}/test-object-simple2.json", {if_match: %Q("invalid")}) do |response|
@res = response
end
end
it "does not delete the object" do
@res.code.must_equal 412
do_head_request("#{CONFIG[:category]}/test-object-simple2.json") do |response|
response.code.must_equal 200
end
end
end
describe "DELETE with matching If-Match header" do
before do
etag = do_head_request("#{CONFIG[:category]}/test-object-simple2.json").headers[:etag]
@res = do_delete_request("#{CONFIG[:category]}/test-object-simple2.json", {if_match: etag})
end
it "deletes the object" do
[200, 204].must_include @res.code
do_head_request("#{CONFIG[:category]}/test-object-simple2.json") do |response|
response.code.must_equal 404
end
end
end
describe "DELETE with If-Match header to non-existing object" do
before do
do_delete_request("#{CONFIG[:category]}/four-oh-four.json", {if_match: %Q("match me")}) do |response|
@res = response
end
end
it "returns 412" do
@res.code.must_equal 412
end
end
end