TpdsUpdateSenderTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 and hash', 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. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  132. projectName
  133. )}${encodeURIComponent(path)}`,
  134. headers: {},
  135. },
  136. },
  137. }
  138. )
  139. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  140. this.enqueueUrl,
  141. {
  142. json: {
  143. group: collaberatorRef,
  144. },
  145. }
  146. )
  147. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  148. this.enqueueUrl,
  149. {
  150. json: {
  151. group: readOnlyRef,
  152. job: {},
  153. },
  154. }
  155. )
  156. })
  157. it('post doc with stream origin of docstore', async function () {
  158. const docId = '4545345'
  159. const path = '/some/path/here.tex'
  160. const lines = ['line1', 'line2', 'line3']
  161. await this.TpdsUpdateSender.promises.addDoc({
  162. projectId,
  163. docId,
  164. path,
  165. docLines: lines,
  166. projectName,
  167. })
  168. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  169. this.enqueueUrl,
  170. {
  171. json: {
  172. group: userId,
  173. method: 'pipeStreamFrom',
  174. job: {
  175. method: 'post',
  176. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  177. projectName
  178. )}${encodeURIComponent(path)}`,
  179. streamOrigin: `${this.docstoreUrl}/project/${projectId}/doc/${docId}/raw`,
  180. headers: {},
  181. },
  182. },
  183. }
  184. )
  185. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  186. this.enqueueUrl,
  187. {
  188. json: {
  189. group: collaberatorRef,
  190. job: {
  191. headers: {},
  192. },
  193. },
  194. }
  195. )
  196. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  197. this.enqueueUrl,
  198. {
  199. json: {
  200. group: readOnlyRef,
  201. job: {
  202. headers: {},
  203. },
  204. },
  205. }
  206. )
  207. })
  208. it('deleting entity', async function () {
  209. const path = '/path/here/t.tex'
  210. const subtreeEntityIds = ['id1', 'id2']
  211. await this.TpdsUpdateSender.promises.deleteEntity({
  212. projectId,
  213. path,
  214. projectName,
  215. subtreeEntityIds,
  216. })
  217. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  218. this.enqueueUrl,
  219. {
  220. json: {
  221. group: userId,
  222. method: 'standardHttpRequest',
  223. job: {
  224. method: 'delete',
  225. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  226. projectName
  227. )}${encodeURIComponent(path)}`,
  228. headers: {},
  229. json: { subtreeEntityIds },
  230. },
  231. },
  232. }
  233. )
  234. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  235. this.enqueueUrl,
  236. {
  237. json: {
  238. group: collaberatorRef,
  239. job: {
  240. headers: {},
  241. },
  242. },
  243. }
  244. )
  245. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  246. this.enqueueUrl,
  247. {
  248. json: {
  249. group: readOnlyRef,
  250. job: {
  251. headers: {},
  252. },
  253. },
  254. }
  255. )
  256. })
  257. it('moving entity', async function () {
  258. const startPath = 'staring/here/file.tex'
  259. const endPath = 'ending/here/file.tex'
  260. await this.TpdsUpdateSender.promises.moveEntity({
  261. projectId,
  262. startPath,
  263. endPath,
  264. projectName,
  265. })
  266. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  267. this.enqueueUrl,
  268. {
  269. json: {
  270. group: userId,
  271. method: 'standardHttpRequest',
  272. job: {
  273. method: 'put',
  274. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`,
  275. json: {
  276. startPath: `/${projectName}/${startPath}`,
  277. endPath: `/${projectName}/${endPath}`,
  278. },
  279. headers: {},
  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('should be able to rename a project using the move entity func', async function () {
  308. const oldProjectName = '/oldProjectName/'
  309. const newProjectName = '/newProjectName/'
  310. await this.TpdsUpdateSender.promises.moveEntity({
  311. projectId,
  312. projectName: oldProjectName,
  313. newProjectName,
  314. })
  315. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  316. this.enqueueUrl,
  317. {
  318. json: {
  319. group: userId,
  320. method: 'standardHttpRequest',
  321. job: {
  322. method: 'put',
  323. uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`,
  324. json: {
  325. startPath: oldProjectName,
  326. endPath: newProjectName,
  327. },
  328. headers: {},
  329. },
  330. },
  331. }
  332. )
  333. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  334. this.enqueueUrl,
  335. {
  336. json: {
  337. group: collaberatorRef,
  338. job: {
  339. headers: {},
  340. },
  341. },
  342. }
  343. )
  344. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  345. this.enqueueUrl,
  346. {
  347. json: {
  348. group: readOnlyRef,
  349. job: {
  350. headers: {},
  351. },
  352. },
  353. }
  354. )
  355. })
  356. it('pollDropboxForUser', async function () {
  357. await this.TpdsUpdateSender.promises.pollDropboxForUser(userId)
  358. expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
  359. this.enqueueUrl,
  360. {
  361. json: {
  362. group: userId,
  363. method: 'standardHttpRequest',
  364. job: {
  365. method: 'post',
  366. uri: `${thirdPartyDataStoreApiUrl}/user/poll`,
  367. json: {
  368. user_ids: [userId],
  369. },
  370. },
  371. },
  372. }
  373. )
  374. })
  375. })
  376. describe('user not linked to dropbox', function () {
  377. beforeEach(function () {
  378. this.UserGetter.promises.getUsers
  379. .withArgs({
  380. _id: {
  381. $in: this.memberIds,
  382. },
  383. 'dropbox.access_token.uid': { $ne: null },
  384. })
  385. .resolves([])
  386. })
  387. it('does not make request to tpds', async function () {
  388. const fileId = '4545345'
  389. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  390. const historyId = 91525
  391. const path = '/some/path/here.jpg'
  392. await this.TpdsUpdateSender.promises.addFile({
  393. projectId,
  394. historyId,
  395. hash,
  396. fileId,
  397. path,
  398. projectName,
  399. })
  400. this.FetchUtils.fetchNothing.should.not.have.been.called
  401. })
  402. })
  403. })