UserInfoControllerTests.coffee 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. @UserInfoController = SandboxedModule.require modulePath, requires:
  21. "./UserGetter": @UserGetter = {}
  22. "./UserUpdater": @UserUpdater
  23. "./UserDeleter": @UserDeleter
  24. "sanitizer":@sanitizer
  25. @req = new MockRequest()
  26. @res = new MockResponse()
  27. @next = sinon.stub()
  28. describe "getLoggedInUsersPersonalInfo", ->
  29. beforeEach ->
  30. @user =
  31. _id: ObjectId()
  32. @req.user = @user
  33. @UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  34. @UserInfoController.getLoggedInUsersPersonalInfo(@req, @res, @next)
  35. it "should call sendFormattedPersonalInfo", ->
  36. @UserInfoController.sendFormattedPersonalInfo
  37. .calledWith(@user, @res, @next)
  38. .should.equal true
  39. describe "getPersonalInfo", ->
  40. beforeEach ->
  41. @user_id = ObjectId().toString()
  42. @user =
  43. _id: ObjectId(@user_id)
  44. @req.params = user_id: @user_id
  45. describe "when the user exists", ->
  46. beforeEach ->
  47. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  48. @UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  49. @UserInfoController.getPersonalInfo(@req, @res, @next)
  50. it "should look up the user in the database", ->
  51. @UserGetter.getUser
  52. .calledWith(@user_id, { _id: true, first_name: true, last_name: true, email: true })
  53. .should.equal true
  54. it "should send the formatted details back to the client", ->
  55. @UserInfoController.sendFormattedPersonalInfo
  56. .calledWith(@user, @res, @next)
  57. .should.equal true
  58. describe "when the user does not exist", ->
  59. beforeEach ->
  60. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  61. @UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  62. @UserInfoController.getPersonalInfo(@req, @res, @next)
  63. it "should return 404 to the client", ->
  64. @res.statusCode.should.equal 404
  65. describe "sendFormattedPersonalInfo", ->
  66. beforeEach ->
  67. @user =
  68. _id: ObjectId()
  69. first_name: "Douglas"
  70. last_name: "Adams"
  71. email: "doug@sharelatex.com"
  72. @formattedInfo =
  73. id: @user._id.toString()
  74. first_name: @user.first_name
  75. last_name: @user.last_name
  76. email: @user.email
  77. @UserInfoController._formatPersonalInfo = sinon.stub().callsArgWith(1, null, @formattedInfo)
  78. @UserInfoController.sendFormattedPersonalInfo @user, @res
  79. it "should format the user details for the response", ->
  80. @UserInfoController._formatPersonalInfo
  81. .calledWith(@user)
  82. .should.equal true
  83. it "should send the formatted details back to the client", ->
  84. @res.body.should.equal JSON.stringify(@formattedInfo)
  85. describe "_formatPersonalInfo", ->
  86. it "should return the correctly formatted data", ->
  87. @user =
  88. _id: ObjectId()
  89. first_name: "Douglas"
  90. last_name: "Adams"
  91. email: "doug@sharelatex.com"
  92. password: "should-not-get-included"
  93. signUpDate: new Date()
  94. @UserInfoController._formatPersonalInfo @user, (error, info) =>
  95. expect(info).to.deep.equal {
  96. id: @user._id.toString()
  97. first_name: @user.first_name
  98. last_name: @user.last_name
  99. email: @user.email
  100. signUpDate: @user.signUpDate
  101. }
  102. describe "setPersonalInfo", ->
  103. beforeEach ->
  104. @req =
  105. session:
  106. user:
  107. _id:"123123j321jikuj90jlk"
  108. @req.body =
  109. first_name: "bob"
  110. last_name: "smith"
  111. role:"student"
  112. institution: "Sheffield"
  113. notWanted: "something"
  114. it "should send the data from the body to the user updater", (done)->
  115. @UserUpdater.updatePersonalInfo.callsArgWith(2, null)
  116. @res.send = (statusCode)=>
  117. statusCode.should.equal 204
  118. @UserUpdater.updatePersonalInfo.args[0][0].should.equal @req.session.user._id
  119. args = @UserUpdater.updatePersonalInfo.args[0][1]
  120. args.first_name.should.equal @req.body.first_name
  121. args.last_name.should.equal @req.body.last_name
  122. args.role.should.equal @req.body.role
  123. args.institution.should.equal @req.body.institution
  124. assert.equal args.notWanted, undefined
  125. done()
  126. @UserInfoController.updatePersonalInfo @req, @res
  127. it "should sanitize the data", (done)->
  128. @UserUpdater.updatePersonalInfo.callsArgWith(2, null)
  129. @res.send = (statusCode)=>
  130. @sanitizer.escape.calledWith(@req.body.first_name).should.equal true
  131. @sanitizer.escape.calledWith(@req.body.last_name).should.equal true
  132. @sanitizer.escape.calledWith(@req.body.role).should.equal true
  133. @sanitizer.escape.calledWith(@req.body.institution).should.equal true
  134. done()
  135. @UserInfoController.updatePersonalInfo @req, @res
  136. it "should send an error if the UpserUpdater returns on", (done)->
  137. @UserUpdater.updatePersonalInfo.callsArgWith(2, "error")
  138. @res.send = (statusCode)->
  139. statusCode.should.equal 500
  140. done()
  141. @UserInfoController.updatePersonalInfo @req, @res