ProjectGetterTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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')
  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.projectRO = { _id: 'mock-ro-projects' }
  253. this.projectTokenRW = { _id: 'mock-token-rw-projects' }
  254. this.projectTokenRO = { _id: 'mock-token-ro-projects' }
  255. this.Project.find
  256. .withArgs({ owner_ref: this.userId }, this.fields)
  257. .returns({ exec: sinon.stub().resolves([this.projectOwned]) })
  258. })
  259. it('should return a promise with all the projects', async function () {
  260. this.CollaboratorsGetter.promises.getProjectsUserIsMemberOf.resolves({
  261. readAndWrite: [this.projectRW],
  262. readOnly: [this.projectRO],
  263. tokenReadAndWrite: [this.projectTokenRW],
  264. tokenReadOnly: [this.projectTokenRO],
  265. })
  266. const projects = await this.ProjectGetter.promises.findAllUsersProjects(
  267. this.userId,
  268. this.fields
  269. )
  270. expect(projects).to.deep.equal({
  271. owned: [this.projectOwned],
  272. readAndWrite: [this.projectRW],
  273. readOnly: [this.projectRO],
  274. tokenReadAndWrite: [this.projectTokenRW],
  275. tokenReadOnly: [this.projectTokenRO],
  276. })
  277. })
  278. it('should remove duplicate projects', async function () {
  279. this.CollaboratorsGetter.promises.getProjectsUserIsMemberOf.resolves({
  280. readAndWrite: [this.projectRW, this.projectOwned],
  281. readOnly: [this.projectRO, this.projectRW],
  282. tokenReadAndWrite: [this.projectTokenRW, this.projectRO],
  283. tokenReadOnly: [
  284. this.projectTokenRW,
  285. this.projectTokenRO,
  286. this.projectRO,
  287. ],
  288. })
  289. const projects = await this.ProjectGetter.promises.findAllUsersProjects(
  290. this.userId,
  291. this.fields
  292. )
  293. expect(projects).to.deep.equal({
  294. owned: [this.projectOwned],
  295. readAndWrite: [this.projectRW],
  296. readOnly: [this.projectRO],
  297. tokenReadAndWrite: [this.projectTokenRW],
  298. tokenReadOnly: [this.projectTokenRO],
  299. })
  300. })
  301. })
  302. describe('getProjectIdByReadAndWriteToken', function () {
  303. describe('when project find returns project', function () {
  304. this.beforeEach(async function () {
  305. this.projectIdFound =
  306. await this.ProjectGetter.promises.getProjectIdByReadAndWriteToken(
  307. 'token'
  308. )
  309. })
  310. it('should find project with token', function () {
  311. this.Project.findOne
  312. .calledWithMatch({ 'tokens.readAndWrite': 'token' })
  313. .should.equal(true)
  314. })
  315. it('should return the project id', function () {
  316. expect(this.projectIdFound).to.equal(this.project._id)
  317. })
  318. })
  319. describe('when project not found', function () {
  320. it('should return undefined', async function () {
  321. this.Project.findOne.returns({ exec: sinon.stub().resolves(null) })
  322. const projectId =
  323. await this.ProjectGetter.promises.getProjectIdByReadAndWriteToken(
  324. 'token'
  325. )
  326. expect(projectId).to.equal(undefined)
  327. })
  328. })
  329. describe('when project find returns error', function () {
  330. this.beforeEach(async function () {
  331. this.Project.findOne.returns({ exec: sinon.stub().rejects() })
  332. })
  333. it('should be rejected', function () {
  334. expect(
  335. this.ProjectGetter.promises.getProjectIdByReadAndWriteToken('token')
  336. ).to.be.rejected
  337. })
  338. })
  339. })
  340. describe('findUsersProjectsByName', function () {
  341. it('should perform a case-insensitive search', async function () {
  342. this.project1 = { _id: 1, name: 'find me!' }
  343. this.project2 = { _id: 2, name: 'not me!' }
  344. this.project3 = { _id: 3, name: 'FIND ME!' }
  345. this.project4 = { _id: 4, name: 'Find Me!' }
  346. this.Project.find.withArgs({ owner_ref: this.userId }).returns({
  347. exec: sinon
  348. .stub()
  349. .resolves([
  350. this.project1,
  351. this.project2,
  352. this.project3,
  353. this.project4,
  354. ]),
  355. })
  356. const projects =
  357. await this.ProjectGetter.promises.findUsersProjectsByName(
  358. this.userId,
  359. this.project1.name
  360. )
  361. const projectNames = projects.map(project => project.name)
  362. expect(projectNames).to.have.members([
  363. this.project1.name,
  364. this.project3.name,
  365. this.project4.name,
  366. ])
  367. })
  368. it('should search collaborations as well', async function () {
  369. this.project1 = { _id: 1, name: 'find me!' }
  370. this.project2 = { _id: 2, name: 'FIND ME!' }
  371. this.project3 = { _id: 3, name: 'Find Me!' }
  372. this.project4 = { _id: 4, name: 'find ME!' }
  373. this.project5 = { _id: 5, name: 'FIND me!' }
  374. this.Project.find
  375. .withArgs({ owner_ref: this.userId })
  376. .returns({ exec: sinon.stub().resolves([this.project1]) })
  377. this.CollaboratorsGetter.promises.getProjectsUserIsMemberOf.resolves({
  378. readAndWrite: [this.project2],
  379. readOnly: [this.project3],
  380. tokenReadAndWrite: [this.project4],
  381. tokenReadOnly: [this.project5],
  382. })
  383. const projects =
  384. await this.ProjectGetter.promises.findUsersProjectsByName(
  385. this.userId,
  386. this.project1.name
  387. )
  388. expect(projects.map(project => project.name)).to.have.members([
  389. this.project1.name,
  390. this.project2.name,
  391. ])
  392. })
  393. })
  394. describe('getUsersDeletedProjects', function () {
  395. it('should look up the deleted projects by deletedProjectOwnerId', async function () {
  396. await this.ProjectGetter.promises.getUsersDeletedProjects('giraffe')
  397. sinon.assert.calledWith(this.DeletedProject.find, {
  398. 'deleterData.deletedProjectOwnerId': 'giraffe',
  399. })
  400. })
  401. it('should pass the found projects to the callback', async function () {
  402. const docs = await this.ProjectGetter.promises.getUsersDeletedProjects(
  403. 'giraffe'
  404. )
  405. expect(docs).to.deep.equal([this.deletedProject])
  406. })
  407. })
  408. })