AccessTokenEncryptorTests.js 4.2 KB

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