TpdsControllerTests.mjs 15 KB

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