ProjectGetterTests.coffee 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Project/ProjectGetter.js"
  6. SandboxedModule = require('sandboxed-module')
  7. ObjectId = require("mongojs").ObjectId
  8. describe "ProjectGetter", ->
  9. beforeEach ->
  10. @callback = sinon.stub()
  11. @ProjectGetter = SandboxedModule.require modulePath, requires:
  12. "../../infrastructure/mongojs":
  13. db: @db =
  14. projects: {}
  15. users: {}
  16. ObjectId: ObjectId
  17. describe "getProjectWithoutDocLines", ->
  18. beforeEach ->
  19. @project =
  20. _id: @project_id = "0123456789abcd9876543210"
  21. @db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
  22. @ProjectGetter.getProjectWithoutDocLines @project_id, @callback
  23. it "should call find with the project id", ->
  24. @db.projects.find.calledWith(_id: ObjectId(@project_id)).should.equal true
  25. it "should exclude the doc lines", ->
  26. excludes =
  27. "rootFolder.docs.lines": 0
  28. "rootFolder.folder.docs.lines": 0
  29. "rootFolder.folder.folder.docs.lines": 0
  30. "rootFolder.folder.folder.folder.docs.lines": 0
  31. "rootFolder.folder.folder.folder.folder.docs.lines": 0
  32. "rootFolder.folder.folder.folder.folder.folder.docs.lines": 0
  33. "rootFolder.folder.folder.folder.folder.folder.folder.docs.lines": 0
  34. "rootFolder.folder.folder.folder.folder.folder.folder.folder.docs.lines": 0
  35. @db.projects.find.calledWith(sinon.match.any, excludes)
  36. .should.equal true
  37. it "should call the callback with the project", ->
  38. @callback.calledWith(null, @project).should.equal true
  39. describe "populateProjectWithUsers", ->
  40. beforeEach ->
  41. @users = []
  42. @user_lookup = {}
  43. for i in [0..4]
  44. @users[i] = _id: ObjectId.createPk()
  45. @user_lookup[@users[i]._id.toString()] = @users[i]
  46. @project =
  47. _id: ObjectId.createPk()
  48. owner_ref: @users[0]._id
  49. readOnly_refs: [@users[1]._id, @users[2]._id]
  50. collaberator_refs: [@users[3]._id, @users[4]._id]
  51. @db.users.find = (query, callback) =>
  52. callback null, [@user_lookup[query._id.toString()]]
  53. sinon.spy @db.users, "find"
  54. @ProjectGetter.populateProjectWithUsers @project, @callback
  55. it "should look up each user", ->
  56. for user in @users
  57. @db.users.find.calledWith(_id: user._id).should.equal true
  58. it "should set the owner_ref to the owner", ->
  59. @project.owner_ref.should.equal @users[0]
  60. it "should set the readOnly_refs to the read only users", ->
  61. expect(@project.readOnly_refs).to.deep.equal [@users[1], @users[2]]
  62. it "should set the collaberator_refs to the collaborators", ->
  63. expect(@project.collaberator_refs).to.deep.equal [@users[3], @users[4]]
  64. it "should call the callback", ->
  65. @callback.calledWith(null, @project).should.equal true