Skip to content

Commit d1b3ebd

Browse files
committed
test cases for url-generation
1 parent d67ea7c commit d1b3ebd

File tree

14 files changed

+441
-162
lines changed

14 files changed

+441
-162
lines changed

constants/errorMessages.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
"MANDATORY_INITIALIZATION_MISSING" : { message : "Missing publicKey or privateKey or urlEndpoint during ImageKit initialization", help : "" },
3+
"MANDATORY_PUBLIC_KEY_MISSING" : { message : "Missing publicKey during ImageKit initialization", help : "" },
4+
"MANDATORY_PRIVATE_KEY_MISSING" : { message : "Missing privateKey during ImageKit initialization", help : "" },
5+
"MANDATORY_URL_ENDPOINT_KEY_MISSING" : { message : "Missing urlEndpoint during ImageKit initialization", help : "" },
36
"INVALID_TRANSFORMATION_POSITION" : { message : "Invalid transformationPosition parameter", help : "" },
47
"CACHE_PURGE_URL_MISSING" : { message : "Missing URL parameter for this request", help : "" },
58
"CACHE_PURGE_STATUS_ID_MISSING" : { message : "Missing Request ID parameter for this request", help : "" },

constants/supportedTransforms.js

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
module.exports = {
2-
"height" : "h",
3-
"width" : "w",
4-
"aspectRatio" : "ar",
5-
"quality" : "q",
6-
"crop" : "c",
7-
"cropMode" : "cm",
8-
"x" : "x",
9-
"y" : "y",
10-
"focus" : "fo",
11-
"format" : "f",
12-
"radius" : "r",
13-
"background" : "bg",
14-
"border" : "bo",
15-
"rotation" : "rt",
16-
"blur" : "bl",
17-
"named" : "n",
18-
"overlayImage" : "oi",
19-
"overlayX" : "ox",
20-
"overlayY" : "oy",
21-
"overlayFocus" : "ofo",
22-
"overlayHeight" : "oh",
23-
"overlayWidth" : "ow",
24-
"overlayText" : "ot",
25-
"overlayTextFontSize" : "ots",
26-
"overlayTextFontFamily" : "otf",
27-
"overlayTextColor" : "otc",
28-
"overlayAlpha" : "oa",
29-
"overlayTextTypography" : "ott",
30-
"overlayBackground" : "obg",
31-
"overlayImageTrim" : "oit",
32-
"progressive" : "pr",
33-
"lossless" : "lo",
34-
"trim" : "t",
35-
"metadata" : "md",
36-
"colorProfile" : "cp",
37-
"defaultImage" : "di",
38-
"dpr" : "dpr",
39-
"effectSharpen" : "e-sharpen",
40-
"effectUSM" : "e-usm",
41-
"effectContrast" : "e-contrast",
42-
"effectGray" : "e-grayscale",
43-
"original" : "orig"
2+
"height": "h",
3+
"width": "w",
4+
"aspectRatio": "ar",
5+
"quality": "q",
6+
"crop": "c",
7+
"cropMode": "cm",
8+
"x": "x",
9+
"y": "y",
10+
"focus": "fo",
11+
"format": "f",
12+
"radius": "r",
13+
"background": "bg",
14+
"border": "b",
15+
"rotation": "rt",
16+
"rotate": "rt",
17+
"blur": "bl",
18+
"named": "n",
19+
"overlayImage": "oi",
20+
"overlayImageAspectRatio": "oiar",
21+
"overlayImageBackground": "oibg",
22+
"overlayImageBorder": "oib",
23+
"overlayImageDPR": "oidpr",
24+
"overlayImageQuality": "oiq",
25+
"overlayImageCropping": "oic",
26+
"overlayImageTrim": "oit",
27+
"overlayX": "ox",
28+
"overlayY": "oy",
29+
"overlayFocus": "ofo",
30+
"overlayHeight": "oh",
31+
"overlayWidth": "ow",
32+
"overlayText": "ot",
33+
"overlayTextFontSize": "ots",
34+
"overlayTextFontFamily": "otf",
35+
"overlayTextColor": "otc",
36+
"overlayTextTransparency": "oa",
37+
"overlayAlpha": "oa",
38+
"overlayTextTypography": "ott",
39+
"overlayBackground": "obg",
40+
"overlayTextEncoded": "ote",
41+
"overlayTextWidth": "otw",
42+
"overlayTextBackground": "otbg",
43+
"overlayTextPadding": "otp",
44+
"overlayTextInnerAlignment": "otia",
45+
"overlayRadius": "or",
46+
"progressive": "pr",
47+
"lossless": "lo",
48+
"trim": "t",
49+
"metadata": "md",
50+
"colorProfile": "cp",
51+
"defaultImage": "di",
52+
"dpr": "dpr",
53+
"effectSharpen": "e-sharpen",
54+
"effectUSM": "e-usm",
55+
"effectContrast": "e-contrast",
56+
"effectGray": "e-grayscale",
57+
"original": "orig"
4458
};

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ var ImageKit = function(opts) {
2828
};
2929

3030
this.options = _.extend(this.options, opts);
31-
if(!mandatoryParametersAvailable(this.options)) {
32-
throw new Error(errorMessages.MANDATORY_INITIALIZATION_MISSING.message);
31+
if(!this.options.publicKey) {
32+
throw new Error(errorMessages.MANDATORY_PUBLIC_KEY_MISSING.message);
33+
}
34+
if(!this.options.privateKey) {
35+
throw new Error(errorMessages.MANDATORY_PRIVATE_KEY_MISSING.message);
36+
}
37+
if(!this.options.urlEndpoint) {
38+
throw new Error(errorMessages.MANDATORY_URL_ENDPOINT_KEY_MISSING.message);
3339
}
3440

3541
const promisify = function(f) {

libs/url/builder.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ function constructTransformationString(transformation) {
130130
if(transformation[i][key] === "-") {
131131
parsedTransformStep.push(transformKey);
132132
} else {
133-
parsedTransformStep.push([transformKey, transformation[i][key]].join(transformationUtils.getTransformKeyValueDelimiter()));
133+
var value = transformation[i][key];
134+
if(transformKey === "oi") {
135+
value = removeLeadingSlash(value);
136+
value = value.replace(/\//g,"@@");
137+
}
138+
parsedTransformStep.push([transformKey, value].join(transformationUtils.getTransformKeyValueDelimiter()));
134139
}
135140

136141
}
@@ -140,6 +145,12 @@ function constructTransformationString(transformation) {
140145
return parsedTransforms.join(transformationUtils.getChainTransformDelimiter());
141146
}
142147

148+
function removeLeadingSlash(str) {
149+
if (typeof str == "string" && str[0] == "/") {
150+
str = str.slice(1);
151+
}
152+
return str;
153+
}
143154

144155
function getSignatureTimestamp(seconds) {
145156
if(!seconds) return DEFAULT_TIMESTAMP;

libs/url/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ module.exports = function(urlOpts, defaultOptions) {
2626
function validOptions(opts) {
2727
if(!opts.urlEndpoint) return false;
2828

29-
if(!transformationUtils.validParameters(opts)) return false;
30-
3129
return true;
3230
}

tests/data/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports.initializationParams = {
2+
publicKey: "test_public_key",
3+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
4+
privateKey: "test_private_key",
5+
authenticationEndpoint: "http://test/auth"
6+
}

tests/index.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/initialization.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const chai = require("chai");
2+
const expect = chai.expect;
3+
const initializationParams = require("./data").initializationParams
4+
const ImageKit = require(".."); // This will automatically pick main module (cjs bundle) as per package.json
5+
6+
7+
describe("Initialization checks", function () {
8+
var imagekit = new ImageKit(initializationParams);
9+
10+
it('should throw error', function () {
11+
try {
12+
new ImageKit({});
13+
} catch(err) {
14+
expect(err.message).to.be.equal('Missing publicKey during ImageKit initialization');
15+
}
16+
});
17+
18+
it('should throw error', function () {
19+
try {
20+
new ImageKit({
21+
publicKey: "test_public_key"
22+
});
23+
} catch(err) {
24+
expect(err.message).to.be.equal('Missing privateKey during ImageKit initialization');
25+
}
26+
});
27+
28+
it('should throw error', function () {
29+
try {
30+
new ImageKit({
31+
publicKey: "test_public_key",
32+
privateKey: "test_private_key"
33+
});
34+
} catch(err) {
35+
expect(err.message).to.be.equal('Missing urlEndpoint during ImageKit initialization');
36+
}
37+
});
38+
39+
it('should have options object', function () {
40+
expect(imagekit.options).to.be.an('object');
41+
});
42+
43+
it('should have correctly initialized options object.', function () {
44+
expect(imagekit.options).to.have.property('publicKey').to.be.equal(initializationParams.publicKey);
45+
expect(imagekit.options).to.have.property('urlEndpoint').to.be.equal(initializationParams.urlEndpoint);
46+
expect(imagekit.options).to.have.property('authenticationEndpoint').to.be.equal(initializationParams.authenticationEndpoint);
47+
});
48+
49+
it("should have callable functions 'url' and 'upload'", function () {
50+
expect(imagekit.url).to.exist.and.to.be.a('function');
51+
expect(imagekit.upload).to.exist.and.to.be.a('function');
52+
});
53+
});

tests/phash.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const chai = require('chai');
2+
const initializationParams = require("./data").initializationParams
3+
const ImageKit = require(".."); // This will automatically pick main module (cjs bundle) as per package.json
4+
var imagekit = new ImageKit(initializationParams);
5+
6+
// helpers
7+
const errors = require('./helpers/errors');
8+
const spies = require('./helpers/spies');
9+
10+
const { expect } = chai;
11+
const { pHashDistanceSpy } = spies;
12+
13+
const failureHelper = (expectedError, ...params) => {
14+
const { message, help } = expectedError;
15+
const { message: error } = imagekit.pHashDistance(...params);
16+
17+
expect(error).to.be.equal(`${message}: ${help}`);
18+
};
19+
20+
const successHelper = (distance, ...params) => {
21+
const result = imagekit.pHashDistance(...params);
22+
23+
expect(result).to.be.a('number');
24+
expect(result).to.equal(distance);
25+
expect(pHashDistanceSpy.calledOnceWithExactly(...params)).to.equal(true);
26+
};
27+
28+
const pHash = {
29+
invalidAlphabeticalString: 'INVALIDHEXSTRING',
30+
invalidCharacterString: 'a4a655~!!@94518b',
31+
invalidHexStringLength: '42',
32+
numeric: 2222222222222222,
33+
valid: 'f06830ca9f1e3e90',
34+
// sets
35+
dissimilar: [
36+
'a4a65595ac94518b',
37+
'7838873e791f8400',
38+
],
39+
similar: [
40+
'2d5ad3936d2e015b',
41+
'2d6ed293db36a4fb',
42+
],
43+
};
44+
45+
describe('Utils > pHash > Distance calculator', () => {
46+
beforeEach(() => {
47+
pHashDistanceSpy.resetHistory();
48+
});
49+
50+
after(() => {
51+
pHashDistanceSpy.resetHistory();
52+
});
53+
54+
context('Failure cases:', () => {
55+
it('Should return error for missing first pHash', () => {
56+
failureHelper(errors.MISSING_PHASH_VALUE, null, pHash.valid);
57+
});
58+
59+
it('Should return error for missing second pHash', () => {
60+
failureHelper(errors.MISSING_PHASH_VALUE, pHash.valid);
61+
});
62+
63+
it('Should return error for invalid first pHash', () => {
64+
failureHelper(errors.INVALID_PHASH_VALUE, pHash.invalidAlphabeticalString, pHash.valid);
65+
});
66+
67+
it('Should return error for invalid second pHash', () => {
68+
failureHelper(errors.INVALID_PHASH_VALUE, pHash.valid, pHash.invalidCharacterString);
69+
});
70+
71+
it('Should return error for unequal pHash lengths', () => {
72+
failureHelper(errors.UNEQUAL_STRING_LENGTH, pHash.valid, pHash.invalidHexStringLength);
73+
});
74+
});
75+
76+
context('Success cases:', () => {
77+
it('Should return zero distance between pHash for same image', () => {
78+
successHelper(0, pHash.valid, pHash.valid);
79+
});
80+
81+
it('Should return smaller distance between pHash for similar images', () => {
82+
successHelper(17, pHash.similar[0], pHash.similar[1]);
83+
});
84+
85+
it('Should return larger distance between pHash for dissimilar images', () => {
86+
successHelper(37, pHash.dissimilar[0], pHash.dissimilar[1]);
87+
});
88+
89+
it('Should return distance for non-string but valid hexanumeric pHash', () => {
90+
successHelper(30, pHash.valid, pHash.numeric);
91+
});
92+
});
93+
});

tests/sdk.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)