AccessTokenEncryptorTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const chai = require('chai')
  2. chai.should()
  3. const { expect } = chai
  4. const modulePath = '../../../index.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. describe('AccessTokenEncryptor', function () {
  7. beforeEach(function () {
  8. this.testObject = { hello: 'world' }
  9. this.encrypted2015 =
  10. '2015.1:473a66fb5d816bc716f278ab819d88a5:+mTg7O9sgUND8pNQFG6h2GE='
  11. this.encrypted2016 =
  12. '2016.1:76a7d64a444ccee1a515b49c44844a69:m5YSkexUsLjcF4gLncm72+k='
  13. this.encrypted2019 =
  14. '2019.1:627143b2ab185a020c8720253a4c984e:7gnY6Ez3/Y3UWgLHLfBtJsE=:bf75cecb6aeea55b3c060e1122d2a82d'
  15. this.badLabel = 'xxxxxx:c7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q='
  16. this.badKey = '2015.1:d7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q='
  17. this.badCipherText = '2015.1:c7a39310056b694c:xQf+Uh5Den3JREtvc82GW5Q='
  18. this.settings = {
  19. cipherLabel: '2019.1',
  20. cipherPasswords: {
  21. 2016.1: '11111111111111111111111111111111111111',
  22. 2015.1: '22222222222222222222222222222222222222',
  23. 2019.1: '33333333333333333333333333333333333333',
  24. },
  25. }
  26. this.AccessTokenEncryptor = SandboxedModule.require(modulePath, {
  27. globals: {
  28. Buffer,
  29. },
  30. requires: {
  31. 'logger-sharelatex': {
  32. err() {},
  33. },
  34. },
  35. })
  36. this.encryptor = new this.AccessTokenEncryptor(this.settings)
  37. })
  38. describe('encrypt', function () {
  39. it('should encrypt the object', function (done) {
  40. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  41. expect(err).to.be.null
  42. encrypted.should.match(
  43. /^2019.1:[0-9a-f]{32}:[a-zA-Z0-9=+/]+:[0-9a-f]{32}$/
  44. )
  45. done()
  46. })
  47. })
  48. it('should encrypt the object differently the next time', function (done) {
  49. this.encryptor.encryptJson(this.testObject, (err, encrypted1) => {
  50. expect(err).to.be.null
  51. this.encryptor.encryptJson(this.testObject, (err, encrypted2) => {
  52. expect(err).to.be.null
  53. encrypted1.should.not.equal(encrypted2)
  54. done()
  55. })
  56. })
  57. })
  58. })
  59. describe('decrypt', function () {
  60. it('should decrypt the string to get the same object', function (done) {
  61. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  62. expect(err).to.be.null
  63. this.encryptor.decryptToJson(encrypted, (err, decrypted) => {
  64. expect(err).to.be.null
  65. expect(decrypted).to.deep.equal(this.testObject)
  66. done()
  67. })
  68. })
  69. })
  70. it('should decrypt an 2015 string to get the same object', function (done) {
  71. this.encryptor.decryptToJson(this.encrypted2015, (err, decrypted) => {
  72. expect(err).to.be.null
  73. expect(decrypted).to.deep.equal(this.testObject)
  74. done()
  75. })
  76. })
  77. it('should decrypt an 2016 string to get the same object', function (done) {
  78. this.encryptor.decryptToJson(this.encrypted2016, (err, decrypted) => {
  79. expect(err).to.be.null
  80. expect(decrypted).to.deep.equal(this.testObject)
  81. done()
  82. })
  83. })
  84. it('should decrypt an 2019 string to get the same object', function (done) {
  85. this.encryptor.decryptToJson(this.encrypted2019, (err, decrypted) => {
  86. expect(err).to.be.null
  87. expect(decrypted).to.deep.equal(this.testObject)
  88. done()
  89. })
  90. })
  91. it('should return an error when decrypting an invalid label', function (done) {
  92. this.encryptor.decryptToJson(this.badLabel, (err, decrypted) => {
  93. expect(err).to.be.instanceof(Error)
  94. expect(decrypted).to.be.undefined
  95. done()
  96. })
  97. })
  98. it('should return an error when decrypting an invalid key', function (done) {
  99. this.encryptor.decryptToJson(this.badKey, (err, decrypted) => {
  100. expect(err).to.be.instanceof(Error)
  101. expect(decrypted).to.be.undefined
  102. done()
  103. })
  104. })
  105. it('should return an error when decrypting an invalid ciphertext', function (done) {
  106. this.encryptor.decryptToJson(this.badCipherText, (err, decrypted) => {
  107. expect(err).to.be.instanceof(Error)
  108. expect(decrypted).to.be.undefined
  109. done()
  110. })
  111. })
  112. })
  113. })