UserControllerTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/User/UserController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. MockResponse = require "../helpers/MockResponse"
  9. MockRequest = require "../helpers/MockRequest"
  10. ObjectId = require("mongojs").ObjectId
  11. describe "UserController", ->
  12. beforeEach ->
  13. @UserController = SandboxedModule.require modulePath, requires:
  14. "./UserGetter": @UserGetter = {}
  15. @req = new MockRequest()
  16. @res = new MockResponse()
  17. @next = sinon.stub()
  18. describe "getLoggedInUsersPersonalInfo", ->
  19. beforeEach ->
  20. @user =
  21. _id: ObjectId()
  22. @req.user = @user
  23. @UserController.sendFormattedPersonalInfo = sinon.stub()
  24. @UserController.getLoggedInUsersPersonalInfo(@req, @res, @next)
  25. it "should call sendFormattedPersonalInfo", ->
  26. @UserController.sendFormattedPersonalInfo
  27. .calledWith(@user, @res, @next)
  28. .should.equal true
  29. describe "getPersonalInfo", ->
  30. beforeEach ->
  31. @user_id = ObjectId().toString()
  32. @user =
  33. _id: ObjectId(@user_id)
  34. @req.params = user_id: @user_id
  35. describe "when the user exists", ->
  36. beforeEach ->
  37. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  38. @UserController.sendFormattedPersonalInfo = sinon.stub()
  39. @UserController.getPersonalInfo(@req, @res, @next)
  40. it "should look up the user in the database", ->
  41. @UserGetter.getUser
  42. .calledWith(@user_id, { _id: true, first_name: true, last_name: true, email: true })
  43. .should.equal true
  44. it "should send the formatted details back to the client", ->
  45. @UserController.sendFormattedPersonalInfo
  46. .calledWith(@user, @res, @next)
  47. .should.equal true
  48. describe "when the user does not exist", ->
  49. beforeEach ->
  50. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  51. @UserController.sendFormattedPersonalInfo = sinon.stub()
  52. @UserController.getPersonalInfo(@req, @res, @next)
  53. it "should return 404 to the client", ->
  54. @res.statusCode.should.equal 404
  55. describe "sendFormattedPersonalInfo", ->
  56. beforeEach ->
  57. @user =
  58. _id: ObjectId()
  59. first_name: "Douglas"
  60. last_name: "Adams"
  61. email: "doug@sharelatex.com"
  62. @formattedInfo =
  63. id: @user._id.toString()
  64. first_name: @user.first_name
  65. last_name: @user.last_name
  66. email: @user.email
  67. @UserController._formatPersonalInfo = sinon.stub().callsArgWith(1, null, @formattedInfo)
  68. @UserController.sendFormattedPersonalInfo @user, @res
  69. it "should format the user details for the response", ->
  70. @UserController._formatPersonalInfo
  71. .calledWith(@user)
  72. .should.equal true
  73. it "should send the formatted details back to the client", ->
  74. @res.body.should.equal JSON.stringify(@formattedInfo)
  75. describe "_formatPersonalInfo", ->
  76. it "should return the correctly formatted data", ->
  77. @user =
  78. _id: ObjectId()
  79. first_name: "Douglas"
  80. last_name: "Adams"
  81. email: "doug@sharelatex.com"
  82. password: "should-not-get-included"
  83. signUpDate: new Date()
  84. @UserController._formatPersonalInfo @user, (error, info) =>
  85. expect(info).to.deep.equal {
  86. id: @user._id.toString()
  87. first_name: @user.first_name
  88. last_name: @user.last_name
  89. email: @user.email
  90. signUpDate: @user.signUpDate
  91. }