ProjectGetterTests.js 14 KB

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