ProjectGetterTests.js 14 KB

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