ProjectAuditLogHandlerTests.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const { ObjectId } = require('mongodb')
  4. const SandboxedModule = require('sandboxed-module')
  5. const { Project } = require('../helpers/models/Project')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/Project/ProjectAuditLogHandler'
  8. describe('ProjectAuditLogHandler', function () {
  9. beforeEach(function () {
  10. this.projectId = ObjectId()
  11. this.userId = ObjectId()
  12. this.ProjectMock = sinon.mock(Project)
  13. this.ProjectAuditLogHandler = SandboxedModule.require(MODULE_PATH, {
  14. requires: {
  15. '../../models/Project': { Project },
  16. },
  17. })
  18. })
  19. afterEach(function () {
  20. this.ProjectMock.restore()
  21. })
  22. describe('addEntry', function () {
  23. describe('success', function () {
  24. beforeEach(async function () {
  25. this.dbUpdate = this.ProjectMock.expects('updateOne').withArgs(
  26. { _id: this.projectId },
  27. {
  28. $push: {
  29. auditLog: {
  30. $each: [
  31. {
  32. operation: 'translate',
  33. initiatorId: this.userId,
  34. info: { destinationLanguage: 'tagalog' },
  35. timestamp: sinon.match.typeOf('date'),
  36. },
  37. ],
  38. $slice: -200,
  39. },
  40. },
  41. }
  42. )
  43. this.dbUpdate.chain('exec').resolves({ nModified: 1 })
  44. this.operationId = await this.ProjectAuditLogHandler.promises.addEntry(
  45. this.projectId,
  46. 'translate',
  47. this.userId,
  48. { destinationLanguage: 'tagalog' }
  49. )
  50. })
  51. it('writes a log', async function () {
  52. this.ProjectMock.verify()
  53. })
  54. })
  55. describe('when the project does not exist', function () {
  56. beforeEach(function () {
  57. this.ProjectMock.expects('updateOne')
  58. .chain('exec')
  59. .resolves({ nModified: 0 })
  60. })
  61. it('throws an error', async function () {
  62. await expect(
  63. this.ProjectAuditLogHandler.promises.addEntry(
  64. this.projectId,
  65. 'translate',
  66. this.userId,
  67. { destinationLanguage: 'tagalog' }
  68. )
  69. ).to.be.rejected
  70. })
  71. })
  72. })
  73. })