CreateRevisionFromDump.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Uuid, ContentType, Dates, Result } from '@standardnotes/domain-core'
  2. import { DumpRepositoryInterface } from '../../Dump/DumpRepositoryInterface'
  3. import { Revision } from '../../Revision/Revision'
  4. import { RevisionRepositoryInterface } from '../../Revision/RevisionRepositoryInterface'
  5. import { RevisionRepositoryResolverInterface } from '../../Revision/RevisionRepositoryResolverInterface'
  6. import { CreateRevisionFromDump } from './CreateRevisionFromDump'
  7. describe('CreateRevisionFromDump', () => {
  8. let revisionRepository: RevisionRepositoryInterface
  9. let revision: Revision
  10. let dumpRepository: DumpRepositoryInterface
  11. let revisionRepositoryResolver: RevisionRepositoryResolverInterface
  12. const createUseCase = () => new CreateRevisionFromDump(dumpRepository, revisionRepositoryResolver)
  13. beforeEach(() => {
  14. revision = Revision.create({
  15. itemUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  16. userUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  17. content: 'test',
  18. contentType: ContentType.create('Note').getValue(),
  19. itemsKeyId: 'test',
  20. encItemKey: 'test',
  21. authHash: 'test',
  22. creationDate: new Date(1),
  23. dates: Dates.create(new Date(1), new Date(2)).getValue(),
  24. }).getValue()
  25. dumpRepository = {} as jest.Mocked<DumpRepositoryInterface>
  26. dumpRepository.getRevisionFromDumpPath = jest.fn().mockReturnValue(Result.ok(revision))
  27. dumpRepository.removeDump = jest.fn()
  28. revisionRepository = {} as jest.Mocked<RevisionRepositoryInterface>
  29. revisionRepository.insert = jest.fn().mockReturnValue(true)
  30. revisionRepositoryResolver = {} as jest.Mocked<RevisionRepositoryResolverInterface>
  31. revisionRepositoryResolver.resolve = jest.fn().mockReturnValue(revisionRepository)
  32. })
  33. it('should create a revision from file dump', async () => {
  34. const result = await createUseCase().execute({
  35. filePath: 'foobar',
  36. roleNames: ['CORE_USER'],
  37. })
  38. expect(result.isFailed()).toBeFalsy()
  39. expect(revisionRepository.insert).toHaveBeenCalled()
  40. expect(dumpRepository.removeDump).toHaveBeenCalled()
  41. })
  42. it('should fail if file path is empty', async () => {
  43. const result = await createUseCase().execute({
  44. filePath: '',
  45. roleNames: ['CORE_USER'],
  46. })
  47. expect(result.isFailed()).toBeTruthy()
  48. expect(revisionRepository.insert).not.toHaveBeenCalled()
  49. expect(dumpRepository.removeDump).not.toHaveBeenCalled()
  50. })
  51. it('should fail if role name is invalid', async () => {
  52. const result = await createUseCase().execute({
  53. filePath: 'foobar',
  54. roleNames: ['INVALID_ROLE_NAME'],
  55. })
  56. expect(result.isFailed()).toBeTruthy()
  57. expect(revisionRepository.insert).not.toHaveBeenCalled()
  58. expect(dumpRepository.removeDump).toHaveBeenCalled()
  59. })
  60. it('should fail if revision cannot be found', async () => {
  61. dumpRepository.getRevisionFromDumpPath = jest.fn().mockReturnValue(Result.fail('Oops'))
  62. const result = await createUseCase().execute({
  63. filePath: 'foobar',
  64. roleNames: ['CORE_USER'],
  65. })
  66. expect(result.isFailed()).toBeTruthy()
  67. expect(revisionRepository.insert).not.toHaveBeenCalled()
  68. expect(dumpRepository.removeDump).toHaveBeenCalled()
  69. })
  70. it('should fail if revision cannot be inserted', async () => {
  71. revisionRepository.insert = jest.fn().mockReturnValue(false)
  72. const result = await createUseCase().execute({
  73. filePath: 'foobar',
  74. roleNames: ['CORE_USER'],
  75. })
  76. expect(result.isFailed()).toBeTruthy()
  77. expect(revisionRepository.insert).toHaveBeenCalled()
  78. expect(dumpRepository.removeDump).toHaveBeenCalled()
  79. })
  80. })