APNCertificate.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const APNCertificate = require('../../../lib/credentials/certificate/APNCertificate');
  2. const APNKey = require('../../../lib/credentials/certificate/APNKey');
  3. const forge = require('node-forge');
  4. const fs = require('fs');
  5. describe('APNCertificate', function () {
  6. let certPem;
  7. before(function () {
  8. certPem = fs.readFileSync('test/credentials/support/cert.pem');
  9. });
  10. let cert;
  11. beforeEach(function () {
  12. cert = forge.pki.certificateFromPem(certPem.toString());
  13. });
  14. describe('accepts a Certificate object', function () {
  15. it('does not throw', function () {
  16. expect(function () {
  17. new APNCertificate(cert);
  18. }).to.not.throw(Error);
  19. });
  20. });
  21. describe('throws', function () {
  22. it('missing public key', function () {
  23. delete cert.publicKey;
  24. expect(function () {
  25. new APNCertificate(cert);
  26. }).to.throw('certificate object is invalid');
  27. });
  28. it('missing validity', function () {
  29. delete cert.validity;
  30. expect(function () {
  31. new APNCertificate(cert);
  32. }).to.throw('certificate object is invalid');
  33. });
  34. it('missing subject', function () {
  35. delete cert.subject;
  36. expect(function () {
  37. new APNCertificate(cert);
  38. }).to.throw('certificate object is invalid');
  39. });
  40. });
  41. describe('key', function () {
  42. it('returns an APNKey', function () {
  43. expect(new APNCertificate(cert).key()).to.be.an.instanceof(APNKey);
  44. });
  45. it('returns the the certificates public key', function () {
  46. expect(new APNCertificate(cert).key().fingerprint()).to.equal(
  47. '2d594c9861227dd22ba5ae37cc9354e9117a804d'
  48. );
  49. });
  50. });
  51. describe('validity', function () {
  52. it('returns an object containing notBefore', function () {
  53. expect(new APNCertificate(cert).validity())
  54. .to.have.property('notBefore')
  55. .and.to.eql(new Date('2015-01-01T00:00:00Z'));
  56. });
  57. it('returns an object containing notAfter', function () {
  58. expect(new APNCertificate(cert).validity())
  59. .to.have.property('notAfter')
  60. .and.to.eql(new Date('2025-01-01T00:00:00Z'));
  61. });
  62. });
  63. describe('environment', function () {
  64. describe('development certificate', function () {
  65. it('sandbox flag', function () {
  66. expect(new APNCertificate(cert).environment().sandbox).to.be.true;
  67. });
  68. it('production flag', function () {
  69. expect(new APNCertificate(cert).environment().production).to.be.false;
  70. });
  71. });
  72. describe('production certificate', function () {
  73. let productionCertPem, productionCert;
  74. before(function () {
  75. productionCertPem = fs.readFileSync('test/credentials/support/certProduction.pem');
  76. });
  77. beforeEach(function () {
  78. productionCert = forge.pki.certificateFromPem(productionCertPem.toString());
  79. });
  80. it('sandbox flag', function () {
  81. expect(new APNCertificate(productionCert).environment().sandbox).to.be.false;
  82. });
  83. it('production flag', function () {
  84. expect(new APNCertificate(productionCert).environment().production).to.be.true;
  85. });
  86. });
  87. });
  88. });