Skip to content

Commit d7a1129

Browse files
author
Doug Toppin
committed
Add unit test for crop
1 parent 442a3ce commit d7a1129

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

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

+72-2
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 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
15+
const image_png_white_5x5 = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFAQAAAAClFBtIAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAd2KE6QAAAAHdElNRQfnAxYODhUMhxdmAAAADElEQVQI12P4wQCFABhCBNn4i/hQAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTAzLTIyVDE0OjE0OjIxKzAwOjAwtK8ALAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyMy0wMy0yMlQxNDoxNDoyMSswMDowMMXyuJAAAAAASUVORK5CYII=";
16+
const image_png_white_1x1 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVR4nGP4//8/AAX+Av4N70a4AAAAAElFTkSuQmCC";
17+
1318
describe("crop", () => {
1419
it("Should pass if a cropping area value is out of bounds", async () => {
1520
// Arrange
1621
const originalImage = Buffer.from(
17-
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
22+
image_png_1x1,
1823
"base64"
1924
);
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,69 @@ 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(
49+
image_png_white_5x5,
50+
"base64"
51+
);
52+
const image = sharp(originalImage, { failOnError: false }).withMetadata();
53+
const edits: ImageEdits = {
54+
crop: { left: 0, top: 0, width: 1, height: 1 },
55+
};
56+
57+
// crop an image and compare with the result expected
58+
try {
59+
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
60+
const result = await imageHandler.applyEdits(image, edits, false);
61+
const resultBuffer = await result.toBuffer();
62+
expect(resultBuffer).toEqual(
63+
Buffer.from(
64+
image_png_white_1x1,
65+
"base64"
66+
)
67+
);
68+
} catch (error) {
69+
console.log(error);
70+
throw (error);
71+
}
72+
});
73+
74+
// confirm that an invalid attribute sharp crop request containing *right* rather than *top* returns as a cropping error,
75+
// note that this only confirms the behavior of the image-handler in this case,
76+
// it is not an accurate description of the actual error
77+
it("Should fail with an invalid crop request", async () => {
78+
// 5x5 png
79+
const originalImage = Buffer.from(
80+
image_png_white_5x5,
81+
"base64"
82+
);
83+
const image = sharp(originalImage, { failOnError: false }).withMetadata();
84+
const edits: ImageEdits = {
85+
crop: { left: 0, right: 0, width: 1, height: 1 },
86+
};
87+
88+
// crop an image and compare with the result expected
89+
try {
90+
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
91+
const result = await imageHandler.applyEdits(image, edits, false);
92+
const resultBuffer = await result.toBuffer();
93+
expect(resultBuffer).toEqual(
94+
Buffer.from(
95+
image_png_white_1x1,
96+
"base64"
97+
)
98+
);
99+
} catch (error) {
100+
// Assert
101+
expect(error).toMatchObject({
102+
status: StatusCodes.BAD_REQUEST,
103+
code: "Crop::AreaOutOfBounds",
104+
message:
105+
"The cropping area you provided exceeds the boundaries of the original image. Please try choosing a correct cropping value.",
106+
});
107+
}
108+
});
39109
});

0 commit comments

Comments
 (0)