Skip to content

Commit

Permalink
s3
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed Aug 28, 2021
1 parent 05f7bc7 commit 0132a1f
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 168 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"jest": "^26.6.3",
"pkglint": "0.0.0",
"nodeunit-shim": "0.0.0",
"@aws-cdk/assert-internal": "0.0.0"
},
"dependencies": {
Expand Down
21 changes: 10 additions & 11 deletions packages/@aws-cdk/aws-s3/test/aspect.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// import { expect, haveResource, haveResourceLike, SynthUtils } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { SynthUtils } from '@aws-cdk/assert-internal';
import * as cdk from '@aws-cdk/core';
import { IConstruct } from 'constructs';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as s3 from '../lib';

nodeunitShim({
'bucket must have versioning: failure'(test: Test) {
describe('aspect', () => {
test('bucket must have versioning: failure', () => {
// GIVEN
const stack = new cdk.Stack();
new s3.Bucket(stack, 'MyBucket');
Expand All @@ -17,12 +16,12 @@ nodeunitShim({
// THEN
const assembly = SynthUtils.synthesize(stack);
const errorMessage = assembly.messages.find(m => m.entry.data === 'Bucket versioning is not enabled');
test.ok(errorMessage, 'Error message not reported');
expect(errorMessage).toBeDefined();

test.done();
},

'bucket must have versioning: success'(test: Test) {
});

test('bucket must have versioning: success', () => {
// GIVEN
const stack = new cdk.Stack();
new s3.Bucket(stack, 'MyBucket', {
Expand All @@ -34,10 +33,10 @@ nodeunitShim({

// THEN
const assembly = SynthUtils.synthesize(stack);
test.deepEqual(assembly.messages, []);
expect(assembly.messages.length).toEqual(0);


test.done();
},
});
});

class BucketVersioningChecker implements cdk.IAspect {
Expand Down
47 changes: 23 additions & 24 deletions packages/@aws-cdk/aws-s3/test/bucket-policy.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { expect, haveResource } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { AnyPrincipal, PolicyStatement } from '@aws-cdk/aws-iam';
import { RemovalPolicy, Stack, App } from '@aws-cdk/core';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as s3 from '../lib';

// to make it easy to copy & paste from output:
/* eslint-disable quote-props */

nodeunitShim({
'default properties'(test: Test) {
describe('bucket policy', () => {
test('default properties', () => {
const stack = new Stack();

const myBucket = new s3.Bucket(stack, 'MyBucket');
Expand All @@ -21,7 +20,7 @@ nodeunitShim({
principals: [new AnyPrincipal()],
}));

expect(stack).to(haveResource('AWS::S3::BucketPolicy', {
expect(stack).toHaveResource('AWS::S3::BucketPolicy', {
Bucket: {
'Ref': 'MyBucketF68F3FF0',
},
Expand All @@ -36,12 +35,12 @@ nodeunitShim({
},
],
},
}));
});

test.done();
},

'when specifying a removalPolicy at creation'(test: Test) {
});

test('when specifying a removalPolicy at creation', () => {
const stack = new Stack();

const myBucket = new s3.Bucket(stack, 'MyBucket');
Expand All @@ -55,7 +54,7 @@ nodeunitShim({
principals: [new AnyPrincipal()],
}));

expect(stack).toMatch({
expect(stack).toMatchTemplate({
'Resources': {
'MyBucketF68F3FF0': {
'Type': 'AWS::S3::Bucket',
Expand Down Expand Up @@ -86,10 +85,10 @@ nodeunitShim({
},
});

test.done();
},

'when specifying a removalPolicy after creation'(test: Test) {
});

test('when specifying a removalPolicy after creation', () => {
const stack = new Stack();

const myBucket = new s3.Bucket(stack, 'MyBucket');
Expand All @@ -100,7 +99,7 @@ nodeunitShim({
}));
myBucket.policy?.applyRemovalPolicy(RemovalPolicy.RETAIN);

expect(stack).toMatch({
expect(stack).toMatchTemplate({
'Resources': {
'MyBucketF68F3FF0': {
'Type': 'AWS::S3::Bucket',
Expand Down Expand Up @@ -131,10 +130,10 @@ nodeunitShim({
},
});

test.done();
},

'fails if bucket policy has no actions'(test: Test) {
});

test('fails if bucket policy has no actions', () => {
const app = new App();
const stack = new Stack(app, 'my-stack');
const myBucket = new s3.Bucket(stack, 'MyBucket');
Expand All @@ -143,12 +142,12 @@ nodeunitShim({
principals: [new AnyPrincipal()],
}));

test.throws(() => app.synth(), /A PolicyStatement must specify at least one \'action\' or \'notAction\'/);
expect(() => app.synth()).toThrow(/A PolicyStatement must specify at least one \'action\' or \'notAction\'/);


test.done();
},
});

'fails if bucket policy has no IAM principals'(test: Test) {
test('fails if bucket policy has no IAM principals', () => {
const app = new App();
const stack = new Stack(app, 'my-stack');
const myBucket = new s3.Bucket(stack, 'MyBucket');
Expand All @@ -157,8 +156,8 @@ nodeunitShim({
actions: ['s3:GetObject*'],
}));

test.throws(() => app.synth(), /A PolicyStatement used in a resource-based policy must specify at least one IAM principal/);
expect(() => app.synth()).toThrow(/A PolicyStatement used in a resource-based policy must specify at least one IAM principal/);


test.done();
},
});
});
25 changes: 12 additions & 13 deletions packages/@aws-cdk/aws-s3/test/cors.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect, haveResource } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { Stack } from '@aws-cdk/core';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { Bucket, HttpMethods } from '../lib';

nodeunitShim({
'Can use addCors() to add a CORS configuration'(test: Test) {
describe('cors', () => {
test('Can use addCors() to add a CORS configuration', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -16,19 +15,19 @@ nodeunitShim({
});

// THEN
expect(stack).to(haveResource('AWS::S3::Bucket', {
expect(stack).toHaveResource('AWS::S3::Bucket', {
CorsConfiguration: {
CorsRules: [{
AllowedMethods: ['GET', 'HEAD'],
AllowedOrigins: ['https://example.com'],
}],
},
}));
});


test.done();
},
});

'Bucket with multiple cors configurations'(test: Test) {
test('Bucket with multiple cors configurations', () => {
// GIVEN
const stack = new Stack();

Expand Down Expand Up @@ -74,7 +73,7 @@ nodeunitShim({
});

// THEN
expect(stack).to(haveResource('AWS::S3::Bucket', {
expect(stack).toHaveResource('AWS::S3::Bucket', {
CorsConfiguration: {
CorsRules: [
{
Expand Down Expand Up @@ -114,8 +113,8 @@ nodeunitShim({
},
],
},
}));
});


test.done();
},
});
});
45 changes: 22 additions & 23 deletions packages/@aws-cdk/aws-s3/test/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect, haveResource } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { Stack } from '@aws-cdk/core';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { Bucket } from '../lib';

nodeunitShim({
'Can use addMetrics() to add a metric configuration'(test: Test) {
describe('metrics', () => {
test('Can use addMetrics() to add a metric configuration', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -15,16 +14,16 @@ nodeunitShim({
});

// THEN
expect(stack).to(haveResource('AWS::S3::Bucket', {
expect(stack).toHaveResource('AWS::S3::Bucket', {
MetricsConfigurations: [{
Id: 'test',
}],
}));
});


test.done();
},
});

'Bucket with metrics on prefix'(test: Test) {
test('Bucket with metrics on prefix', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -37,17 +36,17 @@ nodeunitShim({
});

// THEN
expect(stack).to(haveResource('AWS::S3::Bucket', {
expect(stack).toHaveResource('AWS::S3::Bucket', {
MetricsConfigurations: [{
Id: 'test',
Prefix: 'prefix',
}],
}));
});


test.done();
},
});

'Bucket with metrics on tag filter'(test: Test) {
test('Bucket with metrics on tag filter', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -60,20 +59,20 @@ nodeunitShim({
});

// THEN
expect(stack).to(haveResource('AWS::S3::Bucket', {
expect(stack).toHaveResource('AWS::S3::Bucket', {
MetricsConfigurations: [{
Id: 'test',
TagFilters: [
{ Key: 'tagname1', Value: 'tagvalue1' },
{ Key: 'tagname2', Value: 'tagvalue2' },
],
}],
}));
});


test.done();
},
});

'Bucket with multiple metric configurations'(test: Test) {
test('Bucket with multiple metric configurations', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -93,7 +92,7 @@ nodeunitShim({
});

// THEN
expect(stack).to(haveResource('AWS::S3::Bucket', {
expect(stack).toHaveResource('AWS::S3::Bucket', {
MetricsConfigurations: [{
Id: 'test',
TagFilters: [
Expand All @@ -105,8 +104,8 @@ nodeunitShim({
Id: 'test2',
Prefix: 'prefix',
}],
}));
});


test.done();
},
});
});
Loading

0 comments on commit 0132a1f

Please sign in to comment.