Skip to content

Commit 9e542d4

Browse files
author
Doug Toppin
committed
Add unit test for crop
prettier on crop test update snapshot
1 parent 442a3ce commit 9e542d4

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

source/constructs/test/__snapshots__/constructs.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ exports[`Serverless Image Handler Stack Snapshot 1`] = `
11671167
"S3Bucket": {
11681168
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
11691169
},
1170-
"S3Key": "81baf2ab3a41584170190dc0f2ce62f50e091a14cab8fd069cb3aebe903297c5.zip",
1170+
"S3Key": "c835083515d3d05edc1afeea0c49b2617385f676492fc6dc7e805f829947db72.zip",
11711171
},
11721172
"Description": "sih (v6.1.0): Performs image edits and manipulations",
11731173
"Environment": {
@@ -1621,7 +1621,7 @@ exports[`Serverless Image Handler Stack Snapshot 1`] = `
16211621
},
16221622
],
16231623
"SourceObjectKeys": [
1624-
"b17ee622b8fe21bd9a4189063340647fbb783d07261ea136b83db1d58255b0b2.zip",
1624+
"297994e5975cd83b2ec43f52720181be7d5daf08af8987cc4d5749f99db8f777.zip",
16251625
],
16261626
},
16271627
"Type": "Custom::CDKBucketDeployment",

source/image-handler/test/image-handler/crop.spec.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@ import { ImageEdits, StatusCodes } from "../../lib";
1010
const s3Client = new S3();
1111
const rekognitionClient = new Rekognition();
1212

13+
// base64 encoded images
14+
const image_png_1x1 =
15+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
16+
const image_png_white_5x5 =
17+
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFAQAAAAClFBtIAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAd2KE6QAAAAHdElNRQfnAxYODhUMhxdmAAAADElEQVQI12P4wQCFABhCBNn4i/hQAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTAzLTIyVDE0OjE0OjIxKzAwOjAwtK8ALAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyMy0wMy0yMlQxNDoxNDoyMSswMDowMMXyuJAAAAAASUVORK5CYII=";
18+
const image_png_white_1x1 =
19+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVR4nGP4//8/AAX+Av4N70a4AAAAAElFTkSuQmCC";
20+
1321
describe("crop", () => {
1422
it("Should pass if a cropping area value is out of bounds", async () => {
1523
// Arrange
16-
const originalImage = Buffer.from(
17-
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
18-
"base64"
19-
);
24+
const originalImage = Buffer.from(image_png_1x1, "base64");
2025
const image = sharp(originalImage, { failOnError: false }).withMetadata();
2126
const edits: ImageEdits = {
22-
crop: { left: 0, right: 0, width: 100, height: 100 },
27+
crop: { left: 0, top: 0, width: 100, height: 100 },
2328
};
2429

2530
// Act
@@ -36,4 +41,53 @@ describe("crop", () => {
3641
});
3742
}
3843
});
44+
45+
// confirm that crops perform as expected
46+
it("Should pass with a standard crop", async () => {
47+
// 5x5 png
48+
const originalImage = Buffer.from(image_png_white_5x5, "base64");
49+
const image = sharp(originalImage, { failOnError: false }).withMetadata();
50+
const edits: ImageEdits = {
51+
crop: { left: 0, top: 0, width: 1, height: 1 },
52+
};
53+
54+
// crop an image and compare with the result expected
55+
try {
56+
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
57+
const result = await imageHandler.applyEdits(image, edits, false);
58+
const resultBuffer = await result.toBuffer();
59+
expect(resultBuffer).toEqual(Buffer.from(image_png_white_1x1, "base64"));
60+
} catch (error) {
61+
console.log(error);
62+
throw error;
63+
}
64+
});
65+
66+
// confirm that an invalid attribute sharp crop request containing *right* rather than *top* returns as a cropping error,
67+
// note that this only confirms the behavior of the image-handler in this case,
68+
// it is not an accurate description of the actual error
69+
it("Should fail with an invalid crop request", async () => {
70+
// 5x5 png
71+
const originalImage = Buffer.from(image_png_white_5x5, "base64");
72+
const image = sharp(originalImage, { failOnError: false }).withMetadata();
73+
const edits: ImageEdits = {
74+
crop: { left: 0, right: 0, width: 1, height: 1 },
75+
};
76+
77+
// crop an image and compare with the result expected
78+
try {
79+
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
80+
const result = await imageHandler.applyEdits(image, edits, false);
81+
const resultBuffer = await result.toBuffer();
82+
expect(resultBuffer).toEqual(Buffer.from(image_png_white_1x1, "base64"));
83+
} catch (error) {
84+
// Assert
85+
expect(error).toMatchObject({
86+
status: StatusCodes.BAD_REQUEST,
87+
code: "Crop::AreaOutOfBounds",
88+
message:
89+
"The cropping area you provided exceeds the boundaries of the original image. Please try choosing a correct cropping value.",
90+
});
91+
}
92+
});
3993
});

0 commit comments

Comments
 (0)