TpdsUpdateSenderTests.js 13 KB

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