Skip to content

Commit

Permalink
Add unit test for crop
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Toppin committed Mar 22, 2023
1 parent 442a3ce commit d7a1129
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 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==",
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,69 @@ 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
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) {
console.log(error);
throw (error);
}
});

// 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 d7a1129

Please sign in to comment.