Skip to content

Commit

Permalink
Add unit test for crop, prettier on crop test, update snapshot, Remov…
Browse files Browse the repository at this point in the history
…e unneeded try/catch in image-handler test
  • Loading branch information
Doug Toppin committed Mar 28, 2023
1 parent 442a3ce commit 998a921
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions source/constructs/test/__snapshots__/constructs.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ exports[`Serverless Image Handler Stack Snapshot 1`] = `
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
},
"S3Key": "81baf2ab3a41584170190dc0f2ce62f50e091a14cab8fd069cb3aebe903297c5.zip",
"S3Key": "c835083515d3d05edc1afeea0c49b2617385f676492fc6dc7e805f829947db72.zip",
},
"Description": "sih (v6.1.0): Performs image edits and manipulations",
"Environment": {
Expand Down Expand Up @@ -1621,7 +1621,7 @@ exports[`Serverless Image Handler Stack Snapshot 1`] = `
},
],
"SourceObjectKeys": [
"b17ee622b8fe21bd9a4189063340647fbb783d07261ea136b83db1d58255b0b2.zip",
"297994e5975cd83b2ec43f52720181be7d5daf08af8987cc4d5749f99db8f777.zip",
],
},
"Type": "Custom::CDKBucketDeployment",
Expand Down
59 changes: 54 additions & 5 deletions source/image-handler/test/image-handler/crop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ import { ImageEdits, StatusCodes } from "../../lib";
const s3Client = new S3();
const rekognitionClient = new Rekognition();

// base64 encoded images
const image_png_1x1 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
const image_png_white_5x5 =
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFAQAAAAClFBtIAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAd2KE6QAAAAHdElNRQfnAxYODhUMhxdmAAAADElEQVQI12P4wQCFABhCBNn4i/hQAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTAzLTIyVDE0OjE0OjIxKzAwOjAwtK8ALAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyMy0wMy0yMlQxNDoxNDoyMSswMDowMMXyuJAAAAAASUVORK5CYII=";
const image_png_white_1x1 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVR4nGP4//8/AAX+Av4N70a4AAAAAElFTkSuQmCC";

describe("crop", () => {
it("Should pass if a cropping area value is out of bounds", async () => {
// Arrange
const originalImage = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
"base64"
);
const originalImage = Buffer.from(image_png_1x1, "base64");
const image = sharp(originalImage, { failOnError: false }).withMetadata();
const edits: ImageEdits = {
crop: { left: 0, right: 0, width: 100, height: 100 },
crop: { left: 0, top: 0, width: 100, height: 100 },
};

// Act
Expand All @@ -36,4 +41,48 @@ describe("crop", () => {
});
}
});

// confirm that crops perform as expected
it("Should pass with a standard crop", async () => {
// 5x5 png
const originalImage = Buffer.from(image_png_white_5x5, "base64");
const image = sharp(originalImage, { failOnError: false }).withMetadata();
const edits: ImageEdits = {
crop: { left: 0, top: 0, width: 1, height: 1 },
};

// crop an image and compare with the result expected
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
const result = await imageHandler.applyEdits(image, edits, false);
const resultBuffer = await result.toBuffer();
expect(resultBuffer).toEqual(Buffer.from(image_png_white_1x1, "base64"));
});

// confirm that an invalid attribute sharp crop request containing *right* rather than *top* returns as a cropping error,
// note that this only confirms the behavior of the image-handler in this case,
// it is not an accurate description of the actual error
it("Should fail with an invalid crop request", async () => {
// 5x5 png
const originalImage = Buffer.from(image_png_white_5x5, "base64");
const image = sharp(originalImage, { failOnError: false }).withMetadata();
const edits: ImageEdits = {
crop: { left: 0, right: 0, width: 1, height: 1 },
};

// crop an image and compare with the result expected
try {
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
const result = await imageHandler.applyEdits(image, edits, false);
const resultBuffer = await result.toBuffer();
expect(resultBuffer).toEqual(Buffer.from(image_png_white_1x1, "base64"));
} catch (error) {
// Assert
expect(error).toMatchObject({
status: StatusCodes.BAD_REQUEST,
code: "Crop::AreaOutOfBounds",
message:
"The cropping area you provided exceeds the boundaries of the original image. Please try choosing a correct cropping value.",
});
}
});
});

0 comments on commit 998a921

Please sign in to comment.