LinkedFilesControllerTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const sinon = require('sinon')
  4. const modulePath =
  5. '../../../../app/src/Features/LinkedFiles/LinkedFilesController'
  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(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. this.settings = { enabledLinkedFileTypes: [] }
  44. this.LinkedFilesController = SandboxedModule.require(modulePath, {
  45. requires: {
  46. '../Authentication/SessionManager': this.SessionManager,
  47. '../../../../app/src/Features/Analytics/AnalyticsManager':
  48. this.AnalyticsManager,
  49. './LinkedFilesHandler': this.LinkedFilesHandler,
  50. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  51. '../References/ReferencesHandler': this.ReferencesHandler,
  52. './UrlAgent': this.UrlAgent,
  53. './ProjectFileAgent': this.ProjectFileAgent,
  54. './ProjectOutputFileAgent': this.ProjectOutputFileAgent,
  55. '../Editor/EditorController': this.EditorController,
  56. '../Project/ProjectLocator': this.ProjectLocator,
  57. '@overleaf/logger': this.logger,
  58. '@overleaf/settings': this.settings,
  59. },
  60. })
  61. this.LinkedFilesController._getAgent = sinon.stub().returns(this.Agent)
  62. })
  63. describe('createLinkedFile', function () {
  64. beforeEach(function () {
  65. this.req = {
  66. params: { project_id: this.projectId },
  67. body: {
  68. name: this.name,
  69. provider: this.provider,
  70. data: this.data,
  71. },
  72. }
  73. this.next = sinon.stub()
  74. })
  75. it('sets importedAt timestamp on linkedFileData', function (done) {
  76. this.next = sinon.stub().callsFake(() => done('unexpected error'))
  77. this.res = {
  78. json: () => {
  79. expect(this.Agent.promises.createLinkedFile).to.have.been.calledWith(
  80. this.projectId,
  81. { ...this.data, importedAt: this.fakeTime.toISOString() },
  82. this.name,
  83. undefined,
  84. this.userId
  85. )
  86. done()
  87. },
  88. }
  89. this.LinkedFilesController.createLinkedFile(this.req, this.res, this.next)
  90. })
  91. })
  92. describe('refreshLinkedFiles', function () {
  93. beforeEach(function () {
  94. this.data.provider = this.provider
  95. this.file = {
  96. name: this.name,
  97. linkedFileData: {
  98. ...this.data,
  99. importedAt: new Date(2020, 1, 1).toISOString(),
  100. },
  101. }
  102. this.LinkedFilesHandler.promises.getFileById
  103. .withArgs(this.projectId, 'file-id')
  104. .resolves({
  105. file: this.file,
  106. path: 'fake-path',
  107. parentFolder: {
  108. _id: 'parent-folder-id',
  109. },
  110. })
  111. this.req = {
  112. params: { project_id: this.projectId, file_id: 'file-id' },
  113. body: {},
  114. }
  115. this.next = sinon.stub()
  116. })
  117. it('resets importedAt timestamp on linkedFileData', function (done) {
  118. this.next = sinon.stub().callsFake(() => done('unexpected error'))
  119. this.res = {
  120. json: () => {
  121. expect(this.Agent.promises.refreshLinkedFile).to.have.been.calledWith(
  122. this.projectId,
  123. {
  124. ...this.data,
  125. importedAt: this.fakeTime.toISOString(),
  126. },
  127. this.name,
  128. 'parent-folder-id',
  129. this.userId
  130. )
  131. done()
  132. },
  133. }
  134. this.LinkedFilesController.refreshLinkedFile(
  135. this.req,
  136. this.res,
  137. this.next
  138. )
  139. })
  140. })
  141. })