UserInfoControllerTests.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. '../Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()}
  28. @req = new MockRequest()
  29. @res = new MockResponse()
  30. @next = sinon.stub()
  31. describe "getLoggedInUsersPersonalInfo", ->
  32. beforeEach ->
  33. @user =
  34. _id: ObjectId()
  35. @req.user = @user
  36. @req.session.user = @user
  37. @UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  38. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  39. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@user._id)
  40. @UserInfoController.getLoggedInUsersPersonalInfo(@req, @res, @next)
  41. it "should call sendFormattedPersonalInfo", ->
  42. @UserInfoController.sendFormattedPersonalInfo
  43. .calledWith(@user, @res, @next)
  44. .should.equal true
  45. describe "getPersonalInfo", ->
  46. beforeEach ->
  47. @user_id = ObjectId().toString()
  48. @user =
  49. _id: ObjectId(@user_id)
  50. @req.params = user_id: @user_id
  51. describe "when the user exists", ->
  52. beforeEach ->
  53. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  54. @UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  55. @UserInfoController.getPersonalInfo(@req, @res, @next)
  56. it "should look up the user in the database", ->
  57. @UserGetter.getUser
  58. .calledWith(@user_id, { _id: true, first_name: true, last_name: true, email: true })
  59. .should.equal true
  60. it "should send the formatted details back to the client", ->
  61. @UserInfoController.sendFormattedPersonalInfo
  62. .calledWith(@user, @res, @next)
  63. .should.equal true
  64. describe "when the user does not exist", ->
  65. beforeEach ->
  66. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  67. @UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  68. @UserInfoController.getPersonalInfo(@req, @res, @next)
  69. it "should return 404 to the client", ->
  70. @res.statusCode.should.equal 404
  71. describe "sendFormattedPersonalInfo", ->
  72. beforeEach ->
  73. @user =
  74. _id: ObjectId()
  75. first_name: "Douglas"
  76. last_name: "Adams"
  77. email: "doug@sharelatex.com"
  78. @formattedInfo =
  79. id: @user._id.toString()
  80. first_name: @user.first_name
  81. last_name: @user.last_name
  82. email: @user.email
  83. @UserInfoController.formatPersonalInfo = sinon.stub().returns(@formattedInfo)
  84. @UserInfoController.sendFormattedPersonalInfo @user, @res
  85. it "should format the user details for the response", ->
  86. @UserInfoController.formatPersonalInfo
  87. .calledWith(@user)
  88. .should.equal true
  89. it "should send the formatted details back to the client", ->
  90. @res.body.should.equal JSON.stringify(@formattedInfo)
  91. describe "formatPersonalInfo", ->
  92. it "should return the correctly formatted data", ->
  93. @user =
  94. _id: ObjectId()
  95. first_name: "Douglas"
  96. last_name: "Adams"
  97. email: "doug@sharelatex.com"
  98. password: "should-not-get-included"
  99. signUpDate: new Date()
  100. role:"student"
  101. institution:"sheffield"
  102. expect(@UserInfoController.formatPersonalInfo(@user)).to.deep.equal {
  103. id: @user._id.toString()
  104. first_name: @user.first_name
  105. last_name: @user.last_name
  106. email: @user.email
  107. signUpDate: @user.signUpDate
  108. role: @user.role
  109. institution: @user.institution
  110. }