ProjectGetterTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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('mongojs')
  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/CollaboratorsHandler': (this.CollaboratorsHandler = {}),
  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.find = 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 find with the project id', function() {
  163. expect(this.db.projects.find.callCount).to.equal(1)
  164. return expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
  165. _id: ObjectId(this.project_id)
  166. })
  167. })
  168. })
  169. describe('without project id', function() {
  170. beforeEach(function() {
  171. return this.ProjectGetter.getProject(null, this.callback)
  172. })
  173. it('should callback with error', function() {
  174. expect(this.db.projects.find.callCount).to.equal(0)
  175. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  176. })
  177. })
  178. })
  179. describe('with projection', function() {
  180. beforeEach(function() {
  181. return (this.projection = { _id: 1 })
  182. })
  183. describe('with project id', function() {
  184. beforeEach(function() {
  185. return this.ProjectGetter.getProject(
  186. this.project_id,
  187. this.projection,
  188. this.callback
  189. )
  190. })
  191. it('should call find with the project id', function() {
  192. expect(this.db.projects.find.callCount).to.equal(1)
  193. expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
  194. _id: ObjectId(this.project_id)
  195. })
  196. return expect(this.db.projects.find.lastCall.args[1]).to.deep.equal(
  197. this.projection
  198. )
  199. })
  200. })
  201. describe('without project id', function() {
  202. beforeEach(function() {
  203. return this.ProjectGetter.getProject(null, this.callback)
  204. })
  205. it('should callback with error', function() {
  206. expect(this.db.projects.find.callCount).to.equal(0)
  207. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  208. })
  209. })
  210. })
  211. })
  212. describe('getProjectWithoutLock', function() {
  213. beforeEach(function() {
  214. this.project = { _id: (this.project_id = '56d46b0a1d3422b87c5ebcb1') }
  215. return (this.db.projects.find = sinon
  216. .stub()
  217. .callsArgWith(2, null, [this.project]))
  218. })
  219. describe('without projection', function() {
  220. describe('with project id', function() {
  221. beforeEach(function() {
  222. return this.ProjectGetter.getProjectWithoutLock(
  223. this.project_id,
  224. this.callback
  225. )
  226. })
  227. it('should call find with the project id', function() {
  228. expect(this.db.projects.find.callCount).to.equal(1)
  229. return expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
  230. _id: ObjectId(this.project_id)
  231. })
  232. })
  233. })
  234. describe('without project id', function() {
  235. beforeEach(function() {
  236. return this.ProjectGetter.getProjectWithoutLock(null, this.callback)
  237. })
  238. it('should callback with error', function() {
  239. expect(this.db.projects.find.callCount).to.equal(0)
  240. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  241. })
  242. })
  243. })
  244. describe('with projection', function() {
  245. beforeEach(function() {
  246. return (this.projection = { _id: 1 })
  247. })
  248. describe('with project id', function() {
  249. beforeEach(function() {
  250. return this.ProjectGetter.getProjectWithoutLock(
  251. this.project_id,
  252. this.projection,
  253. this.callback
  254. )
  255. })
  256. it('should call find with the project id', function() {
  257. expect(this.db.projects.find.callCount).to.equal(1)
  258. expect(this.db.projects.find.lastCall.args[0]).to.deep.equal({
  259. _id: ObjectId(this.project_id)
  260. })
  261. return expect(this.db.projects.find.lastCall.args[1]).to.deep.equal(
  262. this.projection
  263. )
  264. })
  265. })
  266. describe('without project id', function() {
  267. beforeEach(function() {
  268. return this.ProjectGetter.getProjectWithoutLock(null, this.callback)
  269. })
  270. it('should callback with error', function() {
  271. expect(this.db.projects.find.callCount).to.equal(0)
  272. return expect(this.callback.lastCall.args[0]).to.be.instanceOf(Error)
  273. })
  274. })
  275. })
  276. })
  277. describe('findAllUsersProjects', function() {
  278. beforeEach(function() {
  279. this.fields = { mock: 'fields' }
  280. this.Project.find = sinon.stub()
  281. this.Project.find
  282. .withArgs({ owner_ref: this.user_id }, this.fields)
  283. .yields(null, ['mock-owned-projects'])
  284. this.CollaboratorsHandler.getProjectsUserIsMemberOf = sinon.stub()
  285. this.CollaboratorsHandler.getProjectsUserIsMemberOf
  286. .withArgs(this.user_id, this.fields)
  287. .yields(null, {
  288. readAndWrite: ['mock-rw-projects'],
  289. readOnly: ['mock-ro-projects'],
  290. tokenReadAndWrite: ['mock-token-rw-projects'],
  291. tokenReadOnly: ['mock-token-ro-projects']
  292. })
  293. return this.ProjectGetter.findAllUsersProjects(
  294. this.user_id,
  295. this.fields,
  296. this.callback
  297. )
  298. })
  299. it('should call the callback with all the projects', function() {
  300. return this.callback
  301. .calledWith(null, {
  302. owned: ['mock-owned-projects'],
  303. readAndWrite: ['mock-rw-projects'],
  304. readOnly: ['mock-ro-projects'],
  305. tokenReadAndWrite: ['mock-token-rw-projects'],
  306. tokenReadOnly: ['mock-token-ro-projects']
  307. })
  308. .should.equal(true)
  309. })
  310. })
  311. describe('getProjectIdByReadAndWriteToken', function() {
  312. describe('when project find returns project', function() {
  313. this.beforeEach(function() {
  314. this.Project.findOne = sinon.stub().yields(null, { _id: 'project-id' })
  315. return this.ProjectGetter.getProjectIdByReadAndWriteToken(
  316. 'token',
  317. this.callback
  318. )
  319. })
  320. it('should find project with token', function() {
  321. return this.Project.findOne
  322. .calledWithMatch({ 'tokens.readAndWrite': 'token' })
  323. .should.equal(true)
  324. })
  325. it('should callback with project id', function() {
  326. return this.callback.calledWith(null, 'project-id').should.equal(true)
  327. })
  328. })
  329. describe('when project not found', function() {
  330. this.beforeEach(function() {
  331. this.Project.findOne = sinon.stub().yields()
  332. return this.ProjectGetter.getProjectIdByReadAndWriteToken(
  333. 'token',
  334. this.callback
  335. )
  336. })
  337. it('should callback empty', function() {
  338. return expect(this.callback.firstCall.args.length).to.equal(0)
  339. })
  340. })
  341. describe('when project find returns error', function() {
  342. this.beforeEach(function() {
  343. this.Project.findOne = sinon.stub().yields('error')
  344. return this.ProjectGetter.getProjectIdByReadAndWriteToken(
  345. 'token',
  346. this.callback
  347. )
  348. })
  349. it('should callback with error', function() {
  350. return this.callback.calledWith('error').should.equal(true)
  351. })
  352. })
  353. })
  354. describe('getUsersDeletedProjects', function() {
  355. it('should look up the deleted projects by deletedProjectOwnerId', function(done) {
  356. this.ProjectGetter.getUsersDeletedProjects('giraffe', err => {
  357. if (err) {
  358. return done(err)
  359. }
  360. sinon.assert.calledWith(this.DeletedProject.find, {
  361. 'deleterData.deletedProjectOwnerId': 'giraffe'
  362. })
  363. done()
  364. })
  365. })
  366. it('should pass the found projects to the callback', function(done) {
  367. this.ProjectGetter.getUsersDeletedProjects('giraffe', (err, docs) => {
  368. if (err) {
  369. return done(err)
  370. }
  371. expect(docs).to.deep.equal([this.deletedProject])
  372. done()
  373. })
  374. })
  375. })
  376. })