|
| 1 | +import { describe, expect, test } from '@jest/globals'; |
| 2 | + |
| 3 | +import { mapS3Encryption } from './map-s3-encryption.js'; |
| 4 | +import { aws_s3, Stack } from 'aws-cdk-lib'; |
| 5 | + |
| 6 | +describe(`mapS3Encryption()`, () => { |
| 7 | + test(`returns S3_MANAGED encryption when no encryption is provided`, () => { |
| 8 | + const result = mapS3Encryption(undefined as any, {} as Stack); |
| 9 | + expect(result).toEqual({ encryption: aws_s3.BucketEncryption.S3_MANAGED }); |
| 10 | + }); |
| 11 | + |
| 12 | + test(`returns S3_MANAGED encryption when S3_MANAGED is provided`, () => { |
| 13 | + const result = mapS3Encryption({ encryption: 'S3_MANAGED' }, {} as Stack); |
| 14 | + expect(result).toEqual({ encryption: aws_s3.BucketEncryption.S3_MANAGED }); |
| 15 | + }); |
| 16 | + |
| 17 | + test(`returns KMS_MANAGED encryption when KMS_MANAGED is provided`, () => { |
| 18 | + const result = mapS3Encryption({ encryption: 'KMS_MANAGED' }, {} as Stack); |
| 19 | + expect(result).toEqual({ |
| 20 | + encryption: aws_s3.BucketEncryption.KMS_MANAGED, |
| 21 | + bucketKeyEnabled: true, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + test(`returns KMS_MANAGED encryption with bucketKeyEnabled when KMS_MANAGED and bucketKeyEnabled are provided`, () => { |
| 26 | + const result = mapS3Encryption( |
| 27 | + { encryption: 'KMS_MANAGED', bucketKeyEnabled: false }, |
| 28 | + {} as Stack, |
| 29 | + ); |
| 30 | + expect(result).toEqual({ |
| 31 | + encryption: aws_s3.BucketEncryption.KMS_MANAGED, |
| 32 | + bucketKeyEnabled: false, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + test(`returns KMS encryption with key when KMS and encryptionKeyArn are provided`, () => { |
| 37 | + const stack = new Stack(); |
| 38 | + expect(Stack.isStack(stack)).toBeTruthy(); |
| 39 | + |
| 40 | + const keyArn = 'arn:aws:kms:region:account-id:key/key-id'; |
| 41 | + const result = mapS3Encryption({ encryption: 'KMS', encryptionKeyArn: keyArn }, stack); |
| 42 | + expect(result.encryption).toEqual(aws_s3.BucketEncryption.KMS); |
| 43 | + expect(result.encryptionKey).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + test(`returns KMS encryption without key when KMS is provided without encryptionKeyArn`, () => { |
| 47 | + const result = mapS3Encryption({ encryption: 'KMS' }, {} as Stack); |
| 48 | + expect(result).toEqual({ |
| 49 | + encryption: aws_s3.BucketEncryption.KMS, |
| 50 | + encryptionKey: undefined, |
| 51 | + bucketKeyEnabled: true, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + test(`returns KMS encryption with bucketKeyEnabled when KMS, bucketKeyEnabled and encryptionKeyArn are provided`, () => { |
| 56 | + const stack = new Stack(); |
| 57 | + expect(Stack.isStack(stack)).toBeTruthy(); |
| 58 | + |
| 59 | + const keyArn = 'arn:aws:kms:region:account-id:key/key-id'; |
| 60 | + const result = mapS3Encryption( |
| 61 | + { encryption: 'KMS', encryptionKeyArn: keyArn, bucketKeyEnabled: false }, |
| 62 | + stack, |
| 63 | + ); |
| 64 | + expect(result).toEqual({ |
| 65 | + encryption: aws_s3.BucketEncryption.KMS, |
| 66 | + encryptionKey: expect.any(Object), |
| 67 | + bucketKeyEnabled: false, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + test(`throws an error for unsupported encryption types`, () => { |
| 72 | + expect(() => mapS3Encryption({ encryption: 'UNSUPPORTED' } as any, {} as Stack)).toThrow( |
| 73 | + 'Unsupported encryption type', |
| 74 | + ); |
| 75 | + }); |
| 76 | +}); |
0 commit comments