Skip to content

Commit

Permalink
Merge pull request #7 from Theodo-UK/feature/s3-assertion-bucket-exists
Browse files Browse the repository at this point in the history
Add S3 assertion toExist(bucketName)
  • Loading branch information
hamilton-s authored Jul 26, 2021
2 parents 0a0caa3 + c5ca199 commit 11a83a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ sls-test-tools is currently being actively maintained, yet is in alpha. Your fee
## Assertions:

### EventBridge

```
expect(eventBridgeEvents).toHaveEvent();
expect(eventBridgeEvents).toHaveEventWithSource("order.created");
```

### S3

// note these is an async assertion and requires "await"

```
await expect("BUCKET NAME").toHaveS3ObjectWithNameEqualTo("FILE NAME");
// note this is an async assertion and requires "await"
```

```
await expect("BUCKET NAME").toExistAsS3Bucket();
```

## Helpers

### General

AWSClient - An AWS client with credentials set up

```
Expand All @@ -58,6 +66,7 @@ getOptions() - get options for making requests to AWS
```

### EventBridge

An interface to the deployed EventBridge, allowing events to be injected and intercepted via an SQS queue and EventBridge rule.

#### Static
Expand All @@ -78,16 +87,19 @@ An interface to the deployed EventBridge, allowing events to be injected and int
## Running with `jest`

### Arguments

- When running tests with `jest` using `sls-test-tools` matchers there are certain parameters needed for `sls-test-tools` to make assertions.
- These are passed as command line arguments, using quotation to match `jests` convention on test arguments.

**Required**
- `'--stack=my-service-dev'` - the CloudFormation stack name of the stack under test.

- `'--stack=my-service-dev'` - the CloudFormation stack name of the stack under test.

**Optional**
- `'--profile=[PROFILE NAME]'` (will default to `default`)
- `'--region=[AWS Region]'` (will default to `eu-west-2`)
- `'--keep=true'` - keeps testing resources up to avoid creation throttles (e.g. SQS Queue created for EventBridge assertions)

- `'--profile=[PROFILE NAME]'` (will default to `default`)
- `'--region=[AWS Region]'` (will default to `eu-west-2`)
- `'--keep=true'` - keeps testing resources up to avoid creation throttles (e.g. SQS Queue created for EventBridge assertions)

- To avoid issues we recommend `--runInBand`

Expand Down Expand Up @@ -127,21 +139,22 @@ describe("Integration Testing Event Bridge", () => {
});
it("correctly generates a PDF when an order is created", async () => {
const bucketName = example-bucket
await eventBridge
.publishEvent("order.created", "example", JSON.stringify({ filename: filename }));
await sleep(5000); // wait 5 seconds to allow event to pass
const params = {
Bucket: "example-dev-thumbnails-bucket",
Bucket: bucketName,
Key: filename,
};
// Assert that file was added to the S3 bucket
const obj = await s3.getObject(params).promise();
expect(obj.ContentType).toBe("application/pdf");
await expect("example-dev-thumbnails-bucket").toHaveS3ObjectWithNameEqualTo(
filename
);
});
```

## Contributors ✨
Expand Down
31 changes: 31 additions & 0 deletions src/assertions/toExistAsS3Bucket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AWSClient } from "../../helpers/general";

export default {
async toExistAsS3Bucket(bucketName) {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
};

let testResult;
try {
await s3.headBucket(params).promise();
testResult = {
message: () => `expected S3 bucket to exist with name ${bucketName}`,
pass: true,
};
} catch (error) {
if (error.statusCode === 404) {
testResult = {
message: () =>
`expected S3 bucket to exist with name ${bucketName} - not found`,
pass: false,
};
} else {
throw error;
}
}

return testResult;
},
};

0 comments on commit 11a83a8

Please sign in to comment.