TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.insert = jest.fn().mockResolvedValue(true)
  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.sleep = jest.fn()
  102. timer.getTimestampInMicroseconds = jest.fn().mockReturnValue(123)
  103. timer.convertMicrosecondsToTimeStructure = jest.fn().mockReturnValue({
  104. days: 0,
  105. hours: 0,
  106. minutes: 0,
  107. seconds: 0,
  108. milliseconds: 0,
  109. })
  110. })
  111. describe('successfull transition', () => {
  112. it('should transition Revisions from primary to secondary database', async () => {
  113. const useCase = createUseCase()
  114. const result = await useCase.execute({
  115. userUuid: '00000000-0000-0000-0000-000000000000',
  116. })
  117. expect(result.isFailed()).toBeFalsy()
  118. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledTimes(2)
  119. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledWith(
  120. Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  121. )
  122. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenCalledTimes(4)
  123. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(1, {
  124. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  125. limit: 1,
  126. offset: 0,
  127. })
  128. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(2, {
  129. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  130. limit: 1,
  131. offset: 1,
  132. })
  133. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(3, {
  134. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  135. limit: 1,
  136. offset: 0,
  137. })
  138. expect(primaryRevisionRepository.findByUserUuid).toHaveBeenNthCalledWith(4, {
  139. userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  140. limit: 1,
  141. offset: 1,
  142. })
  143. expect((secondaryRevisionRepository as RevisionRepositoryInterface).insert).toHaveBeenCalledTimes(2)
  144. expect((secondaryRevisionRepository as RevisionRepositoryInterface).insert).toHaveBeenCalledWith(primaryRevision1)
  145. expect((secondaryRevisionRepository as RevisionRepositoryInterface).insert).toHaveBeenCalledWith(primaryRevision2)
  146. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).not.toHaveBeenCalled()
  147. expect(primaryRevisionRepository.removeByUserUuid).toHaveBeenCalledTimes(1)
  148. })
  149. it('should log an error if deleting Revisions from primary database fails', async () => {
  150. primaryRevisionRepository.removeByUserUuid = jest.fn().mockRejectedValue(new Error('error'))
  151. const useCase = createUseCase()
  152. const result = await useCase.execute({
  153. userUuid: '00000000-0000-0000-0000-000000000000',
  154. })
  155. expect(result.isFailed()).toBeFalsy()
  156. expect(logger.error).toHaveBeenCalledTimes(1)
  157. expect(logger.error).toHaveBeenCalledWith(
  158. 'Failed to clean up primary database revisions for user 00000000-0000-0000-0000-000000000000: Errored when deleting revisions for user 00000000-0000-0000-0000-000000000000: error',
  159. )
  160. })
  161. })
  162. describe('failed transition', () => {
  163. it('should remove Revisions from secondary database if integrity check fails', async () => {
  164. const secondaryRevision2WithDifferentContent = Revision.create({
  165. ...secondaryRevision2.props,
  166. content: 'different-content',
  167. }).getValue()
  168. ;(secondaryRevisionRepository as RevisionRepositoryInterface).findOneByUuid = jest
  169. .fn()
  170. .mockResolvedValueOnce(secondaryRevision1)
  171. .mockResolvedValueOnce(secondaryRevision2WithDifferentContent)
  172. const useCase = createUseCase()
  173. const result = await useCase.execute({
  174. userUuid: '00000000-0000-0000-0000-000000000000',
  175. })
  176. expect(result.isFailed()).toBeTruthy()
  177. expect(result.getError()).toEqual(
  178. 'Revision 00000000-0000-0000-0000-000000000001 is not identical in primary and secondary database',
  179. )
  180. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  181. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  182. })
  183. it('should remove Revisions from secondary database if migrating Revisions fails', async () => {
  184. primaryRevisionRepository.findByUserUuid = jest
  185. .fn()
  186. .mockResolvedValueOnce([primaryRevision1])
  187. .mockRejectedValueOnce(new Error('error'))
  188. const useCase = createUseCase()
  189. const result = await useCase.execute({
  190. userUuid: '00000000-0000-0000-0000-000000000000',
  191. })
  192. expect(result.isFailed()).toBeTruthy()
  193. expect(result.getError()).toEqual(
  194. 'Errored when migrating revisions for user 00000000-0000-0000-0000-000000000000: error',
  195. )
  196. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  197. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  198. })
  199. it('should return an error for a specific revision if it errors when saving to secondary database', async () => {
  200. ;(secondaryRevisionRepository as RevisionRepositoryInterface).insert = jest
  201. .fn()
  202. .mockResolvedValueOnce(true)
  203. .mockRejectedValueOnce(new Error('error'))
  204. const useCase = createUseCase()
  205. const result = await useCase.execute({
  206. userUuid: '84c0f8e8-544a-4c7e-9adf-26209303bc1d',
  207. })
  208. expect(result.isFailed()).toBeTruthy()
  209. expect(result.getError()).toEqual(
  210. 'Errored when saving revision 00000000-0000-0000-0000-000000000001 to secondary database: error',
  211. )
  212. })
  213. it('should log an error if deleting Revisions from secondary database fails upon migration failure', async () => {
  214. primaryRevisionRepository.findByUserUuid = jest
  215. .fn()
  216. .mockResolvedValueOnce([primaryRevision1])
  217. .mockRejectedValueOnce(new Error('error'))
  218. ;(secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid = jest
  219. .fn()
  220. .mockRejectedValue(new Error('error'))
  221. const useCase = createUseCase()
  222. const result = await useCase.execute({
  223. userUuid: '00000000-0000-0000-0000-000000000000',
  224. })
  225. expect(result.isFailed()).toBeTruthy()
  226. expect(logger.error).toHaveBeenCalledTimes(1)
  227. expect(logger.error).toHaveBeenCalledWith(
  228. 'Failed to clean up secondary database revisions for user 00000000-0000-0000-0000-000000000000: Errored when deleting revisions for user 00000000-0000-0000-0000-000000000000: error',
  229. )
  230. })
  231. it('should log an error if deleting Revisions from secondary database fails upon integrity check failure', async () => {
  232. const secondaryRevision2WithDifferentContent = Revision.create({
  233. ...secondaryRevision2.props,
  234. content: 'different-content',
  235. }).getValue()
  236. ;(secondaryRevisionRepository as RevisionRepositoryInterface).findOneByUuid = jest
  237. .fn()
  238. .mockResolvedValueOnce(secondaryRevision1)
  239. .mockResolvedValueOnce(secondaryRevision2WithDifferentContent)
  240. ;(secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid = jest
  241. .fn()
  242. .mockRejectedValue(new Error('error'))
  243. const useCase = createUseCase()
  244. const result = await useCase.execute({
  245. userUuid: '00000000-0000-0000-0000-000000000000',
  246. })
  247. expect(result.isFailed()).toBeTruthy()
  248. expect(logger.error).toHaveBeenCalledTimes(1)
  249. expect(logger.error).toHaveBeenCalledWith(
  250. 'Failed to clean up secondary database revisions for user 00000000-0000-0000-0000-000000000000: Errored when deleting revisions for user 00000000-0000-0000-0000-000000000000: error',
  251. )
  252. })
  253. it('should not perform the transition if secondary Revision repository is not set', async () => {
  254. secondaryRevisionRepository = null
  255. const useCase = createUseCase()
  256. const result = await useCase.execute({
  257. userUuid: '00000000-0000-0000-0000-000000000000',
  258. })
  259. expect(result.isFailed()).toBeTruthy()
  260. expect(result.getError()).toEqual('Secondary revision repository is not set')
  261. expect(primaryRevisionRepository.countByUserUuid).not.toHaveBeenCalled()
  262. expect(primaryRevisionRepository.findByUserUuid).not.toHaveBeenCalled()
  263. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  264. })
  265. it('should not perform the transition if the user uuid is invalid', async () => {
  266. const useCase = createUseCase()
  267. const result = await useCase.execute({
  268. userUuid: 'invalid-uuid',
  269. })
  270. expect(result.isFailed()).toBeTruthy()
  271. expect(result.getError()).toEqual('Given value is not a valid uuid: invalid-uuid')
  272. expect(primaryRevisionRepository.countByUserUuid).not.toHaveBeenCalled()
  273. expect(primaryRevisionRepository.findByUserUuid).not.toHaveBeenCalled()
  274. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  275. })
  276. it('should fail integrity check if the Revision count is not the same in both databases', async () => {
  277. ;(secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid = jest.fn().mockResolvedValue(1)
  278. const useCase = createUseCase()
  279. const result = await useCase.execute({
  280. userUuid: '00000000-0000-0000-0000-000000000000',
  281. })
  282. expect(result.isFailed()).toBeTruthy()
  283. expect(result.getError()).toEqual(
  284. 'Total revisions count for user 00000000-0000-0000-0000-000000000000 in primary database (2) does not match total revisions count in secondary database (1)',
  285. )
  286. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledTimes(2)
  287. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledWith(
  288. Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  289. )
  290. expect((secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid).toHaveBeenCalledTimes(1)
  291. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  292. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  293. })
  294. it('should fail if one Revision is not found in the secondary database', async () => {
  295. ;(secondaryRevisionRepository as RevisionRepositoryInterface).findOneByUuid = jest
  296. .fn()
  297. .mockResolvedValueOnce(secondaryRevision1)
  298. .mockResolvedValueOnce(null)
  299. const useCase = createUseCase()
  300. const result = await useCase.execute({
  301. userUuid: '00000000-0000-0000-0000-000000000000',
  302. })
  303. expect(result.isFailed()).toBeTruthy()
  304. expect(result.getError()).toEqual('Revision 00000000-0000-0000-0000-000000000001 not found in secondary database')
  305. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledTimes(2)
  306. expect(primaryRevisionRepository.countByUserUuid).toHaveBeenCalledWith(
  307. Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
  308. )
  309. expect((secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid).not.toHaveBeenCalled()
  310. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  311. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  312. })
  313. it('should fail if an error is thrown during integrity check between primary and secondary database', async () => {
  314. ;(secondaryRevisionRepository as RevisionRepositoryInterface).countByUserUuid = jest
  315. .fn()
  316. .mockRejectedValue(new Error('error'))
  317. const useCase = createUseCase()
  318. const result = await useCase.execute({
  319. userUuid: '00000000-0000-0000-0000-000000000000',
  320. })
  321. expect(result.isFailed()).toBeTruthy()
  322. expect(result.getError()).toEqual('Errored when checking integrity between primary and secondary database: error')
  323. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  324. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  325. })
  326. it('should fail if a revisions did not save in the secondary database', async () => {
  327. ;(secondaryRevisionRepository as RevisionRepositoryInterface).insert = jest.fn().mockResolvedValue(false)
  328. const useCase = createUseCase()
  329. const result = await useCase.execute({
  330. userUuid: '00000000-0000-0000-0000-000000000000',
  331. })
  332. expect(result.isFailed()).toBeTruthy()
  333. expect(result.getError()).toEqual(
  334. 'Failed to save revision 00000000-0000-0000-0000-000000000000 to secondary database',
  335. )
  336. expect(primaryRevisionRepository.removeByUserUuid).not.toHaveBeenCalled()
  337. expect((secondaryRevisionRepository as RevisionRepositoryInterface).removeByUserUuid).toHaveBeenCalledTimes(1)
  338. })
  339. })
  340. })