Skip to content

Commit

Permalink
improves auth module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoputzer committed Jan 9, 2019
1 parent 6d10af1 commit 73df4fe
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}
}
})
})
})
})

0 comments on commit 73df4fe

Please sign in to comment.