ProjectGetterTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/Features/Project/ProjectGetter.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const { ObjectId } = require('mongodb-legacy')
  6. describe('ProjectGetter', function () {
  7. beforeEach(function () {
  8. this.project = { _id: new ObjectId() }
  9. this.projectIdStr = this.project._id.toString()
  10. this.deletedProject = { deleterData: { wombat: 'potato' } }
  11. this.userId = new ObjectId()
  12. this.DeletedProject = {
  13. find: sinon.stub().returns({
  14. exec: sinon.stub().resolves([this.deletedProject]),
  15. }),
  16. }
  17. this.Project = {
  18. find: sinon.stub().returns({
  19. exec: sinon.stub().resolves(),
  20. }),
  21. findOne: sinon.stub().returns({
  22. exec: sinon.stub().resolves(this.project),
  23. }),
  24. }
  25. this.CollaboratorsGetter = {
  26. promises: {
  27. getProjectsUserIsMemberOf: sinon.stub().resolves({
  28. readAndWrite: [],
  29. readOnly: [],
  30. tokenReadAndWrite: [],
  31. tokenReadOnly: [],
  32. }),
  33. },
  34. }
  35. this.LockManager = {
  36. promises: {
  37. runWithLock: sinon
  38. .stub()
  39. .callsFake((namespace, id, runner) => runner()),
  40. },
  41. }
  42. this.db = {
  43. projects: {
  44. findOne: sinon.stub().resolves(this.project),
  45. },
  46. users: {},
  47. }
  48. this.ProjectEntityMongoUpdateHandler = {
  49. lockKey: sinon.stub().returnsArg(0),
  50. }
  51. this.ProjectGetter = SandboxedModule.require(modulePath, {
  52. requires: {
  53. '../../infrastructure/mongodb': { db: this.db, ObjectId },
  54. '../../models/Project': {
  55. Project: this.Project,
  56. },
  57. '../../models/DeletedProject': {
  58. DeletedProject: this.DeletedProject,
  59. },
  60. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  61. '../../infrastructure/LockManager': this.LockManager,
  62. './ProjectEntityMongoUpdateHandler':
  63. this.ProjectEntityMongoUpdateHandler,
  64. },
  65. })
  66. })
  67. describe('getProjectWithoutDocLines', function () {
  68. beforeEach(function () {
  69. this.ProjectGetter.promises.getProject = sinon.stub().resolves()
  70. })
  71. describe('passing an id', function () {
  72. beforeEach(async function () {
  73. await this.ProjectGetter.promises.getProjectWithoutDocLines(
  74. this.project._id
  75. )
  76. })
  77. it('should call find with the project id', function () {
  78. this.ProjectGetter.promises.getProject
  79. .calledWith(this.project._id)
  80. .should.equal(true)
  81. })
  82. it('should exclude the doc lines', function () {
  83. const excludes = {
  84. 'rootFolder.docs.lines': 0,
  85. 'rootFolder.folders.docs.lines': 0,
  86. 'rootFolder.folders.folders.docs.lines': 0,
  87. 'rootFolder.folders.folders.folders.docs.lines': 0,
  88. 'rootFolder.folders.folders.folders.folders.docs.lines': 0,
  89. 'rootFolder.folders.folders.folders.folders.folders.docs.lines': 0,
  90. 'rootFolder.folders.folders.folders.folders.folders.folders.docs.lines': 0,
  91. 'rootFolder.folders.folders.folders.folders.folders.folders.folders.docs.lines': 0,
  92. }
  93. this.ProjectGetter.promises.getProject
  94. .calledWith(this.project._id, excludes)
  95. .should.equal(true)
  96. })
  97. })
  98. })
  99. describe('getProjectWithOnlyFolders', function () {
  100. beforeEach(function () {
  101. this.ProjectGetter.promises.getProject = sinon.stub().resolves()
  102. })
  103. describe('passing an id', function () {
  104. beforeEach(async function () {
  105. await this.ProjectGetter.promises.getProjectWithOnlyFolders(
  106. this.project._id
  107. )
  108. })
  109. it('should call find with the project id', function () {
  110. this.ProjectGetter.promises.getProject
  111. .calledWith(this.project._id)
  112. .should.equal(true)
  113. })
  114. it('should exclude the docs and files lines', function () {
  115. const excludes = {
  116. 'rootFolder.docs': 0,
  117. 'rootFolder.fileRefs': 0,
  118. 'rootFolder.folders.docs': 0,
  119. 'rootFolder.folders.fileRefs': 0,
  120. 'rootFolder.folders.folders.docs': 0,
  121. 'rootFolder.folders.folders.fileRefs': 0,
  122. 'rootFolder.folders.folders.folders.docs': 0,
  123. 'rootFolder.folders.folders.folders.fileRefs': 0,
  124. 'rootFolder.folders.folders.folders.folders.docs': 0,
  125. 'rootFolder.folders.folders.folders.folders.fileRefs': 0,
  126. 'rootFolder.folders.folders.folders.folders.folders.docs': 0,
  127. 'rootFolder.folders.folders.folders.folders.folders.fileRefs': 0,
  128. 'rootFolder.folders.folders.folders.folders.folders.folders.docs': 0,
  129. 'rootFolder.folders.folders.folders.folders.folders.folders.fileRefs': 0,
  130. 'rootFolder.folders.folders.folders.folders.folders.folders.folders.docs': 0,
  131. 'rootFolder.folders.folders.folders.folders.folders.folders.folders.fileRefs': 0,
  132. }
  133. this.ProjectGetter.promises.getProject
  134. .calledWith(this.project._id, excludes)
  135. .should.equal(true)
  136. })
  137. })
  138. })
  139. describe('getProject', function () {
  140. describe('without projection', function () {
  141. describe('with project id', function () {
  142. beforeEach(async function () {
  143. await this.ProjectGetter.promises.getProject(this.projectIdStr)
  144. })
  145. it('should call findOne with the project id', function () {
  146. expect(this.db.projects.findOne.callCount).to.equal(1)
  147. expect(
  148. this.db.projects.findOne.lastCall.args[0]._id.toString()
  149. ).to.equal(this.projectIdStr)
  150. })
  151. })
  152. describe('without project id', function () {
  153. it('should be rejected', function () {
  154. expect(
  155. this.ProjectGetter.promises.getProject(null)
  156. ).to.be.rejectedWith('no project id provided')
  157. expect(this.db.projects.findOne.callCount).to.equal(0)
  158. })
  159. })
  160. })
  161. describe('with projection', function () {
  162. beforeEach(function () {
  163. this.projection = { _id: 1 }
  164. })
  165. describe('with project id', function () {
  166. beforeEach(async function () {
  167. await this.ProjectGetter.promises.getProject(
  168. this.projectIdStr,
  169. this.projection
  170. )
  171. })
  172. it('should call findOne with the project id', function () {
  173. expect(this.db.projects.findOne.callCount).to.equal(1)
  174. expect(
  175. this.db.projects.findOne.lastCall.args[0]._id.toString()
  176. ).to.equal(this.projectIdStr)
  177. expect(this.db.projects.findOne.lastCall.args[1]).to.deep.equal({
  178. projection: this.projection,
  179. })
  180. })
  181. })
  182. describe('without project id', function () {
  183. it('should be rejected', function () {
  184. expect(
  185. this.ProjectGetter.promises.getProject(null)
  186. ).to.be.rejectedWith('no project id provided')
  187. expect(this.db.projects.findOne.callCount).to.equal(0)
  188. })
  189. })
  190. })
  191. })
  192. describe('getProjectWithoutLock', function () {
  193. describe('without projection', function () {
  194. describe('with project id', function () {
  195. beforeEach(async function () {
  196. await this.ProjectGetter.promises.getProjectWithoutLock(
  197. this.projectIdStr
  198. )
  199. })
  200. it('should call findOne with the project id', function () {
  201. expect(this.db.projects.findOne.callCount).to.equal(1)
  202. expect(
  203. this.db.projects.findOne.lastCall.args[0]._id.toString()
  204. ).to.equal(this.projectIdStr)
  205. })
  206. })
  207. describe('without project id', function () {
  208. it('should be rejected', function () {
  209. expect(
  210. this.ProjectGetter.promises.getProjectWithoutLock(null)
  211. ).to.be.rejectedWith('no project id provided')
  212. expect(this.db.projects.findOne.callCount).to.equal(0)
  213. })
  214. })
  215. })
  216. describe('with projection', function () {
  217. beforeEach(function () {
  218. this.projection = { _id: 1 }
  219. })
  220. describe('with project id', function () {
  221. beforeEach(async function () {
  222. await this.ProjectGetter.promises.getProjectWithoutLock(
  223. this.project._id,
  224. this.projection
  225. )
  226. })
  227. it('should call findOne with the project id', function () {
  228. expect(this.db.projects.findOne.callCount).to.equal(1)
  229. expect(
  230. this.db.projects.findOne.lastCall.args[0]._id.toString()
  231. ).to.equal(this.projectIdStr)
  232. expect(this.db.projects.findOne.lastCall.args[1]).to.deep.equal({
  233. projection: this.projection,
  234. })
  235. })
  236. })
  237. describe('without project id', function () {
  238. it('should be rejected', function () {
  239. expect(
  240. this.ProjectGetter.promises.getProjectWithoutLock(null)
  241. ).to.be.rejectedWith('no project id provided')
  242. expect(this.db.projects.findOne.callCount).to.equal(0)
  243. })
  244. })
  245. })
  246. })
  247. describe('findAllUsersProjects', function () {
  248. beforeEach(function () {
  249. this.fields = { mock: 'fields' }
  250. this.projectOwned = { _id: 'mock-owned-projects' }
  251. this.projectRW = { _id: 'mock-rw-projects' }
  252. this.projectReview = { _id: 'mock-review-projects' }
  253. this.projectRO = { _id: 'mock-ro-projects' }
  254. this.projectTokenRW = { _id: 'mock-token-rw-projects' }
  255. this.projectTokenRO = { _id: 'mock-token-ro-projects' }
  256. this.Project.find
  257. .withArgs({ owner_ref: this.userId }, this.fields)
  258. .returns({ exec: sinon.stub().resolves([this.projectOwned]) })
  259. })
  260. it('should return a promise with all the projects', async function () {
  261. this.CollaboratorsGetter.promises.getProjectsUserIsMemberOf.resolves({
  262. readAndWrite: [this.projectRW],
  263. readOnly: [this.projectRO],
  264. tokenReadAndWrite: [this.projectTokenRW],
  265. tokenReadOnly: [this.projectTokenRO],
  266. review: [this.projectReview],
  267. })
  268. const projects = await this.ProjectGetter.promises.findAllUsersProjects(
  269. this.userId,
  270. this.fields
  271. )
  272. expect(projects).to.deep.equal({
  273. owned: [this.projectOwned],
  274. readAndWrite: [this.projectRW],
  275. readOnly: [this.projectRO],
  276. tokenReadAndWrite: [this.projectTokenRW],
  277. tokenReadOnly: [this.projectTokenRO],
  278. review: [this.projectReview],
  279. })
  280. })
  281. it('should remove duplicate projects', async function () {
  282. this.CollaboratorsGetter.promises.getProjectsUserIsMemberOf.resolves({
  283. readAndWrite: [this.projectRW, this.projectOwned],
  284. readOnly: [this.projectRO, this.projectRW],
  285. tokenReadAndWrite: [this.projectTokenRW, this.projectRO],
  286. tokenReadOnly: [
  287. this.projectTokenRW,
  288. this.projectTokenRO,
  289. this.projectRO,
  290. ],
  291. review: [this.projectReview],
  292. })
  293. const projects = await this.ProjectGetter.promises.findAllUsersProjects(
  294. this.userId,
  295. this.fields
  296. )
  297. expect(projects).to.deep.equal({
  298. owned: [this.projectOwned],
  299. readAndWrite: [this.projectRW],
  300. readOnly: [this.projectRO],
  301. tokenReadAndWrite: [this.projectTokenRW],
  302. tokenReadOnly: [this.projectTokenRO],
  303. review: [this.projectReview],
  304. })
  305. })
  306. })
  307. describe('getProjectIdByReadAndWriteToken', function () {
  308. describe('when project find returns project', function () {
  309. this.beforeEach(async function () {
  310. this.projectIdFound =
  311. await this.ProjectGetter.promises.getProjectIdByReadAndWriteToken(
  312. 'token'
  313. )
  314. })
  315. it('should find project with token', function () {
  316. this.Project.findOne
  317. .calledWithMatch({ 'tokens.readAndWrite': 'token' })
  318. .should.equal(true)
  319. })
  320. it('should return the project id', function () {
  321. expect(this.projectIdFound).to.equal(this.project._id)
  322. })
  323. })
  324. describe('when project not found', function () {
  325. it('should return undefined', async function () {
  326. this.Project.findOne.returns({ exec: sinon.stub().resolves(null) })
  327. const projectId =
  328. await this.ProjectGetter.promises.getProjectIdByReadAndWriteToken(
  329. 'token'
  330. )
  331. expect(projectId).to.equal(undefined)
  332. })
  333. })
  334. describe('when project find returns error', function () {
  335. this.beforeEach(async function () {
  336. this.Project.findOne.returns({ exec: sinon.stub().rejects() })
  337. })
  338. it('should be rejected', function () {
  339. expect(
  340. this.ProjectGetter.promises.getProjectIdByReadAndWriteToken('token')
  341. ).to.be.rejected
  342. })
  343. })
  344. })
  345. describe('findUsersProjectsByName', function () {
  346. it('should perform a case-insensitive search', async function () {
  347. this.project1 = { _id: 1, name: 'find me!' }
  348. this.project2 = { _id: 2, name: 'not me!' }
  349. this.project3 = { _id: 3, name: 'FIND ME!' }
  350. this.project4 = { _id: 4, name: 'Find Me!' }
  351. this.Project.find.withArgs({ owner_ref: this.userId }).returns({
  352. exec: sinon
  353. .stub()
  354. .resolves([
  355. this.project1,
  356. this.project2,
  357. this.project3,
  358. this.project4,
  359. ]),
  360. })
  361. const projects =
  362. await this.ProjectGetter.promises.findUsersProjectsByName(
  363. this.userId,
  364. this.project1.name
  365. )
  366. const projectNames = projects.map(project => project.name)
  367. expect(projectNames).to.have.members([
  368. this.project1.name,
  369. this.project3.name,
  370. this.project4.name,
  371. ])
  372. })
  373. it('should search collaborations as well', async function () {
  374. this.project1 = { _id: 1, name: 'find me!' }
  375. this.project2 = { _id: 2, name: 'FIND ME!' }
  376. this.project3 = { _id: 3, name: 'Find Me!' }
  377. this.project4 = { _id: 4, name: 'find ME!' }
  378. this.project5 = { _id: 5, name: 'FIND me!' }
  379. this.Project.find
  380. .withArgs({ owner_ref: this.userId })
  381. .returns({ exec: sinon.stub().resolves([this.project1]) })
  382. this.CollaboratorsGetter.promises.getProjectsUserIsMemberOf.resolves({
  383. readAndWrite: [this.project2],
  384. readOnly: [this.project3],
  385. tokenReadAndWrite: [this.project4],
  386. tokenReadOnly: [this.project5],
  387. })
  388. const projects =
  389. await this.ProjectGetter.promises.findUsersProjectsByName(
  390. this.userId,
  391. this.project1.name
  392. )
  393. expect(projects.map(project => project.name)).to.have.members([
  394. this.project1.name,
  395. this.project2.name,
  396. ])
  397. })
  398. })
  399. describe('getUsersDeletedProjects', function () {
  400. it('should look up the deleted projects by deletedProjectOwnerId', async function () {
  401. await this.ProjectGetter.promises.getUsersDeletedProjects('giraffe')
  402. sinon.assert.calledWith(this.DeletedProject.find, {
  403. 'deleterData.deletedProjectOwnerId': 'giraffe',
  404. })
  405. })
  406. it('should pass the found projects to the callback', async function () {
  407. const docs =
  408. await this.ProjectGetter.promises.getUsersDeletedProjects('giraffe')
  409. expect(docs).to.deep.equal([this.deletedProject])
  410. })
  411. })
  412. })