Skip to content

Commit 42727a9

Browse files
committed
removed unused code and improved test coverage
1 parent 434b20d commit 42727a9

File tree

8 files changed

+68
-80
lines changed

8 files changed

+68
-80
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ jobs:
1717
uses: actions/setup-node@v1
1818
with:
1919
node-version: ${{ matrix.node-version }}
20-
- name: build and test
20+
- name: Build and Test
2121
run: |
2222
npm install
23+
npm run build
2324
npm run test
2425
npm run report-coverage
2526
env:

.github/workflows/npmpublish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: actions/setup-node@v1
2121
with:
2222
node-version: ${{ matrix.node-version }}
23-
- name: build and test
23+
- name: Build and Test
2424
run: |
2525
npm install
2626
npm run build
@@ -37,7 +37,7 @@ jobs:
3737
with:
3838
node-version: 12
3939
registry-url: https://registry.npmjs.org/
40-
- name: npm publish
40+
- name: NPM Publish
4141
run: |
4242
npm run build
4343
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"scripts": {
3232
"dev": "rollup -c -w",
3333
"build": "rm -rf dist*;rollup -c",
34-
"test": "export NODE_ENV=test && nyc ./node_modules/mocha/bin/mocha -r esm --exit -t 40000 test/*.js;ex=$? ; unset NODE_ENV ; exit $ex;",
34+
"test": "export NODE_ENV=test && nyc ./node_modules/mocha/bin/mocha --coverage -r esm --exit -t 40000 test/*.js;ex=$? ; unset NODE_ENV ; exit $ex;",
3535
"startSampleApp": "yarn build && cd samples/sample-app/ && yarn install && node index.js",
3636
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
3737
},

src/url/builder.js

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,6 @@ export const buildURL = (opts) => {
4242
return urlObj.href;
4343
}
4444

45-
export const buildURLs = (opts) => {
46-
if (!opts.path && !opts.src) {
47-
return "";
48-
}
49-
50-
//Create correct query parameters
51-
var parsedURL, isSrcParameterUsedForURL, parsedHost;
52-
if (opts.path) {
53-
parsedURL = new URL(pathJoin([opts.urlEndpoint, opts.path]));
54-
parsedHost = new URL(opts.urlEndpoint);
55-
} else {
56-
parsedURL = new URL(opts.src);
57-
isSrcParameterUsedForURL = true;
58-
}
59-
60-
var queryParameters = new URLSearchParams(parsedURL.query || "");
61-
if (opts.sdkVersion && opts.sdkVersion.trim() != "") {
62-
queryParameters.append("ik-sdk-version", opts.sdkVersion.trim());
63-
}
64-
for (var i in opts.queryParameters) {
65-
queryParameters.append(i, opts.queryParameters[i]);
66-
}
67-
68-
//Initial URL Construction Object
69-
var urlObject = { host: "", pathname: "", search: "" };
70-
if (opts.path) {
71-
urlObject.protocol = parsedHost.protocol;
72-
urlObject.host = opts.urlEndpoint.replace(urlObject.protocol + "//", "");
73-
} else if (opts.src) {
74-
urlObject.host = [parsedURL.auth, parsedURL.auth ? "@" : "", parsedURL.host].join("");
75-
urlObject.protocol = parsedURL.protocol;
76-
}
77-
urlObject.pathname = parsedURL.pathname;
78-
79-
//Create Transformation String
80-
var transformationString = constructTransformationString(opts.transformation);
81-
if (transformationString) {
82-
//force that if src parameter is being used for URL construction then the transformation
83-
//string should be added only as a query parameter
84-
if (transformationUtils.addAsQueryParameter(opts) || isSrcParameterUsedForURL) {
85-
queryParameters.append(TRANSFORMATION_PARAMETER, transformationString);
86-
} else {
87-
urlObject.pathname = pathJoin([
88-
TRANSFORMATION_PARAMETER,
89-
transformationUtils.getChainTransformDelimiter(),
90-
transformationString,
91-
urlObject.pathname
92-
]);
93-
}
94-
}
95-
96-
urlObject.host = removeTrailingSlash(urlObject.host);
97-
urlObject.pathname = addLeadingSlash(urlObject.pathname);
98-
urlObject.search = queryParameters.toString();
99-
100-
return url.format(urlObject);
101-
};
102-
10345
function constructTransformationString(transformation) {
10446
if (!Array.isArray(transformation)) { return ""; }
10547

@@ -130,14 +72,6 @@ function constructTransformationString(transformation) {
13072
return parsedTransforms.join(transformationUtils.getChainTransformDelimiter());
13173
}
13274

133-
function addLeadingSlash(str) {
134-
if (typeof str == "string" && str[0] != "/") {
135-
str = "/" + str;
136-
}
137-
138-
return str;
139-
}
140-
14175
function removeTrailingSlash(str) {
14276
if (typeof str == "string" && str[str.length - 1] == "/") {
14377
str = str.substring(0, str.length - 1);

src/url/index.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,5 @@ export const url = (urlOpts, defaultOptions) => {
1414
...urlOpts
1515
}
1616

17-
if (!validOptions(opts)) {
18-
return "";
19-
}
20-
2117
return buildURL(opts);
2218
};
23-
24-
function validOptions(opts) {
25-
if (!opts.urlEndpoint) return false;
26-
27-
return true;
28-
}

test/initialization.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ describe("Initialization checks", function () {
1515
}
1616
});
1717

18+
it('Pass private Key', function () {
19+
try {
20+
new ImageKit({
21+
urlEndpoint: initializationParams.urlEndpoint,
22+
privateKey: "should_not_pass"
23+
});
24+
} catch(err) {
25+
expect(err.message).to.be.equal('privateKey should not be passed on the client side');
26+
}
27+
});
28+
1829
it('should have options object', function () {
1930
expect(imagekit.options).to.be.an('object');
2031
});

test/upload.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ function successSignature() {
3434
]);
3535
server.respond();
3636
}
37+
38+
function nonSuccessErrorSignature() {
39+
server.respondWith("GET", initializationParams.authenticationEndpoint,
40+
[
41+
403,
42+
{ "Content-Type": "application/json" },
43+
JSON.stringify({
44+
error: "Not allowed"
45+
})
46+
]);
47+
server.respond();
48+
}
49+
3750
function successUploadResponse() {
3851
server.respondWith("POST", "https://upload.imagekit.io/api/v1/files/upload",
3952
[
@@ -103,6 +116,22 @@ describe("File upload", function () {
103116
sinon.assert.calledWith(callback, { help: "", message: "Missing file parameter for upload" }, null);
104117
});
105118

119+
it('Missing authEndpoint', function () {
120+
const fileOptions = {
121+
fileName: "test_file_name",
122+
file: "test_file"
123+
};
124+
125+
var callback = sinon.spy();
126+
127+
imagekit.upload(fileOptions, callback, {
128+
authenticationEndpoint : ""
129+
});
130+
131+
expect(server.requests.length).to.be.equal(0);
132+
sinon.assert.calledWith(callback, { message: "Missing authentication endpoint for upload", help: "" }, null);
133+
});
134+
106135
it('Auth endpoint network error handling', function () {
107136
const fileOptions = {
108137
fileName: "test_file_name",
@@ -122,6 +151,23 @@ describe("File upload", function () {
122151
sinon.assert.calledWith(callback, { message: "Request to authenticationEndpoint failed due to network error", help: "" }, null);
123152
});
124153

154+
it('Auth endpoint non 200 status code handling', function () {
155+
const fileOptions = {
156+
fileName: "test_file_name",
157+
file: "test_file"
158+
};
159+
160+
var callback = sinon.spy();
161+
162+
imagekit.upload(fileOptions, callback);
163+
164+
expect(server.requests.length).to.be.equal(1);
165+
166+
// Simulate non 200 response on authentication endpoint
167+
nonSuccessErrorSignature();
168+
sinon.assert.calledWith(callback, { error: "Not allowed" }, null);
169+
});
170+
125171
it('Upload endpoint network error handling', function () {
126172
const fileOptions = {
127173
fileName: "test_file_name",

test/url-generation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ describe("URL generation", function () {
99

1010
var imagekit = new ImageKit(initializationParams);
1111

12+
it('no path no src', function () {
13+
const url = imagekit.url({});
14+
15+
expect(url).equal("");
16+
});
17+
1218
it('no transformation path', function () {
1319
const url = imagekit.url({
1420
path: "/test_path.jpg"

0 commit comments

Comments
 (0)