TpdsUpdateSenderTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. const { ObjectId } = require('../../../../app/src/infrastructure/mongojs')
  2. const SandboxedModule = require('sandboxed-module')
  3. const chai = require('chai')
  4. const path = require('path')
  5. const sinon = require('sinon')
  6. chai.should()
  7. const modulePath = path.join(
  8. __dirname,
  9. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateSender.js'
  10. )
  11. const projectId = 'project_id_here'
  12. const userId = ObjectId()
  13. const readOnlyRef = ObjectId()
  14. const collaberatorRef = ObjectId()
  15. const projectName = 'project_name_here'
  16. const thirdPartyDataStoreApiUrl = 'http://third-party-json-store.herokuapp.com'
  17. const httpUsername = 'user'
  18. const httpPass = 'pass'
  19. const siteUrl = 'http://www.localhost:3000'
  20. const httpAuthSiteUrl = `http://${httpUsername}:${httpPass}@www.localhost:3000`
  21. const filestoreUrl = 'filestore.sharelatex.com'
  22. describe('TpdsUpdateSender', function() {
  23. beforeEach(function() {
  24. this.fakeUser = {
  25. _id: '12390i'
  26. }
  27. this.requestQueuer = function(queue, meth, opts, callback) {}
  28. const memberIds = [userId, collaberatorRef, readOnlyRef]
  29. this.CollaboratorsGetter = {
  30. promises: {
  31. getInvitedMemberIds: sinon.stub().resolves(memberIds)
  32. }
  33. }
  34. this.docstoreUrl = 'docstore.sharelatex.env'
  35. this.request = sinon.stub().resolves()
  36. this.settings = {
  37. siteUrl,
  38. httpAuthSiteUrl,
  39. apis: {
  40. thirdPartyDataStore: { url: thirdPartyDataStoreApiUrl },
  41. filestore: {
  42. url: filestoreUrl
  43. },
  44. docstore: {
  45. pubUrl: this.docstoreUrl
  46. }
  47. }
  48. }
  49. const getUsers = sinon.stub().resolves(
  50. memberIds.slice(1).map(userId => {
  51. return { _id: userId }
  52. })
  53. )
  54. this.UserGetter = {
  55. promises: { getUsers }
  56. }
  57. this.updateSender = SandboxedModule.require(modulePath, {
  58. globals: {
  59. console: console
  60. },
  61. requires: {
  62. 'settings-sharelatex': this.settings,
  63. 'logger-sharelatex': { log() {} },
  64. 'request-promise-native': this.request,
  65. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  66. '../User/UserGetter.js': this.UserGetter,
  67. 'metrics-sharelatex': {
  68. inc() {}
  69. }
  70. }
  71. })
  72. })
  73. describe('enqueue', function() {
  74. it('should not call request if there is no tpdsworker url', async function() {
  75. await this.updateSender.promises.enqueue(null, null, null)
  76. this.request.should.not.have.been.called
  77. })
  78. it('should post the message to the tpdsworker', async function() {
  79. this.settings.apis.tpdsworker = { url: 'www.tpdsworker.env' }
  80. const group0 = 'myproject'
  81. const method0 = 'somemethod0'
  82. const job0 = 'do something'
  83. await this.updateSender.promises.enqueue(group0, method0, job0)
  84. const args = this.request.firstCall.args[0]
  85. args.json.group.should.equal(group0)
  86. args.json.job.should.equal(job0)
  87. args.json.method.should.equal(method0)
  88. args.uri.should.equal(
  89. 'www.tpdsworker.env/enqueue/web_to_tpds_http_requests'
  90. )
  91. })
  92. })
  93. describe('sending updates', function() {
  94. beforeEach(function() {
  95. this.settings.apis.tpdsworker = { url: 'www.tpdsworker.env' }
  96. })
  97. it('queues a post the file with user and file id', async function() {
  98. const fileId = '4545345'
  99. const path = '/some/path/here.jpg'
  100. await this.updateSender.promises.addFile({
  101. project_id: projectId,
  102. file_id: fileId,
  103. path,
  104. project_name: projectName
  105. })
  106. const {
  107. group: group0,
  108. job: job0,
  109. method: method0
  110. } = this.request.firstCall.args[0].json
  111. group0.should.equal(userId)
  112. method0.should.equal('pipeStreamFrom')
  113. job0.method.should.equal('post')
  114. job0.streamOrigin.should.equal(
  115. `${filestoreUrl}/project/${projectId}/file/${fileId}`
  116. )
  117. const expectedUrl = `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  118. projectName
  119. )}${encodeURIComponent(path)}`
  120. job0.uri.should.equal(expectedUrl)
  121. job0.headers.sl_all_user_ids.should.equal(JSON.stringify([userId]))
  122. job0.headers.sl_project_owner_user_id.should.equal(userId)
  123. const { group: group1, job: job1 } = this.request.secondCall.args[0].json
  124. group1.should.equal(collaberatorRef)
  125. job1.headers.sl_all_user_ids.should.equal(
  126. JSON.stringify([collaberatorRef])
  127. )
  128. job1.headers.sl_project_owner_user_id.should.equal(userId)
  129. const { group: group2, job: job2 } = this.request.thirdCall.args[0].json
  130. group2.should.equal(readOnlyRef)
  131. job2.headers.sl_all_user_ids.should.equal(JSON.stringify([readOnlyRef]))
  132. job2.headers.sl_project_owner_user_id.should.equal(userId)
  133. this.UserGetter.promises.getUsers.should.have.been.calledOnce.and.calledWith(
  134. {
  135. _id: {
  136. $in: [collaberatorRef, readOnlyRef]
  137. },
  138. 'dropbox.access_token.uid': { $ne: null }
  139. },
  140. { _id: 1 }
  141. )
  142. })
  143. it('post doc with stream origin of docstore', async function() {
  144. const docId = '4545345'
  145. const path = '/some/path/here.tex'
  146. const lines = ['line1', 'line2', 'line3']
  147. await this.updateSender.promises.addDoc({
  148. project_id: projectId,
  149. doc_id: docId,
  150. path,
  151. docLines: lines,
  152. project_name: projectName
  153. })
  154. const {
  155. group: group0,
  156. job: job0,
  157. method: method0
  158. } = this.request.firstCall.args[0].json
  159. group0.should.equal(userId)
  160. method0.should.equal('pipeStreamFrom')
  161. job0.method.should.equal('post')
  162. const expectedUrl = `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  163. projectName
  164. )}${encodeURIComponent(path)}`
  165. job0.uri.should.equal(expectedUrl)
  166. job0.streamOrigin.should.equal(
  167. `${this.docstoreUrl}/project/${projectId}/doc/${docId}/raw`
  168. )
  169. job0.headers.sl_all_user_ids.should.eql(JSON.stringify([userId]))
  170. const { group: group1, job: job1 } = this.request.secondCall.args[0].json
  171. group1.should.equal(collaberatorRef)
  172. job1.headers.sl_all_user_ids.should.equal(
  173. JSON.stringify([collaberatorRef])
  174. )
  175. const { group: group2, job: job2 } = this.request.thirdCall.args[0].json
  176. group2.should.equal(readOnlyRef)
  177. job2.headers.sl_all_user_ids.should.equal(JSON.stringify([readOnlyRef]))
  178. this.UserGetter.promises.getUsers.should.have.been.calledOnce.and.calledWith(
  179. {
  180. _id: {
  181. $in: [collaberatorRef, readOnlyRef]
  182. },
  183. 'dropbox.access_token.uid': { $ne: null }
  184. },
  185. { _id: 1 }
  186. )
  187. })
  188. it('deleting entity', async function() {
  189. const path = '/path/here/t.tex'
  190. await this.updateSender.promises.deleteEntity({
  191. project_id: projectId,
  192. path,
  193. project_name: projectName
  194. })
  195. const {
  196. group: group0,
  197. job: job0,
  198. method: method0
  199. } = this.request.firstCall.args[0].json
  200. group0.should.equal(userId)
  201. method0.should.equal('standardHttpRequest')
  202. job0.method.should.equal('delete')
  203. const expectedUrl = `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
  204. projectName
  205. )}${encodeURIComponent(path)}`
  206. job0.headers.sl_all_user_ids.should.eql(JSON.stringify([userId]))
  207. job0.uri.should.equal(expectedUrl)
  208. const { group: group1, job: job1 } = this.request.secondCall.args[0].json
  209. group1.should.equal(collaberatorRef)
  210. job1.headers.sl_all_user_ids.should.equal(
  211. JSON.stringify([collaberatorRef])
  212. )
  213. const { group: group2, job: job2 } = this.request.thirdCall.args[0].json
  214. group2.should.equal(readOnlyRef)
  215. job2.headers.sl_all_user_ids.should.equal(JSON.stringify([readOnlyRef]))
  216. this.UserGetter.promises.getUsers.should.have.been.calledOnce.and.calledWith(
  217. {
  218. _id: {
  219. $in: [collaberatorRef, readOnlyRef]
  220. },
  221. 'dropbox.access_token.uid': { $ne: null }
  222. },
  223. { _id: 1 }
  224. )
  225. })
  226. it('moving entity', async function() {
  227. const startPath = 'staring/here/file.tex'
  228. const endPath = 'ending/here/file.tex'
  229. await this.updateSender.promises.moveEntity({
  230. project_id: projectId,
  231. startPath,
  232. endPath,
  233. project_name: projectName
  234. })
  235. const {
  236. group: group0,
  237. job: job0,
  238. method: method0
  239. } = this.request.firstCall.args[0].json
  240. group0.should.equal(userId)
  241. method0.should.equal('standardHttpRequest')
  242. job0.method.should.equal('put')
  243. job0.uri.should.equal(
  244. `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`
  245. )
  246. job0.json.startPath.should.equal(`/${projectName}/${startPath}`)
  247. job0.json.endPath.should.equal(`/${projectName}/${endPath}`)
  248. job0.headers.sl_all_user_ids.should.eql(JSON.stringify([userId]))
  249. const { group: group1, job: job1 } = this.request.secondCall.args[0].json
  250. group1.should.equal(collaberatorRef)
  251. job1.headers.sl_all_user_ids.should.equal(
  252. JSON.stringify([collaberatorRef])
  253. )
  254. const { group: group2, job: job2 } = this.request.thirdCall.args[0].json
  255. group2.should.equal(readOnlyRef)
  256. job2.headers.sl_all_user_ids.should.equal(JSON.stringify([readOnlyRef]))
  257. this.UserGetter.promises.getUsers.should.have.been.calledOnce.and.calledWith(
  258. {
  259. _id: {
  260. $in: [collaberatorRef, readOnlyRef]
  261. },
  262. 'dropbox.access_token.uid': { $ne: null }
  263. },
  264. { _id: 1 }
  265. )
  266. })
  267. it('should be able to rename a project using the move entity func', async function() {
  268. const oldProjectName = '/oldProjectName/'
  269. const newProjectName = '/newProjectName/'
  270. await this.updateSender.promises.moveEntity({
  271. project_id: projectId,
  272. project_name: oldProjectName,
  273. newProjectName
  274. })
  275. const {
  276. group: group0,
  277. job: job0,
  278. method: method0
  279. } = this.request.firstCall.args[0].json
  280. group0.should.equal(userId)
  281. method0.should.equal('standardHttpRequest')
  282. job0.method.should.equal('put')
  283. job0.uri.should.equal(
  284. `${thirdPartyDataStoreApiUrl}/user/${userId}/entity`
  285. )
  286. job0.json.startPath.should.equal(oldProjectName)
  287. job0.json.endPath.should.equal(newProjectName)
  288. job0.headers.sl_all_user_ids.should.eql(JSON.stringify([userId]))
  289. const { group: group1, job: job1 } = this.request.secondCall.args[0].json
  290. group1.should.equal(collaberatorRef)
  291. job1.headers.sl_all_user_ids.should.equal(
  292. JSON.stringify([collaberatorRef])
  293. )
  294. const { group: group2, job: job2 } = this.request.thirdCall.args[0].json
  295. group2.should.equal(readOnlyRef)
  296. job2.headers.sl_all_user_ids.should.equal(JSON.stringify([readOnlyRef]))
  297. this.UserGetter.promises.getUsers.should.have.been.calledOnce.and.calledWith(
  298. {
  299. _id: {
  300. $in: [collaberatorRef, readOnlyRef]
  301. },
  302. 'dropbox.access_token.uid': { $ne: null }
  303. },
  304. { _id: 1 }
  305. )
  306. })
  307. it('pollDropboxForUser', async function() {
  308. await this.updateSender.promises.pollDropboxForUser(userId)
  309. const {
  310. group: group0,
  311. job: job0,
  312. method: method0
  313. } = this.request.firstCall.args[0].json
  314. group0.should.equal(`poll-dropbox:${userId}`)
  315. method0.should.equal('standardHttpRequest')
  316. job0.method.should.equal('post')
  317. job0.uri.should.equal(`${thirdPartyDataStoreApiUrl}/user/poll`)
  318. job0.json.user_ids[0].should.equal(userId)
  319. })
  320. })
  321. })