TpdsUpdateSenderTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. }
  56. const getUsers = sinon.stub()
  57. getUsers
  58. .withArgs({
  59. _id: {
  60. $in: this.memberIds,
  61. },
  62. 'dropbox.access_token.uid': { $ne: null },
  63. })
  64. .resolves(
  65. this.memberIds.map(userId => {
  66. return { _id: userId }
  67. })
  68. )
  69. this.UserGetter = {
  70. promises: { getUsers },
  71. }
  72. this.TpdsUpdateSender = SandboxedModule.require(modulePath, {
  73. requires: {
  74. 'mongodb-legacy': { ObjectId },
  75. '@overleaf/settings': this.settings,
  76. '@overleaf/fetch-utils': this.FetchUtils,
  77. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  78. '../User/UserGetter.js': this.UserGetter,
  79. '@overleaf/metrics': {
  80. inc() {},
  81. },
  82. },
  83. })
  84. })
  85. describe('enqueue', function () {
  86. it('should not call request if there is no tpdsworker url', async function () {
  87. await this.TpdsUpdateSender.promises.enqueue(null, null, null)
  88. this.FetchUtils.fetchNothing.should.not.have.been.called
  89. })
  90. it('should post the message to the tpdsworker', async function () {
  91. this.settings.apis.tpdsworker = { url: 'http://tpdsworker' }
  92. const group0 = 'myproject'
  93. const method0 = 'somemethod0'
  94. const job0 = 'do something'
  95. await this.TpdsUpdateSender.promises.enqueue(group0, method0, job0)
  96. this.FetchUtils.fetchNothing.should.have.been.calledWithMatch(
  97. this.enqueueUrl,
  98. {
  99. method: 'POST',
  100. json: { group: group0, job: job0, method: method0 },
  101. }
  102. )
  103. })
  104. })
  105. describe('sending updates', function () {
  106. beforeEach(function () {
  107. this.settings.apis.tpdsworker = { url: 'http://tpdsworker' }
  108. })
  109. it('queues a post the file with user and file id', async function () {
  110. const fileId = '4545345'
  111. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  112. const historyId = 91525
  113. const path = '/some/path/here.jpg'
  114. await this.TpdsUpdateSender.promises.addFile({
  115. projectId,
  116. historyId,
  117. fileId,
  118. hash,
  119. path,
  120. projectName,
  121. })
  122. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  123. this.enqueueUrl,
  124. {
  125. json: {
  126. group: userId,
  127. method: 'pipeStreamFrom',
  128. job: {
  129. method: 'post',
  130. streamOrigin: `${projectHistoryUrl}/project/${historyId}/blob/${hash}`,
  131. streamFallback: `${filestoreUrl}/project/${projectId}/file/${fileId}`,
  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('post doc with stream origin of docstore', async function () {
  159. const docId = '4545345'
  160. const path = '/some/path/here.tex'
  161. const lines = ['line1', 'line2', 'line3']
  162. await this.TpdsUpdateSender.promises.addDoc({
  163. projectId,
  164. docId,
  165. path,
  166. docLines: lines,
  167. projectName,
  168. })
  169. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  170. this.enqueueUrl,
  171. {
  172. json: {
  173. group: userId,
  174. method: 'pipeStreamFrom',
  175. job: {
  176. method: 'post',
  177. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  178. projectName
  179. )}${encodeURIComponent(path)}`,
  180. streamOrigin: `${this.docstoreUrl}/project/${projectId}/doc/${docId}/raw`,
  181. headers: {},
  182. },
  183. },
  184. }
  185. )
  186. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  187. this.enqueueUrl,
  188. {
  189. json: {
  190. group: collaberatorRef,
  191. job: {
  192. headers: {},
  193. },
  194. },
  195. }
  196. )
  197. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  198. this.enqueueUrl,
  199. {
  200. json: {
  201. group: readOnlyRef,
  202. job: {
  203. headers: {},
  204. },
  205. },
  206. }
  207. )
  208. })
  209. it('deleting entity', async function () {
  210. const path = '/path/here/t.tex'
  211. const subtreeEntityIds = ['id1', 'id2']
  212. await this.TpdsUpdateSender.promises.deleteEntity({
  213. projectId,
  214. path,
  215. projectName,
  216. subtreeEntityIds,
  217. })
  218. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  219. this.enqueueUrl,
  220. {
  221. json: {
  222. group: userId,
  223. method: 'standardHttpRequest',
  224. job: {
  225. method: 'delete',
  226. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  227. projectName
  228. )}${encodeURIComponent(path)}`,
  229. headers: {},
  230. json: { subtreeEntityIds },
  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('moving entity', async function () {
  259. const startPath = 'staring/here/file.tex'
  260. const endPath = 'ending/here/file.tex'
  261. await this.TpdsUpdateSender.promises.moveEntity({
  262. projectId,
  263. startPath,
  264. endPath,
  265. projectName,
  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: 'put',
  275. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`,
  276. json: {
  277. startPath: `/${projectName}/${startPath}`,
  278. endPath: `/${projectName}/${endPath}`,
  279. },
  280. headers: {},
  281. },
  282. },
  283. }
  284. )
  285. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  286. this.enqueueUrl,
  287. {
  288. json: {
  289. group: collaberatorRef,
  290. job: {
  291. headers: {},
  292. },
  293. },
  294. }
  295. )
  296. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  297. this.enqueueUrl,
  298. {
  299. json: {
  300. group: readOnlyRef,
  301. job: {
  302. headers: {},
  303. },
  304. },
  305. }
  306. )
  307. })
  308. it('should be able to rename a project using the move entity func', async function () {
  309. const oldProjectName = '/oldProjectName/'
  310. const newProjectName = '/newProjectName/'
  311. await this.TpdsUpdateSender.promises.moveEntity({
  312. projectId,
  313. projectName: oldProjectName,
  314. newProjectName,
  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: oldProjectName,
  327. endPath: newProjectName,
  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('pollDropboxForUser', async function () {
  358. await this.TpdsUpdateSender.promises.pollDropboxForUser(userId)
  359. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  360. this.enqueueUrl,
  361. {
  362. json: {
  363. group: userId,
  364. method: 'standardHttpRequest',
  365. job: {
  366. method: 'post',
  367. uri: `${thirdPartyDataStoreApiUrl}/user/poll`,
  368. json: {
  369. user_ids: [userId],
  370. },
  371. },
  372. },
  373. }
  374. )
  375. })
  376. })
  377. describe('user not linked to dropbox', function () {
  378. beforeEach(function () {
  379. this.UserGetter.promises.getUsers
  380. .withArgs({
  381. _id: {
  382. $in: this.memberIds,
  383. },
  384. 'dropbox.access_token.uid': { $ne: null },
  385. })
  386. .resolves([])
  387. })
  388. it('does not make request to tpds', async function () {
  389. const fileId = '4545345'
  390. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  391. const historyId = 91525
  392. const path = '/some/path/here.jpg'
  393. await this.TpdsUpdateSender.promises.addFile({
  394. projectId,
  395. historyId,
  396. hash,
  397. fileId,
  398. path,
  399. projectName,
  400. })
  401. this.FetchUtils.fetchNothing.should.not.have.been.called
  402. })
  403. })
  404. })