Skip to content

Commit

Permalink
removed unused code and improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
manu4543 committed Oct 13, 2020
1 parent 434b20d commit 42727a9
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: build and test
- name: Build and Test
run: |
npm install
npm run build
npm run test
npm run report-coverage
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: build and test
- name: Build and Test
run: |
npm install
npm run build
Expand All @@ -37,7 +37,7 @@ jobs:
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- name: npm publish
- name: NPM Publish
run: |
npm run build
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"scripts": {
"dev": "rollup -c -w",
"build": "rm -rf dist*;rollup -c",
"test": "export NODE_ENV=test && nyc ./node_modules/mocha/bin/mocha -r esm --exit -t 40000 test/*.js;ex=$? ; unset NODE_ENV ; exit $ex;",
"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;",
"startSampleApp": "yarn build && cd samples/sample-app/ && yarn install && node index.js",
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
},
Expand Down
66 changes: 0 additions & 66 deletions src/url/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,64 +42,6 @@ export const buildURL = (opts) => {
return urlObj.href;
}

export const buildURLs = (opts) => {
if (!opts.path && !opts.src) {
return "";
}

//Create correct query parameters
var parsedURL, isSrcParameterUsedForURL, parsedHost;
if (opts.path) {
parsedURL = new URL(pathJoin([opts.urlEndpoint, opts.path]));
parsedHost = new URL(opts.urlEndpoint);
} else {
parsedURL = new URL(opts.src);
isSrcParameterUsedForURL = true;
}

var queryParameters = new URLSearchParams(parsedURL.query || "");
if (opts.sdkVersion && opts.sdkVersion.trim() != "") {
queryParameters.append("ik-sdk-version", opts.sdkVersion.trim());
}
for (var i in opts.queryParameters) {
queryParameters.append(i, opts.queryParameters[i]);
}

//Initial URL Construction Object
var urlObject = { host: "", pathname: "", search: "" };
if (opts.path) {
urlObject.protocol = parsedHost.protocol;
urlObject.host = opts.urlEndpoint.replace(urlObject.protocol + "//", "");
} else if (opts.src) {
urlObject.host = [parsedURL.auth, parsedURL.auth ? "@" : "", parsedURL.host].join("");
urlObject.protocol = parsedURL.protocol;
}
urlObject.pathname = parsedURL.pathname;

//Create Transformation String
var transformationString = constructTransformationString(opts.transformation);
if (transformationString) {
//force that if src parameter is being used for URL construction then the transformation
//string should be added only as a query parameter
if (transformationUtils.addAsQueryParameter(opts) || isSrcParameterUsedForURL) {
queryParameters.append(TRANSFORMATION_PARAMETER, transformationString);
} else {
urlObject.pathname = pathJoin([
TRANSFORMATION_PARAMETER,
transformationUtils.getChainTransformDelimiter(),
transformationString,
urlObject.pathname
]);
}
}

urlObject.host = removeTrailingSlash(urlObject.host);
urlObject.pathname = addLeadingSlash(urlObject.pathname);
urlObject.search = queryParameters.toString();

return url.format(urlObject);
};

function constructTransformationString(transformation) {
if (!Array.isArray(transformation)) { return ""; }

Expand Down Expand Up @@ -130,14 +72,6 @@ function constructTransformationString(transformation) {
return parsedTransforms.join(transformationUtils.getChainTransformDelimiter());
}

function addLeadingSlash(str) {
if (typeof str == "string" && str[0] != "/") {
str = "/" + str;
}

return str;
}

function removeTrailingSlash(str) {
if (typeof str == "string" && str[str.length - 1] == "/") {
str = str.substring(0, str.length - 1);
Expand Down
10 changes: 0 additions & 10 deletions src/url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,5 @@ export const url = (urlOpts, defaultOptions) => {
...urlOpts
}

if (!validOptions(opts)) {
return "";
}

return buildURL(opts);
};

function validOptions(opts) {
if (!opts.urlEndpoint) return false;

return true;
}
11 changes: 11 additions & 0 deletions test/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ describe("Initialization checks", function () {
}
});

it('Pass private Key', function () {
try {
new ImageKit({
urlEndpoint: initializationParams.urlEndpoint,
privateKey: "should_not_pass"
});
} catch(err) {
expect(err.message).to.be.equal('privateKey should not be passed on the client side');
}
});

it('should have options object', function () {
expect(imagekit.options).to.be.an('object');
});
Expand Down
46 changes: 46 additions & 0 deletions test/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ function successSignature() {
]);
server.respond();
}

function nonSuccessErrorSignature() {
server.respondWith("GET", initializationParams.authenticationEndpoint,
[
403,
{ "Content-Type": "application/json" },
JSON.stringify({
error: "Not allowed"
})
]);
server.respond();
}

function successUploadResponse() {
server.respondWith("POST", "https://upload.imagekit.io/api/v1/files/upload",
[
Expand Down Expand Up @@ -103,6 +116,22 @@ describe("File upload", function () {
sinon.assert.calledWith(callback, { help: "", message: "Missing file parameter for upload" }, null);
});

it('Missing authEndpoint', function () {
const fileOptions = {
fileName: "test_file_name",
file: "test_file"
};

var callback = sinon.spy();

imagekit.upload(fileOptions, callback, {
authenticationEndpoint : ""
});

expect(server.requests.length).to.be.equal(0);
sinon.assert.calledWith(callback, { message: "Missing authentication endpoint for upload", help: "" }, null);
});

it('Auth endpoint network error handling', function () {
const fileOptions = {
fileName: "test_file_name",
Expand All @@ -122,6 +151,23 @@ describe("File upload", function () {
sinon.assert.calledWith(callback, { message: "Request to authenticationEndpoint failed due to network error", help: "" }, null);
});

it('Auth endpoint non 200 status code handling', function () {
const fileOptions = {
fileName: "test_file_name",
file: "test_file"
};

var callback = sinon.spy();

imagekit.upload(fileOptions, callback);

expect(server.requests.length).to.be.equal(1);

// Simulate non 200 response on authentication endpoint
nonSuccessErrorSignature();
sinon.assert.calledWith(callback, { error: "Not allowed" }, null);
});

it('Upload endpoint network error handling', function () {
const fileOptions = {
fileName: "test_file_name",
Expand Down
6 changes: 6 additions & 0 deletions test/url-generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ describe("URL generation", function () {

var imagekit = new ImageKit(initializationParams);

it('no path no src', function () {
const url = imagekit.url({});

expect(url).equal("");
});

it('no transformation path', function () {
const url = imagekit.url({
path: "/test_path.jpg"
Expand Down

0 comments on commit 42727a9

Please sign in to comment.