LinkedFilesControllerTests.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { expect } from 'chai'
  2. import esmock from 'esmock'
  3. import sinon from 'sinon'
  4. const modulePath =
  5. '../../../../app/src/Features/LinkedFiles/LinkedFilesController.mjs'
  6. describe('LinkedFilesController', function () {
  7. beforeEach(function () {
  8. this.fakeTime = new Date()
  9. this.clock = sinon.useFakeTimers(this.fakeTime.getTime())
  10. })
  11. afterEach(function () {
  12. this.clock.restore()
  13. })
  14. beforeEach(async function () {
  15. this.userId = 'user-id'
  16. this.Agent = {
  17. promises: {
  18. createLinkedFile: sinon.stub().resolves(),
  19. refreshLinkedFile: sinon.stub().resolves(),
  20. },
  21. }
  22. this.projectId = 'projectId'
  23. this.provider = 'provider'
  24. this.name = 'linked-file-name'
  25. this.data = { customAgentData: 'foo' }
  26. this.LinkedFilesHandler = {
  27. promises: {
  28. getFileById: sinon.stub(),
  29. },
  30. }
  31. this.AnalyticsManager = {}
  32. this.SessionManager = {
  33. getLoggedInUserId: sinon.stub().returns(this.userId),
  34. }
  35. this.EditorRealTimeController = {}
  36. this.ReferencesHandler = {}
  37. this.UrlAgent = {}
  38. this.ProjectFileAgent = {}
  39. this.ProjectOutputFileAgent = {}
  40. this.EditorController = {}
  41. this.ProjectLocator = {}
  42. this.logger = {
  43. error: sinon.stub(),
  44. }
  45. this.settings = { enabledLinkedFileTypes: [] }
  46. this.LinkedFilesController = await esmock.strict(modulePath, {
  47. '.../../../../app/src/Features/Authentication/SessionManager':
  48. this.SessionManager,
  49. '../../../../app/src/Features/Analytics/AnalyticsManager':
  50. this.AnalyticsManager,
  51. '../../../../app/src/Features/LinkedFiles/LinkedFilesHandler':
  52. this.LinkedFilesHandler,
  53. '../../../../app/src/Features/Editor/EditorRealTimeController':
  54. this.EditorRealTimeController,
  55. '../../../../app/src/Features/References/ReferencesHandler':
  56. this.ReferencesHandler,
  57. '../../../../app/src/Features/LinkedFiles/UrlAgent': this.UrlAgent,
  58. '../../../../app/src/Features/LinkedFiles/ProjectFileAgent':
  59. this.ProjectFileAgent,
  60. '../../../../app/src/Features/LinkedFiles/ProjectOutputFileAgent':
  61. this.ProjectOutputFileAgent,
  62. '../../../../app/src/Features/Editor/EditorController':
  63. this.EditorController,
  64. '../../../../app/src/Features/Project/ProjectLocator':
  65. this.ProjectLocator,
  66. '@overleaf/logger': this.logger,
  67. '@overleaf/settings': this.settings,
  68. })
  69. this.LinkedFilesController._getAgent = sinon.stub().resolves(this.Agent)
  70. })
  71. describe('createLinkedFile', function () {
  72. beforeEach(function () {
  73. this.req = {
  74. params: { project_id: this.projectId },
  75. body: {
  76. name: this.name,
  77. provider: this.provider,
  78. data: this.data,
  79. },
  80. }
  81. this.next = sinon.stub()
  82. })
  83. it('sets importedAt timestamp on linkedFileData', function (done) {
  84. this.next = sinon.stub().callsFake(() => done('unexpected error'))
  85. this.res = {
  86. json: () => {
  87. expect(this.Agent.promises.createLinkedFile).to.have.been.calledWith(
  88. this.projectId,
  89. { ...this.data, importedAt: this.fakeTime.toISOString() },
  90. this.name,
  91. undefined,
  92. this.userId
  93. )
  94. done()
  95. },
  96. }
  97. this.LinkedFilesController.createLinkedFile(this.req, this.res, this.next)
  98. })
  99. })
  100. describe('refreshLinkedFiles', function () {
  101. beforeEach(function () {
  102. this.data.provider = this.provider
  103. this.file = {
  104. name: this.name,
  105. linkedFileData: {
  106. ...this.data,
  107. importedAt: new Date(2020, 1, 1).toISOString(),
  108. },
  109. }
  110. this.LinkedFilesHandler.promises.getFileById
  111. .withArgs(this.projectId, 'file-id')
  112. .resolves({
  113. file: this.file,
  114. path: 'fake-path',
  115. parentFolder: {
  116. _id: 'parent-folder-id',
  117. },
  118. })
  119. this.req = {
  120. params: { project_id: this.projectId, file_id: 'file-id' },
  121. body: {},
  122. }
  123. this.next = sinon.stub()
  124. })
  125. it('resets importedAt timestamp on linkedFileData', function (done) {
  126. this.next = sinon.stub().callsFake(() => done('unexpected error'))
  127. this.res = {
  128. json: () => {
  129. expect(this.Agent.promises.refreshLinkedFile).to.have.been.calledWith(
  130. this.projectId,
  131. {
  132. ...this.data,
  133. importedAt: this.fakeTime.toISOString(),
  134. },
  135. this.name,
  136. 'parent-folder-id',
  137. this.userId
  138. )
  139. done()
  140. },
  141. }
  142. this.LinkedFilesController.refreshLinkedFile(
  143. this.req,
  144. this.res,
  145. this.next
  146. )
  147. })
  148. })
  149. })