UserAuditLogHandlerTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const { ObjectId } = require('mongodb-legacy')
  4. const SandboxedModule = require('sandboxed-module')
  5. const { UserAuditLogEntry } = require('../helpers/models/UserAuditLogEntry')
  6. const MODULE_PATH = '../../../../app/src/Features/User/UserAuditLogHandler'
  7. describe('UserAuditLogHandler', function () {
  8. beforeEach(function () {
  9. this.userId = new ObjectId()
  10. this.initiatorId = new 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.UserAuditLogEntryMock = sinon.mock(UserAuditLogEntry)
  25. this.UserAuditLogHandler = SandboxedModule.require(MODULE_PATH, {
  26. requires: {
  27. '../../models/UserAuditLogEntry': { UserAuditLogEntry },
  28. },
  29. })
  30. })
  31. afterEach(function () {
  32. this.UserAuditLogEntryMock.restore()
  33. })
  34. describe('addEntry', function () {
  35. describe('success', function () {
  36. beforeEach(function () {
  37. this.dbUpdate = this.UserAuditLogEntryMock.expects('create')
  38. .chain('exec')
  39. .resolves({ modifiedCount: 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.UserAuditLogEntryMock.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.UserAuditLogEntryMock.verify()
  62. })
  63. it('updates the log for a email removal via script', async function () {
  64. await expect(
  65. this.UserAuditLogHandler.promises.addEntry(
  66. this.userId,
  67. 'remove-email',
  68. undefined,
  69. this.action.ip,
  70. {
  71. removedEmail: 'foo',
  72. script: true,
  73. }
  74. )
  75. )
  76. this.UserAuditLogEntryMock.verify()
  77. })
  78. it('updates the log when no ip address or initiatorId is specified for a group join event', async function () {
  79. this.UserAuditLogHandler.promises.addEntry(
  80. this.userId,
  81. 'join-group-subscription',
  82. undefined,
  83. undefined,
  84. {
  85. subscriptionId: 'foo',
  86. }
  87. )
  88. })
  89. })
  90. describe('errors', function () {
  91. describe('missing parameters', function () {
  92. it('throws an error when no operation', async function () {
  93. await expect(
  94. this.UserAuditLogHandler.promises.addEntry(
  95. this.userId,
  96. undefined,
  97. this.action.initiatorId,
  98. this.action.ip,
  99. this.action.info
  100. )
  101. ).to.be.rejected
  102. })
  103. it('throws an error when no IP and not excempt', async function () {
  104. await expect(
  105. this.UserAuditLogHandler.promises.addEntry(
  106. this.userId,
  107. this.action.operation,
  108. this.action.initiatorId,
  109. undefined,
  110. this.action.info
  111. )
  112. ).to.be.rejected
  113. })
  114. it('throws an error when no initiatorId and not a password reset operation', async function () {
  115. await expect(
  116. this.UserAuditLogHandler.promises.addEntry(
  117. this.userId,
  118. this.action.operation,
  119. undefined,
  120. this.action.ip,
  121. this.action.info
  122. )
  123. ).to.be.rejected
  124. })
  125. it('throws an error when remove-email is not from a script, but has no initiatorId', async function () {
  126. await expect(
  127. this.UserAuditLogHandler.promises.addEntry(
  128. this.userId,
  129. 'remove-email',
  130. undefined,
  131. this.action.ip,
  132. {
  133. removedEmail: 'foo',
  134. }
  135. )
  136. ).to.be.rejected
  137. })
  138. })
  139. })
  140. })
  141. })