validate.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const sinon = require('sinon');
  2. const validateCredentials = require('../../../lib/credentials/certificate/validate');
  3. const fakeCredentials = function () {
  4. return {
  5. key: {
  6. _fingerprint: 'fingerprint1',
  7. fingerprint: function () {
  8. return this._fingerprint;
  9. },
  10. },
  11. certificates: [
  12. {
  13. _key: {
  14. _fingerprint: 'fingerprint1',
  15. fingerprint: function () {
  16. return this._fingerprint;
  17. },
  18. },
  19. _validity: {
  20. notBefore: new Date(Date.now() - 100000),
  21. notAfter: new Date(Date.now() + 100000),
  22. },
  23. key: function () {
  24. return this._key;
  25. },
  26. validity: function () {
  27. return this._validity;
  28. },
  29. environment: function () {
  30. return { production: true, sandbox: false };
  31. },
  32. },
  33. ],
  34. production: true,
  35. };
  36. };
  37. describe('validateCredentials', function () {
  38. let credentials;
  39. beforeEach(function () {
  40. credentials = fakeCredentials();
  41. });
  42. describe('with valid credentials', function () {
  43. it('returns', function () {
  44. expect(function () {
  45. validateCredentials(credentials);
  46. }).to.not.throw();
  47. });
  48. });
  49. describe('with mismatched key and certificate', function () {
  50. it('throws', function () {
  51. sinon.stub(credentials.certificates[0]._key, 'fingerprint').returns('fingerprint2');
  52. expect(function () {
  53. validateCredentials(credentials);
  54. }).to.throw(/certificate and key do not match/);
  55. });
  56. });
  57. describe('with expired certificate', function () {
  58. it('throws', function () {
  59. sinon.stub(credentials.certificates[0], 'validity').returns({
  60. notBefore: new Date(Date.now() - 100000),
  61. notAfter: new Date(Date.now() - 10000),
  62. });
  63. expect(function () {
  64. validateCredentials(credentials);
  65. }).to.throw(/certificate has expired/);
  66. });
  67. });
  68. describe('with incorrect environment', function () {
  69. it('throws with sandbox cert in production', function () {
  70. sinon.stub(credentials.certificates[0], 'environment').returns({
  71. production: false,
  72. sandbox: true,
  73. });
  74. expect(function () {
  75. validateCredentials(credentials);
  76. }).to.throw('certificate does not support configured environment, production: true');
  77. });
  78. it('throws with production cert in sandbox', function () {
  79. sinon.stub(credentials.certificates[0], 'environment').returns({
  80. production: true,
  81. sandbox: false,
  82. });
  83. credentials.production = false;
  84. expect(function () {
  85. validateCredentials(credentials);
  86. }).to.throw('certificate does not support configured environment, production: false');
  87. });
  88. });
  89. describe('with missing production flag', function () {
  90. it('does not throw', function () {
  91. sinon.stub(credentials.certificates[0], 'environment').returns({
  92. production: true,
  93. sandbox: false,
  94. });
  95. credentials.production = undefined;
  96. expect(function () {
  97. validateCredentials(credentials);
  98. }).to.not.throw();
  99. });
  100. });
  101. describe('with certificate supporting both environments', function () {
  102. it('does not throw', function () {
  103. sinon.stub(credentials.certificates[0], 'environment').returns({
  104. production: true,
  105. sandbox: true,
  106. });
  107. credentials.production = false;
  108. expect(function () {
  109. validateCredentials(credentials);
  110. }).to.not.throw();
  111. });
  112. });
  113. });