@@ -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 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" ;
15
+ const image_png_white_5x5 = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFAQAAAAClFBtIAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAd2KE6QAAAAHdElNRQfnAxYODhUMhxdmAAAADElEQVQI12P4wQCFABhCBNn4i/hQAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTAzLTIyVDE0OjE0OjIxKzAwOjAwtK8ALAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyMy0wMy0yMlQxNDoxNDoyMSswMDowMMXyuJAAAAAASUVORK5CYII=" ;
16
+ const image_png_white_1x1 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVR4nGP4//8/AAX+Av4N70a4AAAAAElFTkSuQmCC" ;
17
+
13
18
describe ( "crop" , ( ) => {
14
19
it ( "Should pass if a cropping area value is out of bounds" , async ( ) => {
15
20
// Arrange
16
21
const originalImage = Buffer . from (
17
- "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" ,
22
+ image_png_1x1 ,
18
23
"base64"
19
24
) ;
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,69 @@ 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 (
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
+ } ) ;
39
109
} ) ;
0 commit comments