UserInfoControllerTests.coffee 3.9 KB

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