UserInfoControllerTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/Features/User/UserInfoController.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const MockResponse = require('../helpers/MockResponse')
  6. const MockRequest = require('../helpers/MockRequest')
  7. const { ObjectId } = require('mongodb')
  8. describe('UserInfoController', function () {
  9. beforeEach(function () {
  10. this.UserDeleter = { deleteUser: sinon.stub().callsArgWith(1) }
  11. this.UserUpdater = { updatePersonalInfo: sinon.stub() }
  12. this.UserGetter = {}
  13. this.UserInfoController = SandboxedModule.require(modulePath, {
  14. requires: {
  15. mongodb: { ObjectId },
  16. './UserGetter': this.UserGetter,
  17. './UserUpdater': this.UserUpdater,
  18. './UserDeleter': this.UserDeleter,
  19. '../Authentication/SessionManager': (this.SessionManager = {
  20. getLoggedInUserId: sinon.stub(),
  21. }),
  22. },
  23. })
  24. this.req = new MockRequest()
  25. this.res = new MockResponse()
  26. this.next = sinon.stub()
  27. })
  28. describe('getPersonalInfo', function () {
  29. describe('when the user exists with sharelatex id', function () {
  30. beforeEach(function () {
  31. this.user_id = ObjectId().toString()
  32. this.user = { _id: ObjectId(this.user_id) }
  33. this.req.params = { user_id: this.user_id }
  34. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  35. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  36. this.UserInfoController.getPersonalInfo(this.req, this.res, this.next)
  37. })
  38. it('should look up the user in the database', function () {
  39. this.UserGetter.getUser
  40. .calledWith(
  41. { _id: ObjectId(this.user_id) },
  42. { _id: true, first_name: true, last_name: true, email: true }
  43. )
  44. .should.equal(true)
  45. })
  46. })
  47. describe('when the user exists with overleaf id', function () {
  48. beforeEach(function () {
  49. this.user_id = 12345
  50. this.user = {
  51. _id: ObjectId(),
  52. overleaf: {
  53. id: this.user_id,
  54. },
  55. }
  56. this.req.params = { user_id: this.user_id.toString() }
  57. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  58. this.UserInfoController.getPersonalInfo(this.req, this.res, this.next)
  59. })
  60. it('should look up the user in the database', function () {
  61. this.UserGetter.getUser
  62. .calledWith(
  63. { 'overleaf.id': this.user_id },
  64. { _id: true, first_name: true, last_name: true, email: true }
  65. )
  66. .should.equal(true)
  67. })
  68. })
  69. describe('when the user does not exist', function () {
  70. beforeEach(function () {
  71. this.user_id = ObjectId().toString()
  72. this.req.params = { user_id: this.user_id }
  73. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  74. this.UserInfoController.getPersonalInfo(this.req, this.res, this.next)
  75. })
  76. it('should return 404 to the client', function () {
  77. this.res.statusCode.should.equal(404)
  78. })
  79. })
  80. describe('when the user id is invalid', function () {
  81. beforeEach(function () {
  82. this.user_id = 'invalid'
  83. this.req.params = { user_id: this.user_id }
  84. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  85. this.UserInfoController.getPersonalInfo(this.req, this.res, this.next)
  86. })
  87. it('should return 400 to the client', function () {
  88. this.res.statusCode.should.equal(400)
  89. })
  90. })
  91. })
  92. describe('sendFormattedPersonalInfo', function () {
  93. beforeEach(function () {
  94. this.user = {
  95. _id: ObjectId(),
  96. first_name: 'Douglas',
  97. last_name: 'Adams',
  98. email: 'doug@sharelatex.com',
  99. }
  100. this.formattedInfo = {
  101. id: this.user._id.toString(),
  102. first_name: this.user.first_name,
  103. last_name: this.user.last_name,
  104. email: this.user.email,
  105. }
  106. this.UserInfoController.sendFormattedPersonalInfo(this.user, this.res)
  107. })
  108. it('should send the formatted details back to the client', function () {
  109. this.res.body.should.equal(JSON.stringify(this.formattedInfo))
  110. })
  111. })
  112. describe('formatPersonalInfo', function () {
  113. it('should return the correctly formatted data', function () {
  114. this.user = {
  115. _id: ObjectId(),
  116. first_name: 'Douglas',
  117. last_name: 'Adams',
  118. email: 'doug@sharelatex.com',
  119. password: 'should-not-get-included',
  120. signUpDate: new Date(),
  121. role: 'student',
  122. institution: 'sheffield',
  123. }
  124. expect(
  125. this.UserInfoController.formatPersonalInfo(this.user)
  126. ).to.deep.equal({
  127. id: this.user._id.toString(),
  128. first_name: this.user.first_name,
  129. last_name: this.user.last_name,
  130. email: this.user.email,
  131. signUpDate: this.user.signUpDate,
  132. role: this.user.role,
  133. institution: this.user.institution,
  134. })
  135. })
  136. })
  137. })