Skip to content

Commit ebe8316

Browse files
committed
README updated, tests for different attr name added
1 parent d8a1615 commit ebe8316

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let verification = objectHmac.verifyHmac(obj, key);
4242
// true
4343
```
4444

45-
### Only calculate HMAC for an object
45+
### Only calculate HMAC for a JSON object
4646

4747
```js
4848
const objectHmac = require('@tsmx/object-hmac');

test/object-hmac.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ describe('object-hmac test suite', () => {
33
const objectHmac = require('../object-hmac');
44
const testKey = 'HmacSecret-0815';
55
const testHmac = 'bb83e36f2c030af71803fd6a82b49ea638944bb6638351754a967f4f5638ac3b';
6+
const testHmacAttribute = '__hmac';
7+
const testHmacAttributeDifferent = '_signature';
68
var testObjects = null;
79

810
beforeEach(() => {
@@ -18,9 +20,17 @@ describe('object-hmac test suite', () => {
1820

1921
it('tests a successful HMAC creation for an object', async (done) => {
2022
let obj = testObjects.testObject;
21-
expect(obj['__hmac']).toBeUndefined();
23+
expect(obj[testHmacAttribute]).toBeUndefined();
2224
objectHmac.createHmac(obj, testKey);
23-
expect(obj['__hmac']).toStrictEqual(testHmac);
25+
expect(obj[testHmacAttribute]).toStrictEqual(testHmac);
26+
done();
27+
});
28+
29+
it('tests a successful HMAC creation for an object with a different attribute name', async (done) => {
30+
let obj = testObjects.testObject;
31+
expect(obj[testHmacAttributeDifferent]).toBeUndefined();
32+
objectHmac.createHmac(obj, testKey, testHmacAttributeDifferent);
33+
expect(obj[testHmacAttributeDifferent]).toStrictEqual(testHmac);
2434
done();
2535
});
2636

@@ -29,6 +39,16 @@ describe('object-hmac test suite', () => {
2939
done();
3040
});
3141

42+
it('test a successful HMAC verification with a different attribute name', async (done) => {
43+
expect(objectHmac.verifyHmac(testObjects.testObjectWithHmacDifferentAttribute, testKey, testHmacAttributeDifferent)).toBeTruthy();
44+
done();
45+
});
46+
47+
it('test a failed HMAC verification - different attribute name', async (done) => {
48+
expect(objectHmac.verifyHmac(testObjects.testObjectWithHmac, testKey, testHmacAttributeDifferent)).toBeFalsy();
49+
done();
50+
});
51+
3252
it('test a failed HMAC verification - no HMAC provided for object', async (done) => {
3353
expect(objectHmac.verifyHmac(testObjects.testObject, testKey)).toBeFalsy();
3454
done();

test/testobjects.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ module.exports.testObjectWithHmac =
2121
__hmac: 'bb83e36f2c030af71803fd6a82b49ea638944bb6638351754a967f4f5638ac3b'
2222
}
2323

24+
module.exports.testObjectWithHmacDifferentAttribute =
25+
{
26+
title: 'Test-Object',
27+
numbers: [1, 12, 123],
28+
subObject: {
29+
name: 'Max',
30+
age: 32,
31+
hobbies: ['sports', 'travelling']
32+
},
33+
_signature: 'bb83e36f2c030af71803fd6a82b49ea638944bb6638351754a967f4f5638ac3b'
34+
}
35+
2436
module.exports.testObjectChangedAttribute =
2537
{
2638
title: 'Test-ObjectX',

0 commit comments

Comments
 (0)