prepare.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const sinon = require('sinon');
  2. describe('perpareCertificate', function () {
  3. let fakes, prepareCertificate;
  4. beforeEach(function () {
  5. fakes = {
  6. load: sinon.stub(),
  7. parse: sinon.stub(),
  8. validate: sinon.stub(),
  9. logger: sinon.stub(),
  10. };
  11. prepareCertificate = require('../../../lib/credentials/certificate/prepare')(fakes);
  12. });
  13. describe('with valid credentials', function () {
  14. let credentials;
  15. const testOptions = {
  16. pfx: 'myCredentials.pfx',
  17. cert: 'myCert.pem',
  18. key: 'myKey.pem',
  19. ca: 'myCa.pem',
  20. passphrase: 'apntest',
  21. production: true,
  22. };
  23. beforeEach(function () {
  24. fakes.load.withArgs(sinon.match(testOptions)).returns({
  25. pfx: 'myPfxData',
  26. cert: 'myCertData',
  27. key: 'myKeyData',
  28. ca: ['myCaData'],
  29. passphrase: 'apntest',
  30. });
  31. fakes.parse.returnsArg(0);
  32. credentials = prepareCertificate(testOptions);
  33. });
  34. describe('the validation stage', function () {
  35. it('is called once', function () {
  36. expect(fakes.validate).to.be.calledOnce;
  37. });
  38. it('is passed the production flag', function () {
  39. expect(fakes.validate.getCall(0).args[0]).to.have.property('production', true);
  40. });
  41. describe('passed credentials', function () {
  42. it('contains the PFX data', function () {
  43. expect(fakes.validate.getCall(0).args[0]).to.have.property('pfx', 'myPfxData');
  44. });
  45. it('contains the key data', function () {
  46. expect(fakes.validate.getCall(0).args[0]).to.have.property('key', 'myKeyData');
  47. });
  48. it('contains the certificate data', function () {
  49. expect(fakes.validate.getCall(0).args[0]).to.have.property('cert', 'myCertData');
  50. });
  51. it('includes passphrase', function () {
  52. expect(fakes.validate.getCall(0).args[0]).to.have.property('passphrase', 'apntest');
  53. });
  54. });
  55. });
  56. describe('resolution value', function () {
  57. it('contains the PFX data', function () {
  58. return expect(credentials).to.have.property('pfx', 'myPfxData');
  59. });
  60. it('contains the key data', function () {
  61. return expect(credentials).to.have.property('key', 'myKeyData');
  62. });
  63. it('contains the certificate data', function () {
  64. return expect(credentials).to.have.property('cert', 'myCertData');
  65. });
  66. it('contains the CA data', function () {
  67. return expect(credentials).to.have.nested.deep.property('ca[0]', 'myCaData');
  68. });
  69. it('includes passphrase', function () {
  70. return expect(credentials).to.have.property('passphrase', 'apntest');
  71. });
  72. });
  73. });
  74. describe('credential file cannot be parsed', function () {
  75. beforeEach(function () {
  76. fakes.load.returns({ cert: 'myCertData', key: 'myKeyData' });
  77. fakes.parse.throws(new Error('unable to parse key'));
  78. });
  79. it('should resolve with the credentials', function () {
  80. const credentials = prepareCertificate({
  81. cert: 'myUnparseableCert.pem',
  82. key: 'myUnparseableKey.pem',
  83. production: true,
  84. });
  85. return expect(credentials).to.deep.equal({ cert: 'myCertData', key: 'myKeyData' });
  86. });
  87. it('should log an error', function () {
  88. prepareCertificate({ cert: 'myUnparseableCert.pem', key: 'myUnparseableKey.pem' });
  89. expect(fakes.logger).to.be.calledWith(
  90. sinon.match(function (err) {
  91. return err.message ? err.message.match(/unable to parse key/) : false;
  92. }, '"unable to parse key"')
  93. );
  94. });
  95. it('should not attempt to validate', function () {
  96. prepareCertificate({ cert: 'myUnparseableCert.pem', key: 'myUnparseableKey.pem' });
  97. expect(fakes.validate).to.not.be.called;
  98. });
  99. });
  100. describe('credential validation fails', function () {
  101. it('should throw', function () {
  102. fakes.load.returns(Promise.resolve({ cert: 'myCertData', key: 'myMismatchedKeyData' }));
  103. fakes.parse.returnsArg(0);
  104. fakes.validate.throws(new Error('certificate and key do not match'));
  105. return expect(() =>
  106. prepareCertificate({ cert: 'myCert.pem', key: 'myMistmatchedKey.pem' })
  107. ).to.throw(/certificate and key do not match/);
  108. });
  109. });
  110. describe('credential file cannot be loaded', function () {
  111. it('should throw', function () {
  112. fakes.load.throws(new Error('ENOENT, no such file or directory'));
  113. return expect(() =>
  114. prepareCertificate({ cert: 'noSuchFile.pem', key: 'myKey.pem' })
  115. ).to.throw('ENOENT, no such file or directory');
  116. });
  117. });
  118. });