ProjectGetterTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../../app/src/Features/Project/ProjectGetter.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. const { ObjectId } = require('mongodb')
  21. const { normalizeQuery } = require('../../../../app/src/Features/Helpers/Mongo')
  22. const { assert } = require('chai')
  23. describe('ProjectGetter', function() {
  24. beforeEach(function() {
  25. this.callback = sinon.stub()
  26. this.deletedProject = { deleterData: { wombat: 'potato' } }
  27. this.DeletedProject = {
  28. find: sinon.stub().yields(null, [this.deletedProject])
  29. }
  30. return (this.ProjectGetter = SandboxedModule.require(modulePath, {
  31. globals: {
  32. console: console
  33. },
  34. requires: {
  35. '../../infrastructure/mongodb': {
  36. db: (this.db = {
  37. projects: {},
  38. users: {}
  39. }),
  40. ObjectId
  41. },
  42. 'metrics-sharelatex': {
  43. timeAsyncMethod: sinon.stub()
  44. },
  45. '../../models/Project': {
  46. Project: (this.Project = {})
  47. },
  48. '../../models/DeletedProject': {
  49. DeletedProject: this.DeletedProject
  50. },
  51. '../Collaborators/CollaboratorsGetter': (this.CollaboratorsGetter = {}),
  52. '../../infrastructure/LockManager': (this.LockManager = {
  53. runWithLock: sinon.spy((namespace, id, runner, callback) =>
  54. runner(callback)
  55. )
  56. }),
  57. './ProjectEntityMongoUpdateHandler': {
  58. lockKey(project_id) {
  59. return project_id
  60. }
  61. },
  62. '../Helpers/Mongo': { normalizeQuery },
  63. 'logger-sharelatex': {
  64. err() {},
  65. log() {}
  66. }
  67. }
  68. }))
  69. })
  70. describe('getProjectWithoutDocLines', function() {
  71. beforeEach(function() {
  72. this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
  73. return (this.ProjectGetter.getProject = sinon.stub().yields())
  74. })
  75. describe('passing an id', function() {
  76. beforeEach(function() {
  77. return this.ProjectGetter.getProjectWithoutDocLines(
  78. this.project_id,
  79. this.callback
  80. )
  81. })
  82. it('should call find with the project id', function() {
  83. return this.ProjectGetter.getProject
  84. .calledWith(this.project_id)
  85. .should.equal(true)
  86. })
  87. it('should exclude the doc lines', function() {
  88. const excludes = {
  89. 'rootFolder.docs.lines': 0,
  90. 'rootFolder.folders.docs.lines': 0,
  91. 'rootFolder.folders.folders.docs.lines': 0,
  92. 'rootFolder.folders.folders.folders.docs.lines': 0,
  93. 'rootFolder.folders.folders.folders.folders.docs.lines': 0,
  94. 'rootFolder.folders.folders.folders.folders.folders.docs.lines': 0,
  95. 'rootFolder.folders.folders.folders.folders.folders.folders.docs.lines': 0,
  96. 'rootFolder.folders.folders.folders.folders.folders.folders.folders.docs.lines': 0
  97. }
  98. return this.ProjectGetter.getProject
  99. .calledWith(this.project_id, excludes)
  100. .should.equal(true)
  101. })
  102. it('should call the callback', function() {
  103. return this.callback.called.should.equal(true)
  104. })
  105. })
  106. })
  107. describe('getProjectWithOnlyFolders', function() {
  108. beforeEach(function() {
  109. this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
  110. return (this.ProjectGetter.getProject = sinon.stub().yields())
  111. })
  112. describe('passing an id', function() {
  113. beforeEach(function() {
  114. return this.ProjectGetter.getProjectWithOnlyFolders(
  115. this.project_id,
  116. this.callback
  117. )
  118. })
  119. it('should call find with the project id', function() {
  120. return this.ProjectGetter.getProject
  121. .calledWith(this.project_id)
  122. .should.equal(true)
  123. })
  124. it('should exclude the docs and files linesaaaa', function() {
  125. const excludes = {
  126. 'rootFolder.docs': 0,
  127. 'rootFolder.fileRefs': 0,
  128. 'rootFolder.folders.docs': 0,
  129. 'rootFolder.folders.fileRefs': 0,
  130. 'rootFolder.folders.folders.docs': 0,
  131. 'rootFolder.folders.folders.fileRefs': 0,
  132. 'rootFolder.folders.folders.folders.docs': 0,
  133. 'rootFolder.folders.folders.folders.fileRefs': 0,
  134. 'rootFolder.folders.folders.folders.folders.docs': 0,
  135. 'rootFolder.folders.folders.folders.folders.fileRefs': 0,
  136. 'rootFolder.folders.folders.folders.folders.folders.docs': 0,
  137. 'rootFolder.folders.folders.folders.folders.folders.fileRefs': 0,
  138. 'rootFolder.folders.folders.folders.folders.folders.folders.docs': 0,
  139. 'rootFolder.folders.folders.folders.folders.folders.folders.fileRefs': 0,
  140. 'rootFolder.folders.folders.folders.folders.folders.folders.folders.docs': 0,
  141. 'rootFolder.folders.folders.folders.folders.folders.folders.folders.fileRefs': 0
  142. }
  143. return this.ProjectGetter.getProject
  144. .calledWith(this.project_id, excludes)
  145. .should.equal(true)
  146. })
  147. it('should call the callback with the project', function() {
  148. return this.callback.called.should.equal(true)
  149. })
  150. })
  151. })
  152. describe('getProject', function() {
  153. beforeEach(function() {
  154. this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
  155. return (this.db.projects.findOne = sinon
  156. .stub()
  157. .callsArgWith(2, null, this.project))
  158. })
  159. describe('without projection', function() {
  160. describe('with project id', function() {
  161. beforeEach(function() {
  162. return this.ProjectGetter.getProject(this.project_id, this.callback)
  163. })
  164. it('should call findOne with the project id', function() {
  165. expect(this.db.projects.findOne.callCount).to.equal(1)
  166. return expect(
  167. this.db.projects.findOne.lastCall.args[0]
  168. ).to.deep.equal({
  169. _id: ObjectId(this.project_id)
  170. })
  171. })
  172. })
  173. describe('without project id', function() {
  174. beforeEach(function() {
  175. return this.ProjectGetter.getProject(null, this.callback)
  176. })
  177. it('should callback with error', function() {
  178. expect(this.db.projects.findOne.callCount).to.equal(0)
  179. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  180. })
  181. })
  182. })
  183. describe('with projection', function() {
  184. beforeEach(function() {
  185. return (this.projection = { _id: 1 })
  186. })
  187. describe('with project id', function() {
  188. beforeEach(function() {
  189. return this.ProjectGetter.getProject(
  190. this.project_id,
  191. this.projection,
  192. this.callback
  193. )
  194. })
  195. it('should call findOne with the project id', function() {
  196. expect(this.db.projects.findOne.callCount).to.equal(1)
  197. expect(this.db.projects.findOne.lastCall.args[0]).to.deep.equal({
  198. _id: ObjectId(this.project_id)
  199. })
  200. return expect(
  201. this.db.projects.findOne.lastCall.args[1]
  202. ).to.deep.equal({ projection: this.projection })
  203. })
  204. })
  205. describe('without project id', function() {
  206. beforeEach(function() {
  207. return this.ProjectGetter.getProject(null, this.callback)
  208. })
  209. it('should callback with error', function() {
  210. expect(this.db.projects.findOne.callCount).to.equal(0)
  211. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  212. })
  213. })
  214. })
  215. })
  216. describe('getProjectWithoutLock', function() {
  217. beforeEach(function() {
  218. this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
  219. return (this.db.projects.findOne = sinon
  220. .stub()
  221. .callsArgWith(2, null, this.project))
  222. })
  223. describe('without projection', function() {
  224. describe('with project id', function() {
  225. beforeEach(function() {
  226. return this.ProjectGetter.getProjectWithoutLock(
  227. this.project_id,
  228. this.callback
  229. )
  230. })
  231. it('should call findOne with the project id', function() {
  232. expect(this.db.projects.findOne.callCount).to.equal(1)
  233. return expect(
  234. this.db.projects.findOne.lastCall.args[0]
  235. ).to.deep.equal({
  236. _id: ObjectId(this.project_id)
  237. })
  238. })
  239. })
  240. describe('without project id', function() {
  241. beforeEach(function() {
  242. return this.ProjectGetter.getProjectWithoutLock(null, this.callback)
  243. })
  244. it('should callback with error', function() {
  245. expect(this.db.projects.findOne.callCount).to.equal(0)
  246. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  247. })
  248. })
  249. })
  250. describe('with projection', function() {
  251. beforeEach(function() {
  252. return (this.projection = { _id: 1 })
  253. })
  254. describe('with project id', function() {
  255. beforeEach(function() {
  256. return this.ProjectGetter.getProjectWithoutLock(
  257. this.project_id,
  258. this.projection,
  259. this.callback
  260. )
  261. })
  262. it('should call findOne with the project id', function() {
  263. expect(this.db.projects.findOne.callCount).to.equal(1)
  264. expect(this.db.projects.findOne.lastCall.args[0]).to.deep.equal({
  265. _id: ObjectId(this.project_id)
  266. })
  267. return expect(
  268. this.db.projects.findOne.lastCall.args[1]
  269. ).to.deep.equal({ projection: this.projection })
  270. })
  271. })
  272. describe('without project id', function() {
  273. beforeEach(function() {
  274. return this.ProjectGetter.getProjectWithoutLock(null, this.callback)
  275. })
  276. it('should callback with error', function() {
  277. expect(this.db.projects.findOne.callCount).to.equal(0)
  278. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  279. })
  280. })
  281. })
  282. })
  283. describe('findAllUsersProjects', function() {
  284. beforeEach(function() {
  285. this.fields = { mock: 'fields' }
  286. this.Project.find = sinon.stub()
  287. this.Project.find
  288. .withArgs({ owner_ref: this.user_id }, this.fields)
  289. .yields(null, ['mock-owned-projects'])
  290. this.CollaboratorsGetter.getProjectsUserIsMemberOf = sinon.stub()
  291. this.CollaboratorsGetter.getProjectsUserIsMemberOf
  292. .withArgs(this.user_id, this.fields)
  293. .yields(null, {
  294. readAndWrite: ['mock-rw-projects'],
  295. readOnly: ['mock-ro-projects'],
  296. tokenReadAndWrite: ['mock-token-rw-projects'],
  297. tokenReadOnly: ['mock-token-ro-projects']
  298. })
  299. return this.ProjectGetter.findAllUsersProjects(
  300. this.user_id,
  301. this.fields,
  302. this.callback
  303. )
  304. })
  305. it('should call the callback with all the projects', function() {
  306. return this.callback
  307. .calledWith(null, {
  308. owned: ['mock-owned-projects'],
  309. readAndWrite: ['mock-rw-projects'],
  310. readOnly: ['mock-ro-projects'],
  311. tokenReadAndWrite: ['mock-token-rw-projects'],
  312. tokenReadOnly: ['mock-token-ro-projects']
  313. })
  314. .should.equal(true)
  315. })
  316. })
  317. describe('getProjectIdByReadAndWriteToken', function() {
  318. describe('when project find returns project', function() {
  319. this.beforeEach(function() {
  320. this.Project.findOne = sinon.stub().yields(null, { _id: 'project-id' })
  321. return this.ProjectGetter.getProjectIdByReadAndWriteToken(
  322. 'token',
  323. this.callback
  324. )
  325. })
  326. it('should find project with token', function() {
  327. return this.Project.findOne
  328. .calledWithMatch({ 'tokens.readAndWrite': 'token' })
  329. .should.equal(true)
  330. })
  331. it('should callback with project id', function() {
  332. return this.callback.calledWith(null, 'project-id').should.equal(true)
  333. })
  334. })
  335. describe('when project not found', function() {
  336. this.beforeEach(function() {
  337. this.Project.findOne = sinon.stub().yields()
  338. return this.ProjectGetter.getProjectIdByReadAndWriteToken(
  339. 'token',
  340. this.callback
  341. )
  342. })
  343. it('should callback empty', function() {
  344. return expect(this.callback.firstCall.args.length).to.equal(0)
  345. })
  346. })
  347. describe('when project find returns error', function() {
  348. this.beforeEach(function() {
  349. this.Project.findOne = sinon.stub().yields('error')
  350. return this.ProjectGetter.getProjectIdByReadAndWriteToken(
  351. 'token',
  352. this.callback
  353. )
  354. })
  355. it('should callback with error', function() {
  356. return this.callback.calledWith('error').should.equal(true)
  357. })
  358. })
  359. })
  360. describe('getUsersDeletedProjects', function() {
  361. it('should look up the deleted projects by deletedProjectOwnerId', function(done) {
  362. this.ProjectGetter.getUsersDeletedProjects('giraffe', err => {
  363. if (err) {
  364. return done(err)
  365. }
  366. sinon.assert.calledWith(this.DeletedProject.find, {
  367. 'deleterData.deletedProjectOwnerId': 'giraffe'
  368. })
  369. done()
  370. })
  371. })
  372. it('should pass the found projects to the callback', function(done) {
  373. this.ProjectGetter.getUsersDeletedProjects('giraffe', (err, docs) => {
  374. if (err) {
  375. return done(err)
  376. }
  377. expect(docs).to.deep.equal([this.deletedProject])
  378. done()
  379. })
  380. })
  381. })
  382. })