Skip to content

Commit 4a69f20

Browse files
committed
upload test cases and code coverage
1 parent da2c717 commit 4a69f20

File tree

12 files changed

+1158
-51
lines changed

12 files changed

+1158
-51
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ jobs:
1717
uses: actions/setup-node@v1
1818
with:
1919
node-version: ${{ matrix.node-version }}
20-
- name: npm install, build, and test
20+
- name: Test and report coverage
2121
run: |
2222
npm i -g yarn
2323
yarn install
2424
yarn test
25+
yarn report-coverage
2526
env:
2627
CI: true

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules/*
2-
.vscode
2+
.vscode
3+
.nyc_output
4+
coverage.lcov
5+
coverage

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
[![Node CI](https://github.com/imagekit-developer/imagekit-nodejs/workflows/Node%20CI/badge.svg)](https://github.com/imagekit-developer/imagekit-nodejs/)
55
[![npm version](https://img.shields.io/npm/v/imagekit)](https://www.npmjs.com/package/imagekit)
6+
[![codecov](https://codecov.io/gh/imagekit-developer/imagekit-nodejs/branch/master/graph/badge.svg)](https://codecov.io/gh/imagekit-developer/imagekit-nodejs)
67
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
78
[![Twitter Follow](https://img.shields.io/twitter/follow/imagekitio?label=Follow&style=social)](https://twitter.com/ImagekitIo)
89

libs/upload/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ module.exports = function(uploadOptions, defaultOptions, callback) {
5252

5353
request(requestOptions, defaultOptions, function(err, response, body) {
5454
if(err) {
55-
respond(true, err, callback);
55+
if(err instanceof Error && err.message) {
56+
respond(true, err.message, callback);
57+
} else {
58+
respond(true, err, callback);
59+
}
5660
return;
5761
}
5862

libs/url/builder.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports.buildURL = function(opts) {
9797

9898
var intermediateURL = url.format(urlObject);
9999

100-
var urlSignature = getSignature({
100+
var urlSignature = module.exports.getSignature({
101101
privateKey : opts.privateKey,
102102
url : intermediateURL,
103103
urlEndpoint : opts.urlEndpoint,
@@ -131,8 +131,8 @@ function constructTransformationString(transformation) {
131131
parsedTransformStep.push(transformKey);
132132
} else {
133133
var value = transformation[i][key];
134-
if(transformKey === "oi") {
135-
value = removeLeadingSlash(value);
134+
if(transformKey === "oi" || transformKey === "di") {
135+
value = removeTrailingSlash(removeLeadingSlash(value));
136136
value = value.replace(/\//g,"@@");
137137
}
138138
parsedTransformStep.push([transformKey, value].join(transformationUtils.getTransformKeyValueDelimiter()));
@@ -145,6 +145,13 @@ function constructTransformationString(transformation) {
145145
return parsedTransforms.join(transformationUtils.getChainTransformDelimiter());
146146
}
147147

148+
function removeTrailingSlash(str) {
149+
if (typeof str == "string" && str[str.length - 1] == "/") {
150+
str = str.substring(0, str.length - 1);
151+
}
152+
return str;
153+
}
154+
148155
function removeLeadingSlash(str) {
149156
if (typeof str == "string" && str[0] == "/") {
150157
str = str.slice(1);
@@ -162,7 +169,7 @@ function getSignatureTimestamp(seconds) {
162169
return currentTimestamp + sec;
163170
}
164171

165-
function getSignature(opts) {
172+
module.exports.getSignature = function(opts) {
166173
if(!opts.privateKey || !opts.url || !opts.urlEndpoint) return "";
167174
var stringToSign = opts.url.replace(urlFormatter.addTrailingSlash(opts.urlEndpoint), "") + opts.expiryTimestamp;
168175
return crypto.createHmac('sha1', opts.privateKey).update(stringToSign).digest('hex');

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"lint": "./node_modules/eslint/bin/eslint.js tests/ utils/phash.js",
88
"lint:fix": "./node_modules/eslint/bin/eslint.js --fix tests/ utils/phash.js",
9-
"test": "export NODE_ENV=test; ./node_modules/mocha/bin/mocha --exit -t 40000 tests/*.js;ex=$?;unset NODE_ENV; exit $ex;"
9+
"test": "export NODE_ENV=test; nyc ./node_modules/mocha/bin/mocha --coverage --exit -t 40000 tests/*.js;ex=$?;unset NODE_ENV; exit $ex;",
10+
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
1011
},
1112
"author": "ImageKit Developer <[email protected]>",
1213
"homepage": "https://imagekit.io",
@@ -22,10 +23,13 @@
2223
},
2324
"devDependencies": {
2425
"chai": "^4.2.0",
26+
"codecov": "^3.8.0",
2527
"eslint": "^7.7.0",
2628
"eslint-config-airbnb-base": "^14.2.0",
2729
"eslint-plugin-import": "^2.22.0",
2830
"mocha": "^8.1.1",
29-
"sinon": "^9.0.3"
31+
"nock": "^13.0.4",
32+
"nyc": "^15.1.0",
33+
"sinon": "^9.2.0"
3034
}
3135
}

tests/authParams.js

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

tests/unit.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const chai = require("chai");
2+
const expect = chai.expect;
3+
const ImageKit = require(".."); // This will automatically pick main module (cjs bundle) as per package.json
4+
5+
const urlBuilder = require("../libs/url/builder");
6+
7+
8+
describe("Unit test cases", function () {
9+
var imagekit = new ImageKit({
10+
publicKey: "public_key_test",
11+
privateKey: "private_key_test",
12+
urlEndpoint: "https://test-domain.com/test-endpoint"
13+
});
14+
15+
it('Authentication params check', function () {
16+
var authenticationParameters = imagekit.getAuthenticationParameters("your_token", 1582269249);
17+
expect(authenticationParameters).to.deep.equal({
18+
token: 'your_token',
19+
expire: 1582269249,
20+
signature: 'e71bcd6031016b060d349d212e23e85c791decdd'
21+
})
22+
});
23+
24+
it('Signed URL signature without slash default expiry', function () {
25+
var url = "https://test-domain.com/test-endpoint/tr:w-100/test-signed-url.png";
26+
var signature = urlBuilder.getSignature({
27+
privateKey: "private_key_test",
28+
url: url,
29+
urlEndpoint:"https://test-domain.com/test-endpoint",
30+
expiryTimestamp: "9999999999"
31+
})
32+
expect(signature).to.be.equal("41b3075c40bc84147eb71b8b49ae7fbf349d0f00")
33+
});
34+
35+
it('Signed URL signature with slash default expiry', function () {
36+
var url = "https://test-domain.com/test-endpoint/tr:w-100/test-signed-url.png";
37+
var signature = urlBuilder.getSignature({
38+
privateKey: "private_key_test",
39+
url: url,
40+
urlEndpoint:"https://test-domain.com/test-endpoint/",
41+
expiryTimestamp: "9999999999"
42+
})
43+
expect(signature).to.be.equal("41b3075c40bc84147eb71b8b49ae7fbf349d0f00")
44+
});
45+
46+
it('pHash distance different', function () {
47+
var pHashDistance = imagekit.pHashDistance("33699c96619cc69e","968e978414fe04ea");
48+
expect(pHashDistance).to.be.equal(30)
49+
});
50+
51+
it('pHash distance similar', function () {
52+
var pHashDistance = imagekit.pHashDistance("63433b3ccf8e1ebe","f5d2226cd9d32b16");
53+
expect(pHashDistance).to.be.equal(27)
54+
});
55+
56+
it('pHash distance similar reverse', function () {
57+
var pHashDistance = imagekit.pHashDistance("f5d2226cd9d32b16","63433b3ccf8e1ebe");
58+
expect(pHashDistance).to.be.equal(27)
59+
});
60+
61+
it('pHash distance same', function () {
62+
var pHashDistance = imagekit.pHashDistance("33699c96619cc69e","33699c96619cc69e");
63+
expect(pHashDistance).to.be.equal(0)
64+
});
65+
});

0 commit comments

Comments
 (0)