-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Theodo-UK/feature/s3-content-type
Add toHaveContentTypeEqualTo assertion
- Loading branch information
Showing
4 changed files
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 toHaveContentTypeEqualTo({ bucketName, objectName }, contentType) { | ||
const s3 = new AWSClient.S3(); | ||
const params = { | ||
Bucket: bucketName, | ||
Key: objectName, | ||
}; | ||
|
||
let message; | ||
try { | ||
const object = await s3.getObject(params).promise(); | ||
if (object.ContentType === contentType) { | ||
message = `expected ${objectName} to have content type ${contentType}`; | ||
return testResult(message, true); | ||
} | ||
message = `expected ${objectName} to have content type ${contentType}, but content type found was ${object.ContentType}`; | ||
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; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
import { testResult } from "../../utils/testResult"; | ||
|
||
export default { | ||
toHaveEventWithSource(eventBridgeEvents, expectedSourceName) { | ||
let message; | ||
const receivedSource = JSON.parse(eventBridgeEvents.Messages[0].Body) | ||
.source; | ||
if (receivedSource === expectedSourceName) { | ||
return { | ||
message: () => | ||
`expected sent event to have source ${expectedSourceName}`, | ||
pass: true, | ||
}; | ||
message = `expected sent event to have source ${expectedSourceName}`; | ||
return testResult(message, true); | ||
} | ||
return { | ||
message: () => | ||
`sent event source "${receivedSource}" does not match expected source "${expectedSourceName}"`, | ||
pass: false, | ||
}; | ||
message = `sent event source "${receivedSource}" does not match expected source "${expectedSourceName}"`; | ||
return testResult(message, false); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const testResult = (message, pass) => ({ | ||
message: () => message, | ||
pass, | ||
}); |