TpdsController.test.mjs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. import { expect, vi } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. import sinon from 'sinon'
  4. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  5. import MockResponse from '../helpers/MockResponse.js'
  6. import MockRequest from '../helpers/MockRequest.js'
  7. const ObjectId = mongodb.ObjectId
  8. const MODULE_PATH =
  9. '../../../../app/src/Features/ThirdPartyDataStore/TpdsController.mjs'
  10. describe('TpdsController', function () {
  11. beforeEach(async function (ctx) {
  12. ctx.metadata = {
  13. projectId: new ObjectId(),
  14. entityId: new ObjectId(),
  15. folderId: new ObjectId(),
  16. entityType: 'doc',
  17. rev: 2,
  18. }
  19. ctx.TpdsUpdateHandler = {
  20. promises: {
  21. newUpdate: sinon.stub().resolves(ctx.metadata),
  22. deleteUpdate: sinon.stub().resolves(ctx.metadata.entityId),
  23. createFolder: sinon.stub().resolves(),
  24. },
  25. }
  26. ctx.UpdateMerger = {
  27. promises: {
  28. mergeUpdate: sinon.stub().resolves(ctx.metadata),
  29. deleteUpdate: sinon.stub().resolves(ctx.metadata.entityId),
  30. },
  31. }
  32. ctx.NotificationsBuilder = {
  33. tpdsFileLimit: sinon.stub().returns({ create: sinon.stub() }),
  34. }
  35. ctx.SessionManager = {
  36. getLoggedInUserId: sinon.stub().returns('user-id'),
  37. }
  38. ctx.TpdsQueueManager = {
  39. promises: {
  40. getQueues: sinon.stub().resolves('queues'),
  41. },
  42. }
  43. ctx.HttpErrorHandler = {
  44. conflict: sinon.stub(),
  45. }
  46. ctx.newProject = { _id: new ObjectId() }
  47. ctx.ProjectCreationHandler = {
  48. promises: { createBlankProject: sinon.stub().resolves(ctx.newProject) },
  49. }
  50. ctx.ProjectDetailsHandler = {
  51. promises: {
  52. generateUniqueName: sinon.stub().resolves('unique'),
  53. },
  54. }
  55. vi.doMock(
  56. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateHandler',
  57. () => ({
  58. default: ctx.TpdsUpdateHandler,
  59. })
  60. )
  61. vi.doMock(
  62. '../../../../app/src/Features/ThirdPartyDataStore/UpdateMerger',
  63. () => ({
  64. default: ctx.UpdateMerger,
  65. })
  66. )
  67. vi.doMock(
  68. '../../../../app/src/Features/Notifications/NotificationsBuilder',
  69. () => ({
  70. default: ctx.NotificationsBuilder,
  71. })
  72. )
  73. vi.doMock(
  74. '../../../../app/src/Features/Authentication/SessionManager',
  75. () => ({
  76. default: ctx.SessionManager,
  77. })
  78. )
  79. vi.doMock('../../../../app/src/Features/Errors/HttpErrorHandler', () => ({
  80. default: ctx.HttpErrorHandler,
  81. }))
  82. vi.doMock(
  83. '../../../../app/src/Features/ThirdPartyDataStore/TpdsQueueManager',
  84. () => ({
  85. default: ctx.TpdsQueueManager,
  86. })
  87. )
  88. vi.doMock(
  89. '../../../../app/src/Features/Project/ProjectCreationHandler',
  90. () => ({
  91. default: ctx.ProjectCreationHandler,
  92. })
  93. )
  94. vi.doMock(
  95. '../../../../app/src/Features/Project/ProjectDetailsHandler',
  96. () => ({
  97. default: ctx.ProjectDetailsHandler,
  98. })
  99. )
  100. ctx.TpdsController = (await import(MODULE_PATH)).default
  101. ctx.user_id = 'dsad29jlkjas'
  102. })
  103. describe('creating a project', function () {
  104. it('should yield the new projects id', function (ctx) {
  105. return new Promise(resolve => {
  106. const res = new MockResponse()
  107. const req = new MockRequest()
  108. req.params.user_id = ctx.user_id
  109. req.body = { projectName: 'foo' }
  110. res.callback = err => {
  111. if (err) resolve(err)
  112. expect(res.body).to.equal(
  113. JSON.stringify({ projectId: ctx.newProject._id.toString() })
  114. )
  115. expect(
  116. ctx.ProjectDetailsHandler.promises.generateUniqueName
  117. ).to.have.been.calledWith(ctx.user_id, 'foo')
  118. expect(
  119. ctx.ProjectCreationHandler.promises.createBlankProject
  120. ).to.have.been.calledWith(
  121. ctx.user_id,
  122. 'unique',
  123. {},
  124. { skipCreatingInTPDS: true }
  125. )
  126. resolve()
  127. }
  128. ctx.TpdsController.createProject(req, res)
  129. })
  130. })
  131. })
  132. describe('getting an update', function () {
  133. beforeEach(function (ctx) {
  134. ctx.projectName = 'projectName'
  135. ctx.path = '/here.txt'
  136. ctx.req = {
  137. params: {
  138. 0: `${ctx.projectName}${ctx.path}`,
  139. user_id: ctx.user_id,
  140. project_id: '',
  141. },
  142. headers: {
  143. 'x-update-source': (ctx.source = 'dropbox'),
  144. },
  145. }
  146. })
  147. it('should process the update with the update receiver by name', function (ctx) {
  148. return new Promise(resolve => {
  149. const res = {
  150. json: payload => {
  151. expect(payload).to.deep.equal({
  152. status: 'applied',
  153. projectId: ctx.metadata.projectId.toString(),
  154. entityId: ctx.metadata.entityId.toString(),
  155. folderId: ctx.metadata.folderId.toString(),
  156. entityType: ctx.metadata.entityType,
  157. rev: ctx.metadata.rev.toString(),
  158. })
  159. ctx.TpdsUpdateHandler.promises.newUpdate
  160. .calledWith(
  161. ctx.user_id,
  162. '', // projectId
  163. ctx.projectName,
  164. ctx.path,
  165. ctx.req,
  166. ctx.source
  167. )
  168. .should.equal(true)
  169. resolve()
  170. },
  171. }
  172. ctx.TpdsController.mergeUpdate(ctx.req, res)
  173. })
  174. })
  175. it('should indicate in the response when the update was rejected', function (ctx) {
  176. return new Promise(resolve => {
  177. ctx.TpdsUpdateHandler.promises.newUpdate.resolves(null)
  178. const res = {
  179. json: payload => {
  180. expect(payload).to.deep.equal({ status: 'rejected' })
  181. resolve()
  182. },
  183. }
  184. ctx.TpdsController.mergeUpdate(ctx.req, res)
  185. })
  186. })
  187. it('should process the update with the update receiver by id', function (ctx) {
  188. return new Promise(resolve => {
  189. const path = '/here.txt'
  190. const req = {
  191. pause() {},
  192. params: { 0: path, user_id: ctx.user_id, project_id: '123' },
  193. session: {
  194. destroy() {},
  195. },
  196. headers: {
  197. 'x-update-source': (ctx.source = 'dropbox'),
  198. },
  199. }
  200. const res = {
  201. json: () => {
  202. ctx.TpdsUpdateHandler.promises.newUpdate.should.have.been.calledWith(
  203. ctx.user_id,
  204. '123',
  205. '', // projectName
  206. '/here.txt',
  207. req,
  208. ctx.source
  209. )
  210. resolve()
  211. },
  212. }
  213. ctx.TpdsController.mergeUpdate(req, res)
  214. })
  215. })
  216. it('should return a 500 error when the update receiver fails', function (ctx) {
  217. return new Promise(resolve => {
  218. ctx.TpdsUpdateHandler.promises.newUpdate.rejects(new Error())
  219. const res = {
  220. json: sinon.stub(),
  221. }
  222. ctx.TpdsController.mergeUpdate(ctx.req, res, err => {
  223. expect(err).to.exist
  224. expect(res.json).not.to.have.been.called
  225. resolve()
  226. })
  227. })
  228. })
  229. it('should return a 400 error when the project is too big', function (ctx) {
  230. return new Promise(resolve => {
  231. ctx.TpdsUpdateHandler.promises.newUpdate.rejects({
  232. message: 'project_has_too_many_files',
  233. })
  234. const res = {
  235. sendStatus: status => {
  236. expect(status).to.equal(400)
  237. ctx.NotificationsBuilder.tpdsFileLimit.should.have.been.calledWith(
  238. ctx.user_id
  239. )
  240. resolve()
  241. },
  242. }
  243. ctx.TpdsController.mergeUpdate(ctx.req, res)
  244. })
  245. })
  246. it('should return a 429 error when the update receiver fails due to too many requests error', function (ctx) {
  247. return new Promise(resolve => {
  248. ctx.TpdsUpdateHandler.promises.newUpdate.rejects(
  249. new Errors.TooManyRequestsError('project on cooldown')
  250. )
  251. const res = {
  252. sendStatus: status => {
  253. expect(status).to.equal(429)
  254. resolve()
  255. },
  256. }
  257. ctx.TpdsController.mergeUpdate(ctx.req, res)
  258. })
  259. })
  260. })
  261. describe('getting a delete update', function () {
  262. it('should process the delete with the update receiver by name', function (ctx) {
  263. return new Promise(resolve => {
  264. const path = '/projectName/here.txt'
  265. const req = {
  266. params: { 0: path, user_id: ctx.user_id, project_id: '' },
  267. session: {
  268. destroy() {},
  269. },
  270. headers: {
  271. 'x-update-source': (ctx.source = 'dropbox'),
  272. },
  273. }
  274. const res = {
  275. sendStatus: () => {
  276. ctx.TpdsUpdateHandler.promises.deleteUpdate
  277. .calledWith(
  278. ctx.user_id,
  279. '',
  280. 'projectName',
  281. '/here.txt',
  282. ctx.source
  283. )
  284. .should.equal(true)
  285. resolve()
  286. },
  287. }
  288. ctx.TpdsController.deleteUpdate(req, res)
  289. })
  290. })
  291. it('should process the delete with the update receiver by id', function (ctx) {
  292. return new Promise(resolve => {
  293. const path = '/here.txt'
  294. const req = {
  295. params: { 0: path, user_id: ctx.user_id, project_id: '123' },
  296. session: {
  297. destroy() {},
  298. },
  299. headers: {
  300. 'x-update-source': (ctx.source = 'dropbox'),
  301. },
  302. }
  303. const res = {
  304. sendStatus: () => {
  305. ctx.TpdsUpdateHandler.promises.deleteUpdate.should.have.been.calledWith(
  306. ctx.user_id,
  307. '123',
  308. '', // projectName
  309. '/here.txt',
  310. ctx.source
  311. )
  312. resolve()
  313. },
  314. }
  315. ctx.TpdsController.deleteUpdate(req, res)
  316. })
  317. })
  318. })
  319. describe('updateFolder', function () {
  320. beforeEach(function (ctx) {
  321. ctx.req = {
  322. body: { userId: ctx.user_id, path: '/abc/def/ghi.txt' },
  323. }
  324. ctx.res = {
  325. json: sinon.stub(),
  326. }
  327. })
  328. it("creates a folder if it doesn't exist", function (ctx) {
  329. return new Promise(resolve => {
  330. const metadata = {
  331. folderId: new ObjectId(),
  332. projectId: new ObjectId(),
  333. path: '/def/ghi.txt',
  334. parentFolderId: new ObjectId(),
  335. }
  336. ctx.TpdsUpdateHandler.promises.createFolder.resolves(metadata)
  337. ctx.res.json.callsFake(body => {
  338. expect(body).to.deep.equal({
  339. entityId: metadata.folderId.toString(),
  340. projectId: metadata.projectId.toString(),
  341. path: metadata.path,
  342. folderId: metadata.parentFolderId.toString(),
  343. })
  344. resolve()
  345. })
  346. ctx.TpdsController.updateFolder(ctx.req, ctx.res)
  347. })
  348. })
  349. it('supports top level folders', function (ctx) {
  350. return new Promise(resolve => {
  351. const metadata = {
  352. folderId: new ObjectId(),
  353. projectId: new ObjectId(),
  354. path: '/',
  355. parentFolderId: null,
  356. }
  357. ctx.TpdsUpdateHandler.promises.createFolder.resolves(metadata)
  358. ctx.res.json.callsFake(body => {
  359. expect(body).to.deep.equal({
  360. entityId: metadata.folderId.toString(),
  361. projectId: metadata.projectId.toString(),
  362. path: metadata.path,
  363. folderId: null,
  364. })
  365. resolve()
  366. })
  367. ctx.TpdsController.updateFolder(ctx.req, ctx.res)
  368. })
  369. })
  370. it("returns a 409 if the folder couldn't be created", function (ctx) {
  371. return new Promise(resolve => {
  372. ctx.TpdsUpdateHandler.promises.createFolder.resolves(null)
  373. ctx.HttpErrorHandler.conflict.callsFake((req, res) => {
  374. expect(req).to.equal(ctx.req)
  375. expect(res).to.equal(ctx.res)
  376. resolve()
  377. })
  378. ctx.TpdsController.updateFolder(ctx.req, ctx.res)
  379. })
  380. })
  381. })
  382. describe('parseParams', function () {
  383. it('should take the project name off the start and replace with slash', function (ctx) {
  384. const path = 'noSlashHere'
  385. const req = { params: { 0: path, user_id: ctx.user_id } }
  386. const result = ctx.TpdsController.parseParams(req)
  387. result.userId.should.equal(ctx.user_id)
  388. result.filePath.should.equal('/')
  389. result.projectName.should.equal(path)
  390. })
  391. it('should take the project name off the start and it with no slashes in', function (ctx) {
  392. const path = '/project/file.tex'
  393. const req = { params: { 0: path, user_id: ctx.user_id } }
  394. const result = ctx.TpdsController.parseParams(req)
  395. result.userId.should.equal(ctx.user_id)
  396. result.filePath.should.equal('/file.tex')
  397. result.projectName.should.equal('project')
  398. })
  399. it('should take the project name of and return a slash for the file path', function (ctx) {
  400. const path = '/project_name'
  401. const req = { params: { 0: path, user_id: ctx.user_id } }
  402. const result = ctx.TpdsController.parseParams(req)
  403. result.projectName.should.equal('project_name')
  404. result.filePath.should.equal('/')
  405. })
  406. })
  407. describe('updateProjectContents', function () {
  408. beforeEach(async function (ctx) {
  409. ctx.req = {
  410. params: {
  411. 0: (ctx.path = 'chapters/main.tex'),
  412. project_id: (ctx.project_id = 'project-id-123'),
  413. },
  414. session: {
  415. destroy: sinon.stub(),
  416. },
  417. headers: {
  418. 'x-update-source': (ctx.source = 'github'),
  419. },
  420. }
  421. ctx.res = {
  422. json: sinon.stub(),
  423. sendStatus: sinon.stub(),
  424. }
  425. await ctx.TpdsController.promises.updateProjectContents(ctx.req, ctx.res)
  426. })
  427. it('should merge the update', function (ctx) {
  428. ctx.UpdateMerger.promises.mergeUpdate.should.be.calledWith(
  429. null,
  430. ctx.project_id,
  431. `/${ctx.path}`,
  432. ctx.req,
  433. ctx.source
  434. )
  435. })
  436. it('should return a success', function (ctx) {
  437. ctx.res.json.should.be.calledWith({
  438. entityId: ctx.metadata.entityId.toString(),
  439. rev: ctx.metadata.rev,
  440. })
  441. })
  442. })
  443. describe('deleteProjectContents', function () {
  444. beforeEach(async function (ctx) {
  445. ctx.req = {
  446. params: {
  447. 0: (ctx.path = 'chapters/main.tex'),
  448. project_id: (ctx.project_id = 'project-id-123'),
  449. },
  450. session: {
  451. destroy: sinon.stub(),
  452. },
  453. headers: {
  454. 'x-update-source': (ctx.source = 'github'),
  455. },
  456. }
  457. ctx.res = {
  458. sendStatus: sinon.stub(),
  459. json: sinon.stub(),
  460. }
  461. await ctx.TpdsController.promises.deleteProjectContents(ctx.req, ctx.res)
  462. })
  463. it('should delete the file', function (ctx) {
  464. ctx.UpdateMerger.promises.deleteUpdate.should.be.calledWith(
  465. null,
  466. ctx.project_id,
  467. `/${ctx.path}`,
  468. ctx.source
  469. )
  470. })
  471. it('should return a success', function (ctx) {
  472. ctx.res.json.should.be.calledWith({
  473. entityId: ctx.metadata.entityId,
  474. })
  475. })
  476. })
  477. describe('getQueues', function () {
  478. beforeEach(function (ctx) {
  479. ctx.req = {}
  480. ctx.res = { json: sinon.stub() }
  481. ctx.next = sinon.stub()
  482. })
  483. describe('success', function () {
  484. beforeEach(function (ctx) {
  485. return new Promise(resolve => {
  486. ctx.res.json.callsFake(() => {
  487. resolve()
  488. })
  489. ctx.TpdsController.getQueues(ctx.req, ctx.res, ctx.next)
  490. })
  491. })
  492. it('should use userId from session', function (ctx) {
  493. ctx.SessionManager.getLoggedInUserId.should.have.been.calledOnce
  494. ctx.TpdsQueueManager.promises.getQueues.should.have.been.calledWith(
  495. 'user-id'
  496. )
  497. })
  498. it('should call json with response', function (ctx) {
  499. ctx.res.json.should.have.been.calledWith('queues')
  500. ctx.next.should.not.have.been.called
  501. })
  502. })
  503. describe('error', function () {
  504. beforeEach(function (ctx) {
  505. return new Promise(resolve => {
  506. ctx.err = new Error()
  507. ctx.TpdsQueueManager.promises.getQueues = sinon
  508. .stub()
  509. .rejects(ctx.err)
  510. ctx.next.callsFake(() => {
  511. resolve()
  512. })
  513. ctx.TpdsController.getQueues(ctx.req, ctx.res, ctx.next)
  514. })
  515. })
  516. it('should call next with error', function (ctx) {
  517. ctx.res.json.should.not.have.been.called
  518. ctx.next.should.have.been.calledWith(ctx.err)
  519. })
  520. })
  521. })
  522. })