TpdsUpdateSenderTests.js 12 KB

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