From 73df4fe0e4d5be68e069f832ae7d6bf8edf8d58c Mon Sep 17 00:00:00 2001 From: Ivo von Putzer Reibegg Date: Wed, 9 Jan 2019 14:54:16 +0100 Subject: [PATCH] improves auth module tests --- test/auth.js | 61 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/test/auth.js b/test/auth.js index 1db1f30..5d3a920 100644 --- a/test/auth.js +++ b/test/auth.js @@ -3,24 +3,63 @@ test('coinbase-pro-api/auth', () => { test('.signatureFor', () => { const { signatureFor } = require('../auth') - const { createHmac } = require('crypto') test('is callable', () => { deepStrictEqual(typeof signatureFor, 'function') }) - test('creates sha256 hmac signature using crypto', () => { - const signature = signatureFor({ timestamp: 1, method: 'get' }) - deepStrictEqual(signature, createHmac('sha256', Buffer.from(String.prototype, 'base64')) - .update(`1GET`) - .digest('base64')) + test('creates base64 encoded sha256 hmac signature using crypto', (done) => { + signatureFor({ timestamp: 1, method: 'get' }, undefined, { + createHmac (algorithm) { + deepStrictEqual(algorithm, 'sha256') + return { + update (digest) { + deepStrictEqual(digest, '1GET') + return { + digest (encoding) { + deepStrictEqual(encoding, 'base64') + done() + } + } + } + } + } + }) }) - test('takes an optional secret argument', () => { - const signature = signatureFor({ timestamp: 1, method: 'get' }, { npm_config_coinbase_pro_api_secret: String.prototype }) - deepStrictEqual(signature, createHmac('sha256', Buffer.from(String.prototype, 'base64')) - .update(`1GET`) - .digest('base64')) + test('takes an optional secret argument', (done) => { + const secret = Buffer.from('secret').toString('base64') + signatureFor({ timestamp: 1, method: 'get' }, { npm_config_coinbase_pro_api_secret: secret }, { + createHmac (algorithm, key) { + deepStrictEqual(key, Buffer.from(secret, 'base64')) + return { + update (digest) { + return { + digest (encoding) { + done() + } + } + } + } + } + }) + }) + + test('optional secret argument defaults to empty string when configuration is missing', (done) => { + signatureFor({ timestamp: 1, method: 'get' }, {}, { + createHmac (algorithm, key) { + deepStrictEqual(key, Buffer.from(String.prototype, 'base64')) + return { + update (digest) { + return { + digest (encoding) { + done() + } + } + } + } + } + }) }) }) })