UserAuditLogHandlerTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const { ObjectId } = require('mongodb')
  4. const SandboxedModule = require('sandboxed-module')
  5. const { User } = require('../helpers/models/User')
  6. const MODULE_PATH = '../../../../app/src/Features/User/UserAuditLogHandler'
  7. describe('UserAuditLogHandler', function () {
  8. beforeEach(function () {
  9. this.userId = ObjectId()
  10. this.initiatorId = ObjectId()
  11. this.action = {
  12. operation: 'clear-sessions',
  13. initiatorId: this.initiatorId,
  14. info: {
  15. sessions: [
  16. {
  17. ip_address: '0:0:0:0',
  18. session_created: '2020-07-15T16:07:57.652Z',
  19. },
  20. ],
  21. },
  22. ip: '0:0:0:0',
  23. }
  24. this.UserMock = sinon.mock(User)
  25. this.UserAuditLogHandler = SandboxedModule.require(MODULE_PATH, {
  26. requires: {
  27. '../../models/User': { User },
  28. },
  29. })
  30. })
  31. afterEach(function () {
  32. this.UserMock.restore()
  33. })
  34. describe('addEntry', function () {
  35. describe('success', function () {
  36. beforeEach(function () {
  37. this.dbUpdate = this.UserMock.expects('updateOne')
  38. .chain('exec')
  39. .resolves({ nModified: 1 })
  40. })
  41. it('writes a log', async function () {
  42. await this.UserAuditLogHandler.promises.addEntry(
  43. this.userId,
  44. this.action.operation,
  45. this.action.initiatorId,
  46. this.action.ip,
  47. this.action.info
  48. )
  49. this.UserMock.verify()
  50. })
  51. it('updates the log for password reset operation witout a initiatorId', async function () {
  52. await expect(
  53. this.UserAuditLogHandler.promises.addEntry(
  54. this.userId,
  55. 'reset-password',
  56. undefined,
  57. this.action.ip,
  58. this.action.info
  59. )
  60. )
  61. this.UserMock.verify()
  62. })
  63. })
  64. describe('errors', function () {
  65. describe('when the user does not exist', function () {
  66. beforeEach(function () {
  67. this.UserMock.expects('updateOne')
  68. .chain('exec')
  69. .resolves({ nModified: 0 })
  70. })
  71. it('throws an error', async function () {
  72. await expect(
  73. this.UserAuditLogHandler.promises.addEntry(
  74. this.userId,
  75. this.action.operation,
  76. this.action.initiatorId,
  77. this.action.ip,
  78. this.action.info
  79. )
  80. ).to.be.rejected
  81. })
  82. })
  83. describe('missing parameters', function () {
  84. it('throws an error when no operation', async function () {
  85. await expect(
  86. this.UserAuditLogHandler.promises.addEntry(
  87. this.userId,
  88. undefined,
  89. this.action.initiatorId,
  90. this.action.ip,
  91. this.action.info
  92. )
  93. ).to.be.rejected
  94. })
  95. it('throws an error when no IP', async function () {
  96. await expect(
  97. this.UserAuditLogHandler.promises.addEntry(
  98. this.userId,
  99. this.action.operation,
  100. this.action.initiatorId,
  101. undefined,
  102. this.action.info
  103. )
  104. ).to.be.rejected
  105. })
  106. it('throws an error when no initiatorId and not a password reset operation', async function () {
  107. await expect(
  108. this.UserAuditLogHandler.promises.addEntry(
  109. this.userId,
  110. this.action.operation,
  111. undefined,
  112. this.action.ip,
  113. this.action.info
  114. )
  115. ).to.be.rejected
  116. })
  117. })
  118. })
  119. })
  120. })