Skip to content

Commit

Permalink
fix(package): Update code to new eslint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rdohms committed Apr 3, 2023
1 parent 2fea7c0 commit 1b4e6fa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
22 changes: 10 additions & 12 deletions lib/signing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ class SignatureFactory {
this.queryParameters,
this.getCanonicalHeaders(),
this.getSignedHeaders(),
SignatureFactory.hash('', 'hex')
SignatureFactory.hash('', 'hex'),
].join('\n');
}

Expand All @@ -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');
}

Expand All @@ -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(', ');
}

Expand All @@ -162,7 +160,7 @@ class SignatureFactory {
headers: this.headers,
url: this.queryParameters
? `${this.url}?${this.queryParameters}`
: this.url
: this.url,
};
}

Expand All @@ -171,7 +169,7 @@ class SignatureFactory {

return {
shortdate: date.substr(0, 8),
longdate: `${date.substr(0, 15)}Z`
longdate: `${date.substr(0, 15)}Z`,
};
}

Expand Down Expand Up @@ -203,5 +201,5 @@ function sign(accessKey, privateKey, path, headers, options) {

module.exports = {
sign,
SignatureFactory
SignatureFactory,
};
37 changes: 19 additions & 18 deletions test/actions.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable jest/no-conditional-expect,jest/no-done-callback */
const axios = require('axios');
const action = require('../lib/action');

Expand Down Expand Up @@ -26,32 +27,32 @@ 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();
})
.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
Expand All @@ -60,43 +61,43 @@ 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();
})
.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();
});
Expand Down
46 changes: 26 additions & 20 deletions test/signing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('SignatureFactory', () => {

expect(signatureFactory.headers).toEqual({
fooA: 'barA',
fooB: 'barB'
fooB: 'barB',
});
});
});
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -72,8 +72,8 @@ describe('SignatureFactory', () => {
let query = {
params: {
limit: 'foo',
since: 'bar'
}
since: 'bar',
},
};

signatureFactory.handleQuery(query);
Expand All @@ -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);
});
Expand All @@ -117,7 +121,7 @@ describe('SignatureFactory', () => {
//init
signatureFactory.headers = {
fooB: 'barB',
fooA: 'barA'
fooA: 'barA',
};

let headers = signatureFactory.getCanonicalHeaders();
Expand All @@ -131,7 +135,7 @@ describe('SignatureFactory', () => {
//init
signatureFactory.headers = {
fooB: 'barB',
fooA: 'barA'
fooA: 'barA',
};

let headers = signatureFactory.getSignedHeaders();
Expand All @@ -153,7 +157,7 @@ describe('SignatureFactory', () => {
'',
'host:data.usabilla.com\n',
'host',
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
].join('\n')
);
});
Expand All @@ -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();
});
});

Expand All @@ -178,7 +186,7 @@ describe('SignatureFactory', () => {
'USBL1-HMAC-SHA256',
signatureFactory.dates.longdate,
`${signatureFactory.dates.shortdate}/usbl1_request`,
'foo'
'foo',
].join('\n')
);
});
Expand Down Expand Up @@ -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(', ')
);
});
Expand Down

0 comments on commit 1b4e6fa

Please sign in to comment.