Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore (unit tests): add unit tests for crop #459

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "b185a6f6cf7251b6ebb161e931ff427a5dbde900aad6846c487ead475e19469c.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
57 changes: 52 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,19 @@ import { ImageEdits, StatusCodes } from "../../lib";
const s3Client = new S3();
dougtoppin marked this conversation as resolved.
Show resolved Hide resolved
const rekognitionClient = new Rekognition();

// base64 encoded images
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_white_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 +39,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({
dougtoppin marked this conversation as resolved.
Show resolved Hide resolved
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.",
});
}
});
});