TpdsControllerTests.js 14 KB

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