TpdsUpdateSenderTests.js 11 KB

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