Skip to content

Commit

Permalink
add test for validating signature
Browse files Browse the repository at this point in the history
  • Loading branch information
radTuti committed Apr 28, 2017
1 parent 4dd4277 commit c77ec81
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/client/validateSignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export default class ValidateSignature {
if (!expected || expected !== computed) {
throw new Error('Invalid signature');
}
else {
log.debug('Valid signature!');
}
log.debug('Valid signature!');
}
}
54 changes: 54 additions & 0 deletions test/client/validateSignature.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from 'tape';
import sinon from 'sinon';
import crypto from 'crypto';
import ValidateSignature from '../../src/client/validateSignature';

test('ValidateSignature.constructor', (expect) => {
const validateSignature = new ValidateSignature('APP_SECRET');
expect.same(validateSignature, {
appSecret: 'APP_SECRET',
}, 'should contain the same structure');
expect.end();
});

test('ValidateSignature.validate - invalid signature', (expect) => {
sinon.stub(crypto, 'createHmac')
.returns({
update: (buffer, encoding) => ({
buffer: buffer,
encoding: encoding,
}),
digest: () => 'INVALID_SECRET',
});
const validateSignature = new ValidateSignature('APP_SECRET');
try {
validateSignature.validate({ get: () => 'INVALID_SECRET' }, {}, 'buffer');
expect.fail('should not throw error');
}
catch (err) {
expect.same(err.message, 'Invalid signature', 'should throw error');
}
expect.end();
crypto.createHmac.restore();
});

test('ValidateSignature.validate - valid signature', (expect) => {
sinon.stub(crypto, 'createHmac')
.returns({
update: (buffer, encoding) => ({
buffer: buffer,
encoding: encoding,
}),
digest: () => 'APP_SECRET',
});
const validateSignature = new ValidateSignature('APP_SECRET');
try {
validateSignature.validate({ get: () => 'APP_SECRET' }, {}, 'buffer');
expect.pass('should not throw error');
}
catch (err) {
expect.pass('should not throw error');
}
expect.end();
crypto.createHmac.restore();
});

0 comments on commit c77ec81

Please sign in to comment.