2016-03-25 11:04:46 +00:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../index.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
path = require('path')
|
|
|
|
|
|
|
|
describe 'AccessTokenEncryptor', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@testObject = {"hello":"world"}
|
2019-12-04 10:03:10 +00:00
|
|
|
@encrypted2015 = "2015.1:473a66fb5d816bc716f278ab819d88a5:+mTg7O9sgUND8pNQFG6h2GE="
|
|
|
|
@encrypted2016 = "2016.1:76a7d64a444ccee1a515b49c44844a69:m5YSkexUsLjcF4gLncm72+k="
|
2019-12-09 16:07:25 +00:00
|
|
|
@encrypted2019 = "2019.1:627143b2ab185a020c8720253a4c984e:7gnY6Ez3/Y3UWgLHLfBtJsE=:bf75cecb6aeea55b3c060e1122d2a82d"
|
2016-03-25 11:04:46 +00:00
|
|
|
@badLabel = "xxxxxx:c7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q="
|
|
|
|
@badKey = "2015.1:d7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q="
|
|
|
|
@badCipherText = "2015.1:c7a39310056b694c:xQf+Uh5Den3JREtvc82GW5Q="
|
2016-03-25 14:33:38 +00:00
|
|
|
@settings =
|
2019-12-04 10:03:10 +00:00
|
|
|
cipherLabel: "2019.1"
|
2016-03-25 14:33:38 +00:00
|
|
|
cipherPasswords:
|
|
|
|
"2016.1": "11111111111111111111111111111111111111"
|
|
|
|
"2015.1": "22222222222222222222222222222222222222"
|
2019-12-04 10:03:10 +00:00
|
|
|
"2019.1": "33333333333333333333333333333333333333"
|
2019-12-09 14:49:23 +00:00
|
|
|
@AccessTokenEncryptor = SandboxedModule.require modulePath
|
|
|
|
@encryptor = new @AccessTokenEncryptor(@settings)
|
2016-03-25 11:04:46 +00:00
|
|
|
|
|
|
|
describe "encrypt", ->
|
|
|
|
it 'should encrypt the object', (done)->
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.encryptJson @testObject, (err, encrypted)->
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.null
|
2019-12-09 14:49:23 +00:00
|
|
|
encrypted.should.match(/^2019.1:[0-9a-f]{32}:[a-zA-Z0-9=+\/]+:[0-9a-f]{32}$/)
|
2016-03-25 11:04:46 +00:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should encrypt the object differently the next time', (done)->
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.encryptJson @testObject, (err, encrypted1)=>
|
|
|
|
@encryptor.encryptJson @testObject, (err, encrypted2)=>
|
2016-03-25 11:04:46 +00:00
|
|
|
encrypted1.should.not.equal(encrypted2)
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "decrypt", ->
|
|
|
|
it 'should decrypt the string to get the same object', (done)->
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.encryptJson @testObject, (err, encrypted) =>
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.null
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.decryptToJson encrypted, (err, decrypted) =>
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.null
|
|
|
|
expect(decrypted).to.deep.equal @testObject
|
|
|
|
done()
|
|
|
|
|
2019-12-04 10:03:10 +00:00
|
|
|
it 'should decrypt an 2015 string to get the same object', (done)->
|
|
|
|
@encryptor.decryptToJson @encrypted2015, (err, decrypted)=>
|
|
|
|
expect(err).to.be.null
|
|
|
|
expect(decrypted).to.deep.equal @testObject
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should decrypt an 2016 string to get the same object', (done)->
|
|
|
|
@encryptor.decryptToJson @encrypted2016, (err, decrypted)=>
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.null
|
|
|
|
expect(decrypted).to.deep.equal @testObject
|
|
|
|
done()
|
|
|
|
|
2019-12-09 16:07:25 +00:00
|
|
|
it 'should decrypt an 2019 string to get the same object', (done)->
|
|
|
|
@encryptor.decryptToJson @encrypted2019, (err, decrypted)=>
|
|
|
|
expect(err).to.be.null
|
|
|
|
expect(decrypted).to.deep.equal @testObject
|
|
|
|
done()
|
|
|
|
|
2016-03-25 11:04:46 +00:00
|
|
|
it 'should return an error when decrypting an invalid label', (done)->
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.decryptToJson @badLabel, (err, decrypted)->
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.instanceof(Error)
|
|
|
|
expect(decrypted).to.be.undefined
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return an error when decrypting an invalid key', (done)->
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.decryptToJson @badKey, (err, decrypted)->
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.instanceof(Error)
|
|
|
|
expect(decrypted).to.be.undefined
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return an error when decrypting an invalid ciphertext',(done)->
|
2016-03-25 14:33:38 +00:00
|
|
|
@encryptor.decryptToJson @badCipherText, (err, decrypted)->
|
2016-03-25 11:04:46 +00:00
|
|
|
expect(err).to.be.instanceof(Error)
|
|
|
|
expect(decrypted).to.be.undefined
|
|
|
|
done()
|