TpdsUpdateSender.test.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. import path from 'node:path'
  4. import sinon from 'sinon'
  5. const { ObjectId } = mongodb
  6. const modulePath = path.join(
  7. import.meta.dirname,
  8. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateSender.mjs'
  9. )
  10. const projectId = 'project_id_here'
  11. const userId = new ObjectId()
  12. const readOnlyRef = new ObjectId()
  13. const collaberatorRef = new ObjectId()
  14. const projectName = 'project_name_here'
  15. const thirdPartyDataStoreApiUrl = 'http://third-party-json-store.herokuapp.com'
  16. const siteUrl = 'http://127.0.0.1:3000'
  17. const filestoreUrl = 'filestore.overleaf.com'
  18. const projectHistoryUrl = 'http://project-history:3054'
  19. describe('TpdsUpdateSender', function () {
  20. beforeEach(async function (ctx) {
  21. ctx.fakeUser = {
  22. _id: '12390i',
  23. }
  24. ctx.memberIds = [userId, collaberatorRef, readOnlyRef]
  25. ctx.enqueueUrl = new URL(
  26. 'http://tpdsworker/enqueue/web_to_tpds_http_requests'
  27. )
  28. ctx.CollaboratorsGetter = {
  29. promises: {
  30. getInvitedMemberIds: sinon.stub().resolves(ctx.memberIds),
  31. },
  32. }
  33. ctx.docstoreUrl = 'docstore.overleaf.env'
  34. ctx.response = {
  35. ok: true,
  36. json: sinon.stub(),
  37. }
  38. ctx.FetchUtils = {
  39. fetchNothing: sinon.stub().resolves(),
  40. }
  41. ctx.settings = {
  42. siteUrl,
  43. apis: {
  44. thirdPartyDataStore: { url: thirdPartyDataStoreApiUrl },
  45. filestore: {
  46. url: filestoreUrl,
  47. },
  48. docstore: {
  49. pubUrl: ctx.docstoreUrl,
  50. },
  51. project_history: {
  52. url: projectHistoryUrl,
  53. },
  54. },
  55. }
  56. const getUsers = sinon.stub()
  57. getUsers
  58. .withArgs({
  59. _id: {
  60. $in: ctx.memberIds,
  61. },
  62. 'dropbox.access_token.uid': { $ne: null },
  63. })
  64. .resolves(
  65. ctx.memberIds.map(userId => {
  66. return { _id: userId }
  67. })
  68. )
  69. ctx.UserGetter = {
  70. promises: { getUsers },
  71. }
  72. vi.doMock('mongodb-legacy', () => ({
  73. default: { ObjectId },
  74. }))
  75. vi.doMock('@overleaf/settings', () => ({
  76. default: ctx.settings,
  77. }))
  78. vi.doMock('@overleaf/fetch-utils', () => ctx.FetchUtils)
  79. vi.doMock(
  80. '../../../../app/src/Features/Collaborators/CollaboratorsGetter',
  81. () => ({
  82. default: ctx.CollaboratorsGetter,
  83. })
  84. )
  85. vi.doMock('../../../../app/src/Features/User/UserGetter.mjs', () => ({
  86. default: ctx.UserGetter,
  87. }))
  88. ctx.TpdsUpdateSender = (await import(modulePath)).default
  89. })
  90. describe('enqueue', function () {
  91. it('should not call request if there is no tpdsworker url', async function (ctx) {
  92. await ctx.TpdsUpdateSender.promises.enqueue(null, null, null)
  93. ctx.FetchUtils.fetchNothing.should.not.have.been.called
  94. })
  95. it('should post the message to the tpdsworker', async function (ctx) {
  96. ctx.settings.apis.tpdsworker = { url: 'http://tpdsworker' }
  97. const group0 = 'myproject'
  98. const method0 = 'somemethod0'
  99. const job0 = 'do something'
  100. await ctx.TpdsUpdateSender.promises.enqueue(group0, method0, job0)
  101. ctx.FetchUtils.fetchNothing.should.have.been.calledWithMatch(
  102. ctx.enqueueUrl,
  103. {
  104. method: 'POST',
  105. json: { group: group0, job: job0, method: method0 },
  106. }
  107. )
  108. })
  109. })
  110. describe('sending updates', function () {
  111. beforeEach(function (ctx) {
  112. ctx.settings.apis.tpdsworker = { url: 'http://tpdsworker' }
  113. })
  114. it('queues a post the file with user and file id and hash', async function (ctx) {
  115. const fileId = '4545345'
  116. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  117. const historyId = 91525
  118. const path = '/some/path/here.jpg'
  119. await ctx.TpdsUpdateSender.promises.addFile({
  120. projectId,
  121. historyId,
  122. fileId,
  123. hash,
  124. path,
  125. projectName,
  126. })
  127. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  128. ctx.enqueueUrl,
  129. {
  130. json: {
  131. group: userId,
  132. method: 'pipeStreamFrom',
  133. job: {
  134. method: 'post',
  135. streamOrigin: `${projectHistoryUrl}/project/${historyId}/blob/${hash}`,
  136. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  137. projectName
  138. )}${encodeURIComponent(path)}`,
  139. headers: {},
  140. },
  141. },
  142. }
  143. )
  144. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  145. ctx.enqueueUrl,
  146. {
  147. json: {
  148. group: collaberatorRef,
  149. },
  150. }
  151. )
  152. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  153. ctx.enqueueUrl,
  154. {
  155. json: {
  156. group: readOnlyRef,
  157. job: {},
  158. },
  159. }
  160. )
  161. })
  162. it('post doc with stream origin of docstore', async function (ctx) {
  163. const docId = '4545345'
  164. const path = '/some/path/here.tex'
  165. const lines = ['line1', 'line2', 'line3']
  166. await ctx.TpdsUpdateSender.promises.addDoc({
  167. projectId,
  168. docId,
  169. path,
  170. docLines: lines,
  171. projectName,
  172. })
  173. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  174. ctx.enqueueUrl,
  175. {
  176. json: {
  177. group: userId,
  178. method: 'pipeStreamFrom',
  179. job: {
  180. method: 'post',
  181. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  182. projectName
  183. )}${encodeURIComponent(path)}`,
  184. streamOrigin: `${ctx.docstoreUrl}/project/${projectId}/doc/${docId}/raw`,
  185. headers: {},
  186. },
  187. },
  188. }
  189. )
  190. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  191. ctx.enqueueUrl,
  192. {
  193. json: {
  194. group: collaberatorRef,
  195. job: {
  196. headers: {},
  197. },
  198. },
  199. }
  200. )
  201. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  202. ctx.enqueueUrl,
  203. {
  204. json: {
  205. group: readOnlyRef,
  206. job: {
  207. headers: {},
  208. },
  209. },
  210. }
  211. )
  212. })
  213. it('deleting entity', async function (ctx) {
  214. const path = '/path/here/t.tex'
  215. const subtreeEntityIds = ['id1', 'id2']
  216. await ctx.TpdsUpdateSender.promises.deleteEntity({
  217. projectId,
  218. path,
  219. projectName,
  220. subtreeEntityIds,
  221. })
  222. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  223. ctx.enqueueUrl,
  224. {
  225. json: {
  226. group: userId,
  227. method: 'standardHttpRequest',
  228. job: {
  229. method: 'delete',
  230. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  231. projectName
  232. )}${encodeURIComponent(path)}`,
  233. headers: {},
  234. json: { subtreeEntityIds },
  235. },
  236. },
  237. }
  238. )
  239. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  240. ctx.enqueueUrl,
  241. {
  242. json: {
  243. group: collaberatorRef,
  244. job: {
  245. headers: {},
  246. },
  247. },
  248. }
  249. )
  250. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  251. ctx.enqueueUrl,
  252. {
  253. json: {
  254. group: readOnlyRef,
  255. job: {
  256. headers: {},
  257. },
  258. },
  259. }
  260. )
  261. })
  262. it('moving entity', async function (ctx) {
  263. const startPath = 'staring/here/file.tex'
  264. const endPath = 'ending/here/file.tex'
  265. await ctx.TpdsUpdateSender.promises.moveEntity({
  266. projectId,
  267. startPath,
  268. endPath,
  269. projectName,
  270. })
  271. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  272. ctx.enqueueUrl,
  273. {
  274. json: {
  275. group: userId,
  276. method: 'standardHttpRequest',
  277. job: {
  278. method: 'put',
  279. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`,
  280. json: {
  281. startPath: `/${projectName}/${startPath}`,
  282. endPath: `/${projectName}/${endPath}`,
  283. },
  284. headers: {},
  285. },
  286. },
  287. }
  288. )
  289. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  290. ctx.enqueueUrl,
  291. {
  292. json: {
  293. group: collaberatorRef,
  294. job: {
  295. headers: {},
  296. },
  297. },
  298. }
  299. )
  300. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  301. ctx.enqueueUrl,
  302. {
  303. json: {
  304. group: readOnlyRef,
  305. job: {
  306. headers: {},
  307. },
  308. },
  309. }
  310. )
  311. })
  312. it('should be able to rename a project using the move entity func', async function (ctx) {
  313. const oldProjectName = '/oldProjectName/'
  314. const newProjectName = '/newProjectName/'
  315. await ctx.TpdsUpdateSender.promises.moveEntity({
  316. projectId,
  317. projectName: oldProjectName,
  318. newProjectName,
  319. })
  320. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  321. ctx.enqueueUrl,
  322. {
  323. json: {
  324. group: userId,
  325. method: 'standardHttpRequest',
  326. job: {
  327. method: 'put',
  328. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`,
  329. json: {
  330. startPath: oldProjectName,
  331. endPath: newProjectName,
  332. },
  333. headers: {},
  334. },
  335. },
  336. }
  337. )
  338. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  339. ctx.enqueueUrl,
  340. {
  341. json: {
  342. group: collaberatorRef,
  343. job: {
  344. headers: {},
  345. },
  346. },
  347. }
  348. )
  349. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  350. ctx.enqueueUrl,
  351. {
  352. json: {
  353. group: readOnlyRef,
  354. job: {
  355. headers: {},
  356. },
  357. },
  358. }
  359. )
  360. })
  361. it('pollDropboxForUser', async function (ctx) {
  362. await ctx.TpdsUpdateSender.promises.pollDropboxForUser(userId)
  363. expect(ctx.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  364. ctx.enqueueUrl,
  365. {
  366. json: {
  367. group: userId,
  368. method: 'standardHttpRequest',
  369. job: {
  370. method: 'post',
  371. uri: `${thirdPartyDataStoreApiUrl}/user/poll`,
  372. json: {
  373. user_ids: [userId],
  374. },
  375. },
  376. },
  377. }
  378. )
  379. })
  380. })
  381. describe('user not linked to dropbox', function () {
  382. beforeEach(function (ctx) {
  383. ctx.UserGetter.promises.getUsers
  384. .withArgs({
  385. _id: {
  386. $in: ctx.memberIds,
  387. },
  388. 'dropbox.access_token.uid': { $ne: null },
  389. })
  390. .resolves([])
  391. })
  392. it('does not make request to tpds', async function (ctx) {
  393. const fileId = '4545345'
  394. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  395. const historyId = 91525
  396. const path = '/some/path/here.jpg'
  397. await ctx.TpdsUpdateSender.promises.addFile({
  398. projectId,
  399. historyId,
  400. hash,
  401. fileId,
  402. path,
  403. projectName,
  404. })
  405. ctx.FetchUtils.fetchNothing.should.not.have.been.called
  406. })
  407. })
  408. })