From 1b4e6fa6ab4f28c0b5670bcf3a5d7e7e7db6e0b2 Mon Sep 17 00:00:00 2001 From: Rafael Dohms Date: Mon, 3 Apr 2023 14:16:25 +0200 Subject: [PATCH] fix(package): Update code to new eslint rules --- lib/signing.js | 22 ++++++++++----------- test/actions.spec.js | 37 ++++++++++++++++++----------------- test/signing.spec.js | 46 +++++++++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 50 deletions(-) diff --git a/lib/signing.js b/lib/signing.js index 86ebbda..dec5d7f 100644 --- a/lib/signing.js +++ b/lib/signing.js @@ -35,8 +35,8 @@ class SignatureFactory { handleQuery(query) { // mandatory // Transform URL based on query - if (query.id && query.id != '') { - if (query.id == '*') { + if (query.id && query.id !== '') { + if (query.id === '*') { query.id = '%2A'; } this.url = this.url.replace(':id', query.id); @@ -47,7 +47,7 @@ class SignatureFactory { let params = []; for (let k in query.params) { - if (query.params.hasOwnProperty(k)) { + if (Object.prototype.hasOwnProperty.call(query.params, k)) { params.push(k); } } @@ -113,7 +113,7 @@ class SignatureFactory { this.queryParameters, this.getCanonicalHeaders(), this.getSignedHeaders(), - SignatureFactory.hash('', 'hex') + SignatureFactory.hash('', 'hex'), ].join('\n'); } @@ -128,7 +128,7 @@ class SignatureFactory { 'USBL1-HMAC-SHA256', this.dates.longdate, this.dates.shortdate + '/' + 'usbl1_request', - SignatureFactory.hash(this.canonicalString(), 'hex') + SignatureFactory.hash(this.canonicalString(), 'hex'), ].join('\n'); } @@ -147,11 +147,9 @@ class SignatureFactory { this.headers['x-usbl-date'] = this.dates.longdate; return [ - `USBL1-HMAC-SHA256 Credential=${this.accessKey}/${ - this.dates.shortdate - }/usbl1_request`, + `USBL1-HMAC-SHA256 Credential=${this.accessKey}/${this.dates.shortdate}/usbl1_request`, `SignedHeaders=${this.getSignedHeaders()}`, - `Signature=${this.getSignature()}` + `Signature=${this.getSignature()}`, ].join(', '); } @@ -162,7 +160,7 @@ class SignatureFactory { headers: this.headers, url: this.queryParameters ? `${this.url}?${this.queryParameters}` - : this.url + : this.url, }; } @@ -171,7 +169,7 @@ class SignatureFactory { return { shortdate: date.substr(0, 8), - longdate: `${date.substr(0, 15)}Z` + longdate: `${date.substr(0, 15)}Z`, }; } @@ -203,5 +201,5 @@ function sign(accessKey, privateKey, path, headers, options) { module.exports = { sign, - SignatureFactory + SignatureFactory, }; diff --git a/test/actions.spec.js b/test/actions.spec.js index db19045..4d56379 100644 --- a/test/actions.spec.js +++ b/test/actions.spec.js @@ -1,3 +1,4 @@ +/* eslint-disable jest/no-conditional-expect,jest/no-done-callback */ const axios = require('axios'); const action = require('../lib/action'); @@ -26,24 +27,24 @@ describe('action', () => { Authorization: 'USBL1-HMAC-SHA256 Credential=access-key/20160101/usbl1_request, SignedHeaders=host;x-usbl-date, Signature=bdbf025412de09a0331ee3810533f7c69146413745da0412a8d2a71a7d2ba0ce', host: 'data.usabilla.com', - 'x-usbl-date': '20160101T000000Z' + 'x-usbl-date': '20160101T000000Z', }, method: 'get', - url: '/foo' + url: '/foo', }; }); - it('calls request with options and returns items', done => { + it('calls request with options and returns items', (done) => { axios.mockImplementation(() => Promise.resolve({ data: { - items - } + items, + }, }) ); action(apiOptions, endpointOptions, 'access-key', 'private-key', options) - .then(result => { + .then((result) => { expect(result).toEqual(items); expect(axios).toHaveBeenCalledWith(expectedRequestOptions); done(); @@ -51,7 +52,7 @@ describe('action', () => { .catch(done.fail); }); - it('calls request with options and returns items with iteration', done => { + it('calls request with options and returns items with iteration', (done) => { const moreItems = [{ id: '3' }, { id: '4' }]; apiOptions = Object.assign({}, apiOptions, { iterator: true }); axios @@ -60,21 +61,21 @@ describe('action', () => { data: { items, hasMore: true, - lastTimestamp: new Date('2017') - } + lastTimestamp: new Date('2017'), + }, }) ) .mockImplementationOnce(() => Promise.resolve({ data: { items: moreItems, - hasMore: false - } + hasMore: false, + }, }) ); action(apiOptions, endpointOptions, 'access-key', 'private-key') - .then(result => { + .then((result) => { expect(result).toEqual([...items, ...moreItems]); expect(axios).toHaveBeenCalledWith(expectedRequestOptions); done(); @@ -82,21 +83,21 @@ describe('action', () => { .catch(done.fail); }); - it('throws a client error when request fails', done => { + it('throws a client error when request fails', (done) => { axios.mockImplementationOnce(() => Promise.reject({ response: { data: { error: { - message: 'foo' - } - } - } + message: 'foo', + }, + }, + }, }) ); action(apiOptions, endpointOptions, 'access-key', 'private-key', options) .then(done.fail) - .catch(error => { + .catch((error) => { expect(error.message).toBe('foo'); done(); }); diff --git a/test/signing.spec.js b/test/signing.spec.js index 2f34686..55bada1 100644 --- a/test/signing.spec.js +++ b/test/signing.spec.js @@ -30,7 +30,7 @@ describe('SignatureFactory', () => { expect(signatureFactory.headers).toEqual({ fooA: 'barA', - fooB: 'barB' + fooB: 'barB', }); }); }); @@ -39,7 +39,7 @@ describe('SignatureFactory', () => { it('should transform URL based on query with id', () => { signatureFactory.url = 'bar/:id/bar'; let query = { - id: 'foo' + id: 'foo', }; signatureFactory.handleQuery(query); @@ -59,7 +59,7 @@ describe('SignatureFactory', () => { it('should transform URL based on query with star id', () => { signatureFactory.url = 'bar/:id/bar'; let query = { - id: '*' + id: '*', }; signatureFactory.handleQuery(query); @@ -72,8 +72,8 @@ describe('SignatureFactory', () => { let query = { params: { limit: 'foo', - since: 'bar' - } + since: 'bar', + }, }; signatureFactory.handleQuery(query); @@ -88,25 +88,29 @@ describe('SignatureFactory', () => { beforeEach(() => { signatureFactory.headers = { fooB: 'barB', - fooA: 'barA' + fooA: 'barA', }; headers = signatureFactory.getHeadersToSign(); }); it('should add add host header', () => { - expect(headers.hasOwnProperty('host')).toBeTruthy(); + expect( + Object.prototype.hasOwnProperty.call(headers, 'host') + ).toBeTruthy(); }); it('should delete possible cached Authorization header', () => { - expect(headers.hasOwnProperty('Authorization')).toBeFalsy(); + expect( + Object.prototype.hasOwnProperty.call(headers, 'Authorization') + ).toBeFalsy(); }); it('should sort headers alphabetically', () => { let expected = { fooA: 'barA', fooB: 'barB', - host: 'data.usabilla.com' + host: 'data.usabilla.com', }; expect(headers).toEqual(expected); }); @@ -117,7 +121,7 @@ describe('SignatureFactory', () => { //init signatureFactory.headers = { fooB: 'barB', - fooA: 'barA' + fooA: 'barA', }; let headers = signatureFactory.getCanonicalHeaders(); @@ -131,7 +135,7 @@ describe('SignatureFactory', () => { //init signatureFactory.headers = { fooB: 'barB', - fooA: 'barA' + fooA: 'barA', }; let headers = signatureFactory.getSignedHeaders(); @@ -153,7 +157,7 @@ describe('SignatureFactory', () => { '', 'host:data.usabilla.com\n', 'host', - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', ].join('\n') ); }); @@ -163,8 +167,12 @@ describe('SignatureFactory', () => { it('returns an object with "shortdate" and "longdate"', () => { let time = SignatureFactory.getDateTime(); - expect(time.hasOwnProperty('shortdate')).toBeTruthy(); - expect(time.hasOwnProperty('longdate')).toBeTruthy(); + expect( + Object.prototype.hasOwnProperty.call(time, 'shortdate') + ).toBeTruthy(); + expect( + Object.prototype.hasOwnProperty.call(time, 'longdate') + ).toBeTruthy(); }); }); @@ -178,7 +186,7 @@ describe('SignatureFactory', () => { 'USBL1-HMAC-SHA256', signatureFactory.dates.longdate, `${signatureFactory.dates.shortdate}/usbl1_request`, - 'foo' + 'foo', ].join('\n') ); }); @@ -211,18 +219,16 @@ describe('SignatureFactory', () => { it('returns the authorization header string', () => { SignatureFactory.getDateTime = jest.fn().mockReturnValue({ longdate: 'foo', - shortdate: 'bar' + shortdate: 'bar', }); signatureFactory.getSignedHeaders = jest.fn().mockReturnValue('baz'); signatureFactory.getSignature = jest.fn().mockReturnValue('bax'); const authHeader = signatureFactory.authHeader(); expect(authHeader).toBe( [ - `USBL1-HMAC-SHA256 Credential=${signatureFactory.accessKey}/${ - signatureFactory.dates.shortdate - }/usbl1_request`, + `USBL1-HMAC-SHA256 Credential=${signatureFactory.accessKey}/${signatureFactory.dates.shortdate}/usbl1_request`, 'SignedHeaders=baz', - 'Signature=bax' + 'Signature=bax', ].join(', ') ); });