meshblu-core-manager-token
Version:
Meshblu Token Manager
127 lines (97 loc) • 4.93 kB
text/coffeescript
{describe,beforeEach,it,expect} = global
sinon = require 'sinon'
mongojs = require 'mongojs'
Datastore = require 'meshblu-core-datastore'
TokenManager = require '../'
describe 'TokenManager->generateAndStoreToken', ->
beforeEach (done) ->
= 'im-a-pepper'
= resolve: (uuid, callback) => callback(null, uuid)
database = mongojs 'token-manager-test', ['things']
= new Datastore
database: database
collection: 'things'
database.things.remove done
beforeEach ->
= new TokenManager {, , }
beforeEach (done) ->
.insert {uuid: 'spiral'}, done
describe 'when called without metadata', ->
beforeEach (done) ->
._generateToken = sinon.stub().returns 'abc123'
.generateAndStoreToken {uuid: 'spiral'}, (error, ) =>
done error
describe 'when the record is retrieved', ->
beforeEach (done) ->
.findOne { uuid: 'spiral', hashedToken: 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U=' }, (error, ) =>
done error
it 'should not add expiresOn to the datastore', ->
expect(.expiresOn).to.not.exist
it 'should not add root: true to the datastore', ->
expect(.root).to.not.exist
it 'should add a token to the datastore', ->
expect(.hashedToken).to.equal 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U='
it 'should match the generated token', ->
hashedToken = ._hashToken { uuid: 'spiral', token: 'abc123' }
expect(.hashedToken).to.equal hashedToken
describe 'when called with metadata', ->
beforeEach (done) ->
._generateToken = sinon.stub().returns('abc123')
metadata =
tag: 'foo'
.generateAndStoreToken {uuid: 'spiral', metadata}, (error, ) =>
done error
describe 'when the record is retrieved', ->
beforeEach (done) ->
.findOne { uuid: 'spiral', hashedToken: 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U=' }, (error, ) =>
done error
it 'should add a hashedToken to the datastore', ->
expect(.hashedToken).to.equal 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U='
it 'should not add root: true to the datastore', ->
expect(.root).to.not.exist
it 'should not add expiresOn to the datastore', ->
expect(.expiresOn).to.not.exist
it 'should match the generated token', ->
hashedToken = ._hashToken { uuid: 'spiral', token: 'abc123' }
expect(.hashedToken).to.equal hashedToken
it 'should have the correct metadata in the datastore', ->
expect(.metadata.tag).to.equal 'foo'
describe 'when called with an expiresOn', ->
beforeEach (done) ->
._generateToken = sinon.stub().returns('abc123')
metadata =
tag: 'foo'
= new Date(Date.now() - (1000 * 60))
.generateAndStoreToken {uuid: 'spiral', metadata, }, (error, ) =>
done error
describe 'when the record is retrieved', ->
beforeEach (done) ->
.findOne { uuid: 'spiral', hashedToken: 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U=' }, (error, ) =>
done error
it 'should add a hashedToken to the datastore', ->
expect(.hashedToken).to.equal 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U='
it 'should not add root: true to the datastore', ->
expect(.root).to.not.exist
it 'should add a expiresOn to the datastore', ->
expect(.expiresOn).to.deep.equal
it 'should match the generated token', ->
hashedToken = ._hashToken { uuid: 'spiral', token: 'abc123' }
expect(.hashedToken).to.equal hashedToken
it 'should have the correct metadata in the datastore', ->
expect(.metadata.tag).to.equal 'foo'
describe 'when called with root: true', ->
beforeEach (done) ->
._generateToken = sinon.stub().returns('abc123')
.generateAndStoreToken {uuid: 'spiral', root: true }, (error, ) =>
done error
describe 'when the record is retrieved', ->
beforeEach (done) ->
.findOne { uuid: 'spiral', hashedToken: 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U=' }, (error, ) =>
done error
it 'should add a hashedToken to the datastore', ->
expect(.hashedToken).to.equal 'T/GMBdFNOc9l3uagnYZSwgFfjtp8Vlf6ryltQUEUY1U='
it 'should add root:true to the datastore', ->
expect(.root).to.be.true
it 'should match the generated token', ->
hashedToken = ._hashToken { uuid: 'spiral', token: 'abc123' }
expect(.hashedToken).to.equal hashedToken