ProjectGetterTests.js 12 KB

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