@@ -10,16 +10,21 @@ import { ImageEdits, StatusCodes } from "../../lib";
10
10
const s3Client = new S3 ( ) ;
11
11
const rekognitionClient = new Rekognition ( ) ;
12
12
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
+
13
21
describe ( "crop" , ( ) => {
14
22
it ( "Should pass if a cropping area value is out of bounds" , async ( ) => {
15
23
// Arrange
16
- const originalImage = Buffer . from (
17
- "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" ,
18
- "base64"
19
- ) ;
24
+ const originalImage = Buffer . from ( image_png_1x1 , "base64" ) ;
20
25
const image = sharp ( originalImage , { failOnError : false } ) . withMetadata ( ) ;
21
26
const edits : ImageEdits = {
22
- crop : { left : 0 , right : 0 , width : 100 , height : 100 } ,
27
+ crop : { left : 0 , top : 0 , width : 100 , height : 100 } ,
23
28
} ;
24
29
25
30
// Act
@@ -36,4 +41,53 @@ describe("crop", () => {
36
41
} ) ;
37
42
}
38
43
} ) ;
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
+ } ) ;
39
93
} ) ;
0 commit comments