UserAuditLogHandlerTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. globals: {
  27. console: console
  28. },
  29. requires: {
  30. '../../models/User': { User }
  31. }
  32. })
  33. })
  34. afterEach(function() {
  35. this.UserMock.restore()
  36. })
  37. describe('addEntry', function() {
  38. describe('success', function() {
  39. beforeEach(function() {
  40. this.dbUpdate = this.UserMock.expects('updateOne')
  41. .chain('exec')
  42. .resolves({ nModified: 1 })
  43. })
  44. it('writes a log', async function() {
  45. await this.UserAuditLogHandler.promises.addEntry(
  46. this.userId,
  47. this.action.operation,
  48. this.action.initiatorId,
  49. this.action.ip,
  50. this.action.info
  51. )
  52. this.UserMock.verify()
  53. })
  54. it('updates the log for password reset operation witout a initiatorId', async function() {
  55. await expect(
  56. this.UserAuditLogHandler.promises.addEntry(
  57. this.userId,
  58. 'reset-password',
  59. undefined,
  60. this.action.ip,
  61. this.action.info
  62. )
  63. )
  64. this.UserMock.verify()
  65. })
  66. })
  67. describe('errors', function() {
  68. describe('when the user does not exist', function() {
  69. beforeEach(function() {
  70. this.UserMock.expects('updateOne')
  71. .chain('exec')
  72. .resolves({ nModified: 0 })
  73. })
  74. it('throws an error', async function() {
  75. await expect(
  76. this.UserAuditLogHandler.promises.addEntry(
  77. this.userId,
  78. this.action.operation,
  79. this.action.initiatorId,
  80. this.action.ip,
  81. this.action.info
  82. )
  83. ).to.be.rejected
  84. })
  85. })
  86. describe('missing parameters', function() {
  87. it('throws an error when no operation', async function() {
  88. await expect(
  89. this.UserAuditLogHandler.promises.addEntry(
  90. this.userId,
  91. undefined,
  92. this.action.initiatorId,
  93. this.action.ip,
  94. this.action.info
  95. )
  96. ).to.be.rejected
  97. })
  98. it('throws an error when no IP', async function() {
  99. await expect(
  100. this.UserAuditLogHandler.promises.addEntry(
  101. this.userId,
  102. this.action.operation,
  103. this.action.initiatorId,
  104. undefined,
  105. this.action.info
  106. )
  107. ).to.be.rejected
  108. })
  109. it('throws an error when no initiatorId and not a password reset operation', async function() {
  110. await expect(
  111. this.UserAuditLogHandler.promises.addEntry(
  112. this.userId,
  113. this.action.operation,
  114. undefined,
  115. this.action.ip,
  116. this.action.info
  117. )
  118. ).to.be.rejected
  119. })
  120. })
  121. })
  122. })
  123. })