ItemRevisionCreationRequestedEventHandler.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'reflect-metadata'
  2. import {
  3. DomainEventPublisherInterface,
  4. DomainEventService,
  5. ItemRevisionCreationRequestedEvent,
  6. } from '@standardnotes/domain-events'
  7. import { Item } from '../Item/Item'
  8. import { ItemRepositoryInterface } from '../Item/ItemRepositoryInterface'
  9. import { ItemRevisionCreationRequestedEventHandler } from './ItemRevisionCreationRequestedEventHandler'
  10. import { ItemBackupServiceInterface } from '../Item/ItemBackupServiceInterface'
  11. import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface'
  12. import { Uuid, ContentType, Dates, Timestamps, UniqueEntityId } from '@standardnotes/domain-core'
  13. import { ItemRepositoryResolverInterface } from '../Item/ItemRepositoryResolverInterface'
  14. describe('ItemRevisionCreationRequestedEventHandler', () => {
  15. let itemRepositoryResolver: ItemRepositoryResolverInterface
  16. let itemRepository: ItemRepositoryInterface
  17. let event: ItemRevisionCreationRequestedEvent
  18. let item: Item
  19. let itemBackupService: ItemBackupServiceInterface
  20. let domainEventFactory: DomainEventFactoryInterface
  21. let domainEventPublisher: DomainEventPublisherInterface
  22. const createHandler = () =>
  23. new ItemRevisionCreationRequestedEventHandler(
  24. itemRepositoryResolver,
  25. itemBackupService,
  26. domainEventFactory,
  27. domainEventPublisher,
  28. )
  29. beforeEach(() => {
  30. item = Item.create(
  31. {
  32. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  33. updatedWithSession: null,
  34. content: 'foobar1',
  35. contentType: ContentType.create(ContentType.TYPES.Note).getValue(),
  36. encItemKey: null,
  37. authHash: null,
  38. itemsKeyId: null,
  39. duplicateOf: null,
  40. deleted: false,
  41. dates: Dates.create(new Date(1616164633241311), new Date(1616164633241311)).getValue(),
  42. timestamps: Timestamps.create(1616164633241311, 1616164633241311).getValue(),
  43. },
  44. new UniqueEntityId('00000000-0000-0000-0000-000000000000'),
  45. ).getValue()
  46. itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
  47. itemRepository.findByUuid = jest.fn().mockReturnValue(item)
  48. itemRepositoryResolver = {} as jest.Mocked<ItemRepositoryResolverInterface>
  49. itemRepositoryResolver.resolve = jest.fn().mockReturnValue(itemRepository)
  50. event = {} as jest.Mocked<ItemRevisionCreationRequestedEvent>
  51. event.createdAt = new Date(1)
  52. event.payload = {
  53. itemUuid: '00000000-0000-0000-0000-000000000000',
  54. roleNames: ['CORE_USER'],
  55. }
  56. event.meta = {
  57. correlation: {
  58. userIdentifier: '1-2-3',
  59. userIdentifierType: 'uuid',
  60. },
  61. origin: DomainEventService.SyncingServer,
  62. }
  63. itemBackupService = {} as jest.Mocked<ItemBackupServiceInterface>
  64. itemBackupService.dump = jest.fn().mockReturnValue('foo://bar')
  65. domainEventFactory = {} as jest.Mocked<DomainEventFactoryInterface>
  66. domainEventFactory.createItemDumpedEvent = jest.fn()
  67. domainEventPublisher = {} as jest.Mocked<DomainEventPublisherInterface>
  68. domainEventPublisher.publish = jest.fn()
  69. })
  70. it('should create a revision for an item', async () => {
  71. await createHandler().handle(event)
  72. expect(domainEventPublisher.publish).toHaveBeenCalled()
  73. expect(domainEventFactory.createItemDumpedEvent).toHaveBeenCalled()
  74. })
  75. it('should do nothing if roles names are not valid', async () => {
  76. event.payload.roleNames = ['INVALID_ROLE_NAME']
  77. await createHandler().handle(event)
  78. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  79. expect(domainEventFactory.createItemDumpedEvent).not.toHaveBeenCalled()
  80. })
  81. it('should not create a revision for an item that does not exist', async () => {
  82. itemRepository.findByUuid = jest.fn().mockReturnValue(null)
  83. itemRepositoryResolver.resolve = jest.fn().mockReturnValue(itemRepository)
  84. await createHandler().handle(event)
  85. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  86. })
  87. it('should not create a revision for an item if the dump was not created', async () => {
  88. itemBackupService.dump = jest.fn().mockReturnValue('')
  89. await createHandler().handle(event)
  90. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  91. expect(domainEventFactory.createItemDumpedEvent).not.toHaveBeenCalled()
  92. })
  93. it('should not create a revision if the item uuid is invalid', async () => {
  94. event.payload.itemUuid = 'invalid-uuid'
  95. await createHandler().handle(event)
  96. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  97. expect(domainEventFactory.createItemDumpedEvent).not.toHaveBeenCalled()
  98. })
  99. })