InstitutionsReconfirmationHandlerTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { ObjectId } = require('mongodb')
  5. const { expect } = require('chai')
  6. const modulePath = path.join(
  7. __dirname,
  8. '../../../../app/src/Features/Institutions/InstitutionsReconfirmationHandler'
  9. )
  10. describe('InstitutionsReconfirmationHandler', function () {
  11. beforeEach(function () {
  12. this.InstitutionsReconfirmationHandler = SandboxedModule.require(
  13. modulePath,
  14. {
  15. requires: {
  16. '../../infrastructure/mongodb': (this.mongodb = {
  17. ObjectId,
  18. waitForDb: sinon.stub().resolves(),
  19. }),
  20. '../Subscription/FeaturesUpdater': (this.FeaturesUpdater = {
  21. refreshFeatures: sinon.stub(),
  22. }),
  23. './InstitutionsAPI': (this.InstitutionsAPI = {
  24. promises: {
  25. getUsersNeedingReconfirmationsLapsedProcessed: sinon.stub(),
  26. sendUsersWithReconfirmationsLapsedProcessed: sinon.stub(),
  27. },
  28. }),
  29. },
  30. }
  31. )
  32. })
  33. describe('userId list', function () {
  34. it('should throw an error if IDs not an array', async function () {
  35. let error
  36. try {
  37. await this.InstitutionsReconfirmationHandler.processLapsed()
  38. } catch (e) {
  39. error = e
  40. }
  41. expect(error).to.exist
  42. expect(error.message).to.equal('users is not an array')
  43. })
  44. it('should throw an error if IDs not valid ObjectIds', async function () {
  45. this.InstitutionsAPI.promises.getUsersNeedingReconfirmationsLapsedProcessed.resolves(
  46. {
  47. data: { users: ['not an objectid'] },
  48. }
  49. )
  50. let error
  51. try {
  52. await this.InstitutionsReconfirmationHandler.processLapsed()
  53. } catch (e) {
  54. error = e
  55. }
  56. expect(error).to.exist
  57. expect(error.message).to.equal('user ID not valid')
  58. })
  59. })
  60. it('should log users that have refreshFeatures errors', async function () {
  61. const anError = new Error('oops')
  62. const aUserId = '5efb8b6e9b647b0027e4c0b0'
  63. this.FeaturesUpdater.refreshFeatures.yields(anError)
  64. this.InstitutionsAPI.promises.getUsersNeedingReconfirmationsLapsedProcessed.resolves(
  65. {
  66. data: { users: [aUserId] },
  67. }
  68. )
  69. this.InstitutionsAPI.promises.sendUsersWithReconfirmationsLapsedProcessed.resolves()
  70. let error, result
  71. try {
  72. result = await this.InstitutionsReconfirmationHandler.processLapsed()
  73. } catch (e) {
  74. error = e
  75. }
  76. expect(error).to.not.exist
  77. expect(result.failedToRefresh.length).to.equal(1)
  78. expect(result.failedToRefresh[0]).to.equal(aUserId)
  79. expect(result.refreshedUsers.length).to.equal(0)
  80. })
  81. it('should log but not return errors from sendUsersWithReconfirmationsLapsedProcessed', async function () {
  82. const anError = new Error('oops')
  83. const aUserId = '5efb8b6e9b647b0027e4c0b0'
  84. this.FeaturesUpdater.refreshFeatures.yields()
  85. this.InstitutionsAPI.promises.getUsersNeedingReconfirmationsLapsedProcessed.resolves(
  86. {
  87. data: { users: [aUserId] },
  88. }
  89. )
  90. this.InstitutionsAPI.promises.sendUsersWithReconfirmationsLapsedProcessed.rejects(
  91. anError
  92. )
  93. let error, result
  94. try {
  95. result = await this.InstitutionsReconfirmationHandler.processLapsed()
  96. } catch (e) {
  97. error = e
  98. }
  99. expect(error).to.not.exist
  100. expect(result.refreshedUsers.length).to.equal(1)
  101. expect(result.refreshedUsers[0]).to.equal(aUserId)
  102. expect(result.failedToRefresh.length).to.equal(0)
  103. })
  104. })