ProjectGetterTests.js 14 KB

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