UserInfoControllerTests.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { assert, expect } = require('chai')
  15. const modulePath = '../../../../app/src/Features/User/UserInfoController.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. const events = require('events')
  18. const MockResponse = require('../helpers/MockResponse')
  19. const MockRequest = require('../helpers/MockRequest')
  20. const { ObjectId } = require('mongodb')
  21. describe('UserInfoController', function () {
  22. beforeEach(function () {
  23. this.UserDeleter = { deleteUser: sinon.stub().callsArgWith(1) }
  24. this.UserUpdater = { updatePersonalInfo: sinon.stub() }
  25. this.UserGetter = {}
  26. this.UserInfoController = SandboxedModule.require(modulePath, {
  27. requires: {
  28. mongodb: { ObjectId },
  29. './UserGetter': this.UserGetter,
  30. './UserUpdater': this.UserUpdater,
  31. './UserDeleter': this.UserDeleter,
  32. '../Authentication/SessionManager': (this.SessionManager = {
  33. getLoggedInUserId: sinon.stub(),
  34. }),
  35. },
  36. })
  37. this.req = new MockRequest()
  38. this.res = new MockResponse()
  39. return (this.next = sinon.stub())
  40. })
  41. describe('getLoggedInUsersPersonalInfo', function () {
  42. beforeEach(function () {
  43. this.user = { _id: ObjectId() }
  44. this.req.user = this.user
  45. this.req.session.user = this.user
  46. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  47. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  48. this.SessionManager.getLoggedInUserId = sinon
  49. .stub()
  50. .returns(this.user._id)
  51. return this.UserInfoController.getLoggedInUsersPersonalInfo(
  52. this.req,
  53. this.res,
  54. this.next
  55. )
  56. })
  57. it('should call sendFormattedPersonalInfo', function () {
  58. return this.UserInfoController.sendFormattedPersonalInfo
  59. .calledWith(this.user, this.res, this.next)
  60. .should.equal(true)
  61. })
  62. })
  63. describe('getPersonalInfo', function () {
  64. describe('when the user exists with sharelatex id', function () {
  65. beforeEach(function () {
  66. this.user_id = ObjectId().toString()
  67. this.user = { _id: ObjectId(this.user_id) }
  68. this.req.params = { user_id: this.user_id }
  69. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  70. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  71. return this.UserInfoController.getPersonalInfo(
  72. this.req,
  73. this.res,
  74. this.next
  75. )
  76. })
  77. it('should look up the user in the database', function () {
  78. return this.UserGetter.getUser
  79. .calledWith(
  80. { _id: ObjectId(this.user_id) },
  81. { _id: true, first_name: true, last_name: true, email: true }
  82. )
  83. .should.equal(true)
  84. })
  85. it('should send the formatted details back to the client', function () {
  86. return this.UserInfoController.sendFormattedPersonalInfo
  87. .calledWith(this.user, this.res, this.next)
  88. .should.equal(true)
  89. })
  90. })
  91. describe('when the user exists with overleaf id', function () {
  92. beforeEach(function () {
  93. this.user_id = 12345
  94. this.user = {
  95. _id: ObjectId(),
  96. overleaf: {
  97. id: this.user_id,
  98. },
  99. }
  100. this.req.params = { user_id: this.user_id.toString() }
  101. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  102. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  103. return this.UserInfoController.getPersonalInfo(
  104. this.req,
  105. this.res,
  106. this.next
  107. )
  108. })
  109. it('should look up the user in the database', function () {
  110. return this.UserGetter.getUser
  111. .calledWith(
  112. { 'overleaf.id': this.user_id },
  113. { _id: true, first_name: true, last_name: true, email: true }
  114. )
  115. .should.equal(true)
  116. })
  117. it('should send the formatted details back to the client', function () {
  118. return this.UserInfoController.sendFormattedPersonalInfo
  119. .calledWith(this.user, this.res, this.next)
  120. .should.equal(true)
  121. })
  122. })
  123. describe('when the user does not exist', function () {
  124. beforeEach(function () {
  125. this.user_id = ObjectId().toString()
  126. this.req.params = { user_id: this.user_id }
  127. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  128. return this.UserInfoController.getPersonalInfo(
  129. this.req,
  130. this.res,
  131. this.next
  132. )
  133. })
  134. it('should return 404 to the client', function () {
  135. return this.res.statusCode.should.equal(404)
  136. })
  137. })
  138. describe('when the user id is invalid', function () {
  139. beforeEach(function () {
  140. this.user_id = 'invalid'
  141. this.req.params = { user_id: this.user_id }
  142. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  143. return this.UserInfoController.getPersonalInfo(
  144. this.req,
  145. this.res,
  146. this.next
  147. )
  148. })
  149. it('should return 400 to the client', function () {
  150. return this.res.statusCode.should.equal(400)
  151. })
  152. })
  153. })
  154. describe('sendFormattedPersonalInfo', function () {
  155. beforeEach(function () {
  156. this.user = {
  157. _id: ObjectId(),
  158. first_name: 'Douglas',
  159. last_name: 'Adams',
  160. email: 'doug@sharelatex.com',
  161. }
  162. this.formattedInfo = {
  163. id: this.user._id.toString(),
  164. first_name: this.user.first_name,
  165. last_name: this.user.last_name,
  166. email: this.user.email,
  167. }
  168. this.UserInfoController.formatPersonalInfo = sinon
  169. .stub()
  170. .returns(this.formattedInfo)
  171. return this.UserInfoController.sendFormattedPersonalInfo(
  172. this.user,
  173. this.res
  174. )
  175. })
  176. it('should format the user details for the response', function () {
  177. return this.UserInfoController.formatPersonalInfo
  178. .calledWith(this.user)
  179. .should.equal(true)
  180. })
  181. it('should send the formatted details back to the client', function () {
  182. return this.res.body.should.equal(JSON.stringify(this.formattedInfo))
  183. })
  184. })
  185. describe('formatPersonalInfo', function () {
  186. it('should return the correctly formatted data', function () {
  187. this.user = {
  188. _id: ObjectId(),
  189. first_name: 'Douglas',
  190. last_name: 'Adams',
  191. email: 'doug@sharelatex.com',
  192. password: 'should-not-get-included',
  193. signUpDate: new Date(),
  194. role: 'student',
  195. institution: 'sheffield',
  196. }
  197. return expect(
  198. this.UserInfoController.formatPersonalInfo(this.user)
  199. ).to.deep.equal({
  200. id: this.user._id.toString(),
  201. first_name: this.user.first_name,
  202. last_name: this.user.last_name,
  203. email: this.user.email,
  204. signUpDate: this.user.signUpDate,
  205. role: this.user.role,
  206. institution: this.user.institution,
  207. })
  208. })
  209. })
  210. })