Skip to content

Commit

Permalink
Merge pull request #9 from Theodo-UK/feature/s3-item-contents
Browse files Browse the repository at this point in the history
Add toHaveContentEqualTo assertion
  • Loading branch information
hamilton-s authored Jul 26, 2021
2 parents 58d03f0 + c6690b8 commit b954e35
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Note: these async assertions require "await"

where CONTENT_TYPE are [standards MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types)

```
await expect({
bucketName: "BUCKET_NAME",
objectName: "FILE NAME",
}).toHaveContentEqualTo("CONTENT");
```

## Helpers

### General
Expand Down
21 changes: 7 additions & 14 deletions src/assertions/toExistAsS3Bucket/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";

export default {
async toExistAsS3Bucket(bucketName) {
Expand All @@ -7,25 +8,17 @@ export default {
Bucket: bucketName,
};

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

return testResult;
},
};
33 changes: 33 additions & 0 deletions src/assertions/toHaveContentEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";

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

let message;
try {
const object = await s3.getObject(params).promise();
if (JSON.stringify(object.Body) === JSON.stringify(content)) {
message = `expected ${objectName} to have content ${content}`;
return testResult(message, true);
}
message = `expected ${objectName} to have content ${content}, but content found was ${object.Body}`;
return testResult(message, false);
} catch (error) {
if (error.code === "NoSuchKey") {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;
return testResult(message, false);
}
if (error.code === "NoSuchBucket") {
message = `expected ${bucketName} to exist - not found`;
return testResult(message, false);
}
throw error;
}
},
};
15 changes: 7 additions & 8 deletions src/assertions/toHaveEvent/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { testResult } from "../../utils/testResult";

export default {
toHaveEvent(eventBridgeEvents) {
let message;
if (eventBridgeEvents) {
return {
message: () => "expected to have message in EventBridge Bus",
pass: true,
};
message = "expected to have message in EventBridge Bus";
return testResult(message, true);
}
return {
message: () => "no message intercepted from EventBridge Bus",
pass: false,
};
message = "no message intercepted from EventBridge Bus";
return testResult(message, false);
},
};
26 changes: 9 additions & 17 deletions src/assertions/toHaveObjectWithNameEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AWSClient } from '../../helpers/general';
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";

export default {
async toHaveS3ObjectWithNameEqualTo(bucketName, objectName) {
Expand All @@ -8,26 +9,17 @@ export default {
Key: objectName,
};

let testResult;
let message;
try {
await s3.getObject(params).promise();
testResult = {
message: () =>
`expected ${bucketName} to have object with name ${objectName}`,
pass: true,
};
message = `expected ${bucketName} to have object with name ${objectName}`;
return testResult(message, true);
} catch (error) {
if (error.code === 'NoSuchKey') {
testResult = {
message: () =>
`expected ${bucketName} to have object with name ${objectName} - not found`,
pass: false,
};
} else {
throw error;
if (error.code === "NoSuchKey") {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;
return testResult(message, false);
}
throw error;
}

return testResult;
},
};

0 comments on commit b954e35

Please sign in to comment.