TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import { Logger } from 'winston'
  2. import { RevisionRepositoryInterface } from '../../../Revision/RevisionRepositoryInterface'
  3. import { TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser } from './TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser'
  4. import { Revision } from '../../../Revision/Revision'
  5. import { ContentType, Dates, UniqueEntityId, Uuid } from '@standardnotes/domain-core'
  6. import { TimerInterface } from '@standardnotes/time'
  7. describe('TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser', () => {
  8. let primaryRevisionRepository: RevisionRepositoryInterface
  9. let secondaryRevisionRepository: RevisionRepositoryInterface | null
  10. let logger: Logger
  11. let primaryRevision1: Revision
  12. let primaryRevision2: Revision
  13. let secondaryRevision1: Revision
  14. let secondaryRevision2: Revision
  15. let timer: TimerInterface
  16. const createUseCase = () =>
  17. new TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser(
  18. primaryRevisionRepository,
  19. secondaryRevisionRepository,
  20. timer,
  21. logger,
  22. )
  23. beforeEach(() => {
  24. primaryRevision1 = Revision.create(
  25. {
  26. itemUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  27. userUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  28. content: 'test',
  29. contentType: ContentType.create('Note').getValue(),
  30. itemsKeyId: 'test',
  31. encItemKey: 'test',
  32. authHash: 'test',
  33. creationDate: new Date(1),
  34. dates: Dates.create(new Date(1), new Date(2)).getValue(),
  35. },
  36. new UniqueEntityId('00000000-0000-0000-0000-000000000000'),
  37. ).getValue()
  38. primaryRevision2 = Revision.create(
  39. {
  40. itemUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc2d').getValue(),
  41. userUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  42. content: 'test',
  43. contentType: ContentType.create('Note').getValue(),
  44. itemsKeyId: 'test',
  45. encItemKey: 'test',
  46. authHash: 'test',
  47. creationDate: new Date(1),
  48. dates: Dates.create(new Date(1), new Date(2)).getValue(),
  49. },
  50. new UniqueEntityId('00000000-0000-0000-0000-000000000001'),
  51. ).getValue()
  52. secondaryRevision1 = Revision.create(
  53. {
  54. itemUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  55. userUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  56. content: 'test',
  57. contentType: ContentType.create('Note').getValue(),
  58. itemsKeyId: 'test',
  59. encItemKey: 'test',
  60. authHash: 'test',
  61. creationDate: new Date(1),
  62. dates: Dates.create(new Date(1), new Date(2)).getValue(),
  63. },
  64. new UniqueEntityId('00000000-0000-0000-0000-000000000000'),
  65. ).getValue()
  66. secondaryRevision2 = Revision.create(
  67. {
  68. itemUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc2d').getValue(),
  69. userUuid: Uuid.create('84c0f8e8-544a-4c7e-9adf-26209303bc1d').getValue(),
  70. content: 'test',
  71. contentType: ContentType.create('Note').getValue(),
  72. itemsKeyId: 'test',
  73. encItemKey: 'test',
  74. authHash: 'test',
  75. creationDate: new Date(1),
  76. dates: Dates.create(new Date(1), new Date(2)).getValue(),
  77. },
  78. new UniqueEntityId('00000000-0000-0000-0000-000000000001'),
  79. ).getValue()
  80. primaryRevisionRepository = {} as jest.Mocked<RevisionRepositoryInterface>
  81. primaryRevisionRepository.countByUserUuid = jest.fn().mockResolvedValue(2)
  82. primaryRevisionRepository.findByUserUuid = jest
  83. .fn()
  84. .mockResolvedValueOnce([primaryRevision1])
  85. .mockResolvedValueOnce([primaryRevision2])
  86. .mockResolvedValueOnce([primaryRevision1])
  87. .mockResolvedValueOnce([primaryRevision2])
  88. primaryRevisionRepository.removeByUserUuid = jest.fn().mockResolvedValue(undefined)
  89. secondaryRevisionRepository = {} as jest.Mocked<RevisionRepositoryInterface>
  90. secondaryRevisionRepository.save = jest.fn().mockResolvedValue(undefined)
  91. secondaryRevisionRepository.removeByUserUuid = jest.fn().mockResolvedValue(undefined)
  92. secondaryRevisionRepository.countByUserUuid = jest.fn().mockResolvedValue(2)
  93. secondaryRevisionRepository.findOneByUuid = jest
  94. .fn()
  95. .mockResolvedValueOnce(secondaryRevision1)
  96. .mockResolvedValueOnce(secondaryRevision2)
  97. logger = {} as jest.Mocked<Logger>
  98. logger.error = jest.fn()
  99. logger.info = jest.fn()
  100. timer = {} as jest.Mocked<TimerInterface>
  101. timer.getTimestampInMicroseconds = jest.fn().mockReturnValue(123)
  102. timer.convertMicrosecondsToTimeStructure = jest.fn().mockReturnValue({
  103. days: 0,
  104. hours: 0,
  105. minutes: 0,
  106. seconds: 0,
  107. milliseconds: 0,
  108. })
  109. })
  110. describe('successfull transition', () => {
  111. it('should transition Revisions from primary to secondary database', async () => {
  112. const useCase = createUseCase()
  113. const result = await useCase.execute({
  114. userUuid: '00000000-0000-0000-0000-000000000000',
  115. })
  116. expect(result.isFailed()).toBeFalsy()
  117. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledTimes(2)
  118. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledWith(
  119. Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  120. )
  121. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenCalledTimes(4)
  122. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(1, {
  123. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  124. limit: 1,
  125. offset: 0,
  126. })
  127. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(2, {
  128. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  129. limit: 1,
  130. offset: 1,
  131. })
  132. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(3, {
  133. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  134. limit: 1,
  135. offset: 0,
  136. })
  137. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(4, {
  138. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  139. limit: 1,
  140. offset: 1,
  141. })
  142. expect((secondaryRevisionRepository as RevisionRepositoryInterface).save).toHaveBeenCalledTimes(2)
  143. expect((secondaryRevisionRepository as RevisionRepositoryInterface).save).toHaveBeenCalledWith(primaryRevision1)
  144. expect((secondaryRevisionRepository as RevisionRepositoryInterface).save).toHaveBeenCalledWith(primaryRevision2)
  145. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).not.toHaveBeenCalled()
  146. expect(primaryRevisionRepository.removeByUserUuid).toHaveBeenCalledTimes(1)
  147. })
  148. it('should log an error if deleting Revisions from primary database fails', async () => {
  149. primaryRevisionRepository.removeByUserUuid = jest.fn().mockRejectedValue(new Error('error'))
  150. const useCase = createUseCase()
  151. const result = await useCase.execute({
  152. userUuid: '00000000-0000-0000-0000-000000000000',
  153. })
  154. expect(result.isFailed()).toBeFalsy()
  155. expect(logger.error).toHaveBeenCalledTimes(1)
  156. expect(logger.error).toHaveBeenCalledWith(
  157. 'Failed to clean up primary database revisions for user 00000000-0000-0000-0000-000000000000: error',
  158. )
  159. })
  160. })
  161. describe('failed transition', () => {
  162. it('should remove Revisions from secondary database if integrity check fails', async () => {
  163. const secondaryRevision2WithDifferentContent = Revision.create({
  164. ...secondaryRevision2.props,
  165. content: 'different-content',
  166. }).getValue()
  167. ;(secondaryRevisionRepository as RevisionRepositoryInterface).findOneByUuid = jest
  168. .fn()
  169. .mockResolvedValueOnce(secondaryRevision1)
  170. .mockResolvedValueOnce(secondaryRevision2WithDifferentContent)
  171. const useCase = createUseCase()
  172. const result = await useCase.execute({
  173. userUuid: '00000000-0000-0000-0000-000000000000',
  174. })
  175. expect(result.isFailed()).toBeTruthy()
  176. expect(result.getError()).toEqual(
  177. 'Revision 00000000-0000-0000-0000-000000000001 is not identical in primary and secondary database',
  178. )
  179. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  180. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  181. })
  182. it('should remove Revisions from secondary database if migrating Revisions fails', async () => {
  183. primaryRevisionRepository.findByUserUuid = jest
  184. .fn()
  185. .mockResolvedValueOnce([primaryRevision1])
  186. .mockRejectedValueOnce(new Error('error'))
  187. const useCase = createUseCase()
  188. const result = await useCase.execute({
  189. userUuid: '00000000-0000-0000-0000-000000000000',
  190. })
  191. expect(result.isFailed()).toBeTruthy()
  192. expect(result.getError()).toEqual('error')
  193. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  194. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  195. })
  196. it('should log an error if deleting Revisions from secondary database fails upon migration failure', async () => {
  197. primaryRevisionRepository.findByUserUuid = jest
  198. .fn()
  199. .mockResolvedValueOnce([primaryRevision1])
  200. .mockRejectedValueOnce(new Error('error'))
  201. ;(secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid = jest
  202. .fn()
  203. .mockRejectedValue(new Error('error'))
  204. const useCase = createUseCase()
  205. const result = await useCase.execute({
  206. userUuid: '00000000-0000-0000-0000-000000000000',
  207. })
  208. expect(result.isFailed()).toBeTruthy()
  209. expect(logger.error).toHaveBeenCalledTimes(1)
  210. expect(logger.error).toHaveBeenCalledWith(
  211. 'Failed to clean up secondary database revisions for user 00000000-0000-0000-0000-000000000000: error',
  212. )
  213. })
  214. it('should log an error if deleting Revisions from secondary database fails upon integrity check failure', async () => {
  215. const secondaryRevision2WithDifferentContent = Revision.create({
  216. ...secondaryRevision2.props,
  217. content: 'different-content',
  218. }).getValue()
  219. ;(secondaryRevisionRepository as RevisionRepositoryInterface).findOneByUuid = jest
  220. .fn()
  221. .mockResolvedValueOnce(secondaryRevision1)
  222. .mockResolvedValueOnce(secondaryRevision2WithDifferentContent)
  223. ;(secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid = jest
  224. .fn()
  225. .mockRejectedValue(new Error('error'))
  226. const useCase = createUseCase()
  227. const result = await useCase.execute({
  228. userUuid: '00000000-0000-0000-0000-000000000000',
  229. })
  230. expect(result.isFailed()).toBeTruthy()
  231. expect(logger.error).toHaveBeenCalledTimes(1)
  232. expect(logger.error).toHaveBeenCalledWith(
  233. 'Failed to clean up secondary database revisions for user 00000000-0000-0000-0000-000000000000: error',
  234. )
  235. })
  236. it('should not perform the transition if secondary Revision repository is not set', async () => {
  237. secondaryRevisionRepository = null
  238. const useCase = createUseCase()
  239. const result = await useCase.execute({
  240. userUuid: '00000000-0000-0000-0000-000000000000',
  241. })
  242. expect(result.isFailed()).toBeTruthy()
  243. expect(result.getError()).toEqual('Secondary revision repository is not set')
  244. expect(primaryRevisionRepository.countByUserUuid).not.toHaveBeenCalled()
  245. expect(primaryRevisionRepository.findByUserUuid).not.toHaveBeenCalled()
  246. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  247. })
  248. it('should not perform the transition if the user uuid is invalid', async () => {
  249. const useCase = createUseCase()
  250. const result = await useCase.execute({
  251. userUuid: 'invalid-uuid',
  252. })
  253. expect(result.isFailed()).toBeTruthy()
  254. expect(result.getError()).toEqual('Given value is not a valid uuid: invalid-uuid')
  255. expect(primaryRevisionRepository.countByUserUuid).not.toHaveBeenCalled()
  256. expect(primaryRevisionRepository.findByUserUuid).not.toHaveBeenCalled()
  257. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  258. })
  259. it('should fail integrity check if the Revision count is not the same in both databases', async () => {
  260. ;(secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid = jest.fn().mockResolvedValue(1)
  261. const useCase = createUseCase()
  262. const result = await useCase.execute({
  263. userUuid: '00000000-0000-0000-0000-000000000000',
  264. })
  265. expect(result.isFailed()).toBeTruthy()
  266. expect(result.getError()).toEqual(
  267. 'Total revisions count for user 00000000-0000-0000-0000-000000000000 in primary database (2) does not match total revisions count in secondary database (1)',
  268. )
  269. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledTimes(2)
  270. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledWith(
  271. Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  272. )
  273. expect((secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid).toHaveBeenCalledTimes(1)
  274. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  275. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  276. })
  277. it('should fail if one Revision is not found in the secondary database', async () => {
  278. ;(secondaryRevisionRepository as RevisionRepositoryInterface).findOneByUuid = jest
  279. .fn()
  280. .mockResolvedValueOnce(secondaryRevision1)
  281. .mockResolvedValueOnce(null)
  282. const useCase = createUseCase()
  283. const result = await useCase.execute({
  284. userUuid: '00000000-0000-0000-0000-000000000000',
  285. })
  286. expect(result.isFailed()).toBeTruthy()
  287. expect(result.getError()).toEqual('Revision 00000000-0000-0000-0000-000000000001 not found in secondary database')
  288. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledTimes(2)
  289. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledWith(
  290. Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  291. )
  292. expect((secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid).toHaveBeenCalledTimes(1)
  293. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  294. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  295. })
  296. it('should fail if an error is thrown during integrity check between primary and secondary database', async () => {
  297. ;(secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid = jest
  298. .fn()
  299. .mockRejectedValue(new Error('error'))
  300. const useCase = createUseCase()
  301. const result = await useCase.execute({
  302. userUuid: '00000000-0000-0000-0000-000000000000',
  303. })
  304. expect(result.isFailed()).toBeTruthy()
  305. expect(result.getError()).toEqual('error')
  306. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  307. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  308. })
  309. })
  310. })