11const assert = require ( 'assert' ) ;
22const tv4 = require ( 'tv4' ) ;
3+ const {
4+ PutObjectCommand,
5+ ListObjectsCommand,
6+ ListObjectsV2Command,
7+ } = require ( '@aws-sdk/client-s3' ) ;
38
49const withV4 = require ( '../support/withV4' ) ;
510const BucketUtility = require ( '../../lib/utility/bucket-util' ) ;
@@ -287,12 +292,14 @@ const tests = [
287292
288293describe ( 'GET Bucket - AWS.S3.listObjects' , ( ) => {
289294 describe ( 'When user is unauthorized' , ( ) => {
290- let bucketUtil ;
291295 let bucketName ;
296+ let authenticatedBucketUtil ;
297+ let unauthenticatedBucketUtil ;
292298
293299 before ( done => {
294- bucketUtil = new BucketUtility ( ) ;
295- bucketUtil . createRandom ( 1 )
300+ authenticatedBucketUtil = new BucketUtility ( 'default' , { } ) ;
301+ unauthenticatedBucketUtil = new BucketUtility ( 'default' , { } , true ) ;
302+ authenticatedBucketUtil . createRandom ( 1 )
296303 . then ( created => {
297304 bucketName = created ;
298305 done ( ) ;
@@ -301,18 +308,20 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
301308 } ) ;
302309
303310 after ( done => {
304- bucketUtil . deleteOne ( bucketName )
311+ authenticatedBucketUtil . deleteOne ( bucketName )
305312 . then ( ( ) => done ( ) )
306313 . catch ( done ) ;
307314 } ) ;
308315
309316 it ( 'should return 403 and AccessDenied on a private bucket' , done => {
310317 const params = { Bucket : bucketName } ;
311- bucketUtil . s3
312- . makeUnauthenticatedRequest ( 'listObjects' , params , error => {
313- assert ( error ) ;
314- assert . strictEqual ( error . statusCode , 403 ) ;
315- assert . strictEqual ( error . code , 'AccessDenied' ) ;
318+ unauthenticatedBucketUtil . s3 . send ( new ListObjectsCommand ( params ) )
319+ . then ( ( ) => {
320+ assert . fail ( 'Expected request to fail with AccessDenied' ) ;
321+ } )
322+ . catch ( error => {
323+ assert . strictEqual ( error . $metadata . httpStatusCode , 403 ) ;
324+ assert . strictEqual ( error . name , 'AccessDenied' ) ;
316325 done ( ) ;
317326 } ) ;
318327 } ) ;
@@ -332,27 +341,28 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
332341 . catch ( done ) ;
333342 } ) ;
334343
335- after ( done => {
336- bucketUtil . deleteOne ( bucketName ) . then ( ( ) => done ( ) ) . catch ( done ) ;
337- } ) ;
344+ after ( ( ) => bucketUtil . deleteOne ( bucketName ) ) ;
338345
339- afterEach ( done => {
340- bucketUtil . empty ( bucketName ) . then ( ( ) => done ( ) ) . catch ( done ) ;
341- } ) ;
346+ afterEach ( ( ) => bucketUtil . empty ( bucketName ) ) ;
342347
343348 tests . forEach ( test => {
344349 it ( `should ${ test . name } ` , async ( ) => {
345350 const s3 = bucketUtil . s3 ;
346351 const Bucket = bucketName ;
347352 for ( const param of test . objectPutParams ( Bucket ) ) {
348- await s3 . putObject ( param ) . promise ( ) ;
353+ await s3 . send ( new PutObjectCommand ( param ) ) ;
349354 }
350- const data = await s3 . listObjects ( test . listObjectParams ( Bucket ) ) . promise ( ) ;
351- const isValidResponse = tv4 . validate ( data , bucketSchema ) ;
355+ const { $metadata, ...data } = await s3 . send ( new ListObjectsCommand ( test . listObjectParams ( Bucket ) ) ) ;
356+ const validationSchema = {
357+ ...bucketSchema ,
358+ required : bucketSchema . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
359+ } ;
360+ const isValidResponse = tv4 . validate ( data , validationSchema ) ;
352361 if ( ! isValidResponse ) {
353362 throw new Error ( tv4 . error ) ;
354363 }
355364 test . assertions ( data , Bucket ) ;
365+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
356366 } ) ;
357367 } ) ;
358368
@@ -362,14 +372,19 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
362372 const Bucket = bucketName ;
363373
364374 for ( const param of test . objectPutParams ( Bucket ) ) {
365- await s3 . putObject ( param ) . promise ( ) ;
375+ await s3 . send ( new PutObjectCommand ( param ) ) ;
366376 }
367- const data = await s3 . listObjectsV2 ( test . listObjectParams ( Bucket ) ) . promise ( ) ;
368- const isValidResponse = tv4 . validate ( data , bucketSchemaV2 ) ;
377+ const { $metadata, ...data } = await s3 . send ( new ListObjectsV2Command ( test . listObjectParams ( Bucket ) ) ) ;
378+ const validationSchema2 = {
379+ ...bucketSchemaV2 ,
380+ required : bucketSchemaV2 . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
381+ } ;
382+ const isValidResponse = tv4 . validate ( data , validationSchema2 ) ;
369383 if ( ! isValidResponse ) {
370384 throw new Error ( tv4 . error ) ;
371385 }
372386 test . assertions ( data , Bucket ) ;
387+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
373388 } ) ;
374389 } ) ;
375390
@@ -380,14 +395,19 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
380395 const objects = [ { Bucket, Key : k } ] ;
381396
382397 for ( const param of objects ) {
383- await s3 . putObject ( param ) . promise ( ) ;
384- }
385- const data = await s3 . listObjects ( { Bucket, Prefix : k } ) . promise ( ) ;
386- const isValidResponse = tv4 . validate ( data , bucketSchema ) ;
398+ await s3 . send ( new PutObjectCommand ( param ) ) ;
399+ }
400+ const { $metadata, ...data } = await s3 . send ( new ListObjectsCommand ( { Bucket, Prefix : k } ) ) ;
401+ const validationSchema = {
402+ ...bucketSchema ,
403+ required : bucketSchema . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
404+ } ;
405+ const isValidResponse = tv4 . validate ( data , validationSchema ) ;
387406 if ( ! isValidResponse ) {
388407 throw new Error ( tv4 . error ) ;
389408 }
390409 assert . deepStrictEqual ( data . Prefix , k ) ;
410+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
391411 } ) ;
392412 } ) ;
393413
@@ -398,14 +418,19 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
398418 const objects = [ { Bucket, Key : k } ] ;
399419
400420 for ( const param of objects ) {
401- await s3 . putObject ( param ) . promise ( ) ;
421+ await s3 . send ( new PutObjectCommand ( param ) ) ;
402422 }
403- const data = await s3 . listObjects ( { Bucket, Marker : k } ) . promise ( ) ;
404- const isValidResponse = tv4 . validate ( data , bucketSchema ) ;
423+ const { $metadata, ...data } = await s3 . send ( new ListObjectsCommand ( { Bucket, Marker : k } ) ) ;
424+ const validationSchema = {
425+ ...bucketSchema ,
426+ required : bucketSchema . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
427+ } ;
428+ const isValidResponse = tv4 . validate ( data , validationSchema ) ;
405429 if ( ! isValidResponse ) {
406430 throw new Error ( tv4 . error ) ;
407431 }
408432 assert . deepStrictEqual ( data . Marker , k ) ;
433+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
409434 } ) ;
410435 } ) ;
411436
@@ -416,15 +441,21 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
416441 const objects = [ { Bucket, Key : k } , { Bucket, Key : 'zzz' } ] ;
417442
418443 for ( const param of objects ) {
419- await s3 . putObject ( param ) . promise ( ) ;
444+ await s3 . send ( new PutObjectCommand ( param ) ) ;
420445 }
421- const data = await s3 . listObjects ( { Bucket, MaxKeys : 1 ,
422- Delimiter : 'foo' } ) . promise ( ) ;
423- const isValidResponse = tv4 . validate ( data , bucketSchema ) ;
446+ const { $metadata, ...data } = await s3 . send ( new ListObjectsCommand ( { Bucket, MaxKeys : 1 ,
447+ Delimiter : 'foo' } ) ) ;
448+
449+ const validationSchema = {
450+ ...bucketSchema ,
451+ required : bucketSchema . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
452+ } ;
453+ const isValidResponse = tv4 . validate ( data , validationSchema ) ;
424454 if ( ! isValidResponse ) {
425455 throw new Error ( tv4 . error ) ;
426456 }
427457 assert . strictEqual ( data . NextMarker , k ) ;
458+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
428459 } ) ;
429460 } ) ;
430461
@@ -435,15 +466,19 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
435466 const objects = [ { Bucket, Key : k } ] ;
436467
437468 for ( const param of objects ) {
438- await s3 . putObject ( param ) . promise ( ) ;
469+ await s3 . send ( new PutObjectCommand ( param ) ) ;
439470 }
440- const data = await s3 . listObjectsV2 (
441- { Bucket, StartAfter : k } ) . promise ( ) ;
442- const isValidResponse = tv4 . validate ( data , bucketSchemaV2 ) ;
471+ const { $metadata, ...data } = await s3 . send ( new ListObjectsV2Command ( { Bucket, StartAfter : k } ) ) ;
472+ const validationSchema2 = {
473+ ...bucketSchemaV2 ,
474+ required : bucketSchemaV2 . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
475+ } ;
476+ const isValidResponse = tv4 . validate ( data , validationSchema2 ) ;
443477 if ( ! isValidResponse ) {
444478 throw new Error ( tv4 . error ) ;
445479 }
446480 assert . deepStrictEqual ( data . StartAfter , k ) ;
481+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
447482 } ) ;
448483 } ) ;
449484
@@ -455,18 +490,23 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
455490 const objects = [ { Bucket, Key : k } ] ;
456491
457492 for ( const param of objects ) {
458- await s3 . putObject ( param ) . promise ( ) ;
493+ await s3 . send ( new PutObjectCommand ( param ) ) ;
459494 }
460- const data = await s3 . listObjectsV2 ( {
495+ const { $metadata , ... data } = await s3 . send ( new ListObjectsV2Command ( {
461496 Bucket,
462497 ContinuationToken : generateToken ( k ) ,
463- } ) . promise ( ) ;
464- const isValidResponse = tv4 . validate ( data , bucketSchemaV2 ) ;
498+ } ) ) ;
499+ const validationSchema2 = {
500+ ...bucketSchemaV2 ,
501+ required : bucketSchemaV2 . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
502+ } ;
503+ const isValidResponse = tv4 . validate ( data , validationSchema2 ) ;
465504 if ( ! isValidResponse ) {
466505 throw new Error ( tv4 . error ) ;
467506 }
468507 assert . deepStrictEqual (
469508 decryptToken ( data . ContinuationToken ) , k ) ;
509+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
470510 } ) ;
471511 } ) ;
472512
@@ -478,16 +518,21 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
478518 const objects = [ { Bucket, Key : k } , { Bucket, Key : 'zzz' } ] ;
479519
480520 for ( const param of objects ) {
481- await s3 . putObject ( param ) . promise ( ) ;
521+ await s3 . send ( new PutObjectCommand ( param ) ) ;
482522 }
483- const data = await s3 . listObjectsV2 ( { Bucket, MaxKeys : 1 ,
484- Delimiter : 'foo' } ) . promise ( ) ;
485- const isValidResponse = tv4 . validate ( data , bucketSchemaV2 ) ;
523+ const { $metadata, ...data } = await s3 . send ( new ListObjectsV2Command ( { Bucket, MaxKeys : 1 ,
524+ Delimiter : 'foo' } ) ) ;
525+ const validationSchema2 = {
526+ ...bucketSchemaV2 ,
527+ required : bucketSchemaV2 . required . filter ( field => Object . prototype . hasOwnProperty . call ( data , field ) )
528+ } ;
529+ const isValidResponse = tv4 . validate ( data , validationSchema2 ) ;
486530 if ( ! isValidResponse ) {
487531 throw new Error ( tv4 . error ) ;
488532 }
489533 assert . strictEqual (
490534 decryptToken ( data . NextContinuationToken ) , k ) ;
535+ assert . strictEqual ( $metadata . httpStatusCode , 200 ) ;
491536 } ) ;
492537 } ) ;
493538 } ) ;
0 commit comments