UserInfoControllerTests.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 chai = require('chai')
  15. const { assert } = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../../app/src/Features/User/UserInfoController.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. const events = require('events')
  21. const MockResponse = require('../helpers/MockResponse')
  22. const MockRequest = require('../helpers/MockRequest')
  23. const { ObjectId } = require('mongodb')
  24. describe('UserInfoController', function() {
  25. beforeEach(function() {
  26. this.UserDeleter = { deleteUser: sinon.stub().callsArgWith(1) }
  27. this.UserUpdater = { updatePersonalInfo: sinon.stub() }
  28. this.sanitizer = {
  29. escape(v) {
  30. return v
  31. }
  32. }
  33. sinon.spy(this.sanitizer, 'escape')
  34. this.UserGetter = {}
  35. this.UserInfoController = SandboxedModule.require(modulePath, {
  36. globals: {
  37. console: console
  38. },
  39. requires: {
  40. mongodb: { ObjectId },
  41. './UserGetter': this.UserGetter,
  42. './UserUpdater': this.UserUpdater,
  43. './UserDeleter': this.UserDeleter,
  44. 'logger-sharelatex': {
  45. log() {}
  46. },
  47. sanitizer: this.sanitizer,
  48. '../Authentication/AuthenticationController': (this.AuthenticationController = {
  49. getLoggedInUserId: sinon.stub()
  50. })
  51. }
  52. })
  53. this.req = new MockRequest()
  54. this.res = new MockResponse()
  55. return (this.next = sinon.stub())
  56. })
  57. describe('getLoggedInUsersPersonalInfo', function() {
  58. beforeEach(function() {
  59. this.user = { _id: ObjectId() }
  60. this.req.user = this.user
  61. this.req.session.user = this.user
  62. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  63. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  64. this.AuthenticationController.getLoggedInUserId = sinon
  65. .stub()
  66. .returns(this.user._id)
  67. return this.UserInfoController.getLoggedInUsersPersonalInfo(
  68. this.req,
  69. this.res,
  70. this.next
  71. )
  72. })
  73. it('should call sendFormattedPersonalInfo', function() {
  74. return this.UserInfoController.sendFormattedPersonalInfo
  75. .calledWith(this.user, this.res, this.next)
  76. .should.equal(true)
  77. })
  78. })
  79. describe('getPersonalInfo', function() {
  80. describe('when the user exists with sharelatex id', function() {
  81. beforeEach(function() {
  82. this.user_id = ObjectId().toString()
  83. this.user = { _id: ObjectId(this.user_id) }
  84. this.req.params = { user_id: this.user_id }
  85. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  86. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  87. return this.UserInfoController.getPersonalInfo(
  88. this.req,
  89. this.res,
  90. this.next
  91. )
  92. })
  93. it('should look up the user in the database', function() {
  94. return this.UserGetter.getUser
  95. .calledWith(
  96. { _id: ObjectId(this.user_id) },
  97. { _id: true, first_name: true, last_name: true, email: true }
  98. )
  99. .should.equal(true)
  100. })
  101. it('should send the formatted details back to the client', function() {
  102. return this.UserInfoController.sendFormattedPersonalInfo
  103. .calledWith(this.user, this.res, this.next)
  104. .should.equal(true)
  105. })
  106. })
  107. describe('when the user exists with overleaf id', function() {
  108. beforeEach(function() {
  109. this.user_id = 12345
  110. this.user = {
  111. _id: ObjectId(),
  112. overleaf: {
  113. id: this.user_id
  114. }
  115. }
  116. this.req.params = { user_id: this.user_id.toString() }
  117. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  118. this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  119. return this.UserInfoController.getPersonalInfo(
  120. this.req,
  121. this.res,
  122. this.next
  123. )
  124. })
  125. it('should look up the user in the database', function() {
  126. return this.UserGetter.getUser
  127. .calledWith(
  128. { 'overleaf.id': this.user_id },
  129. { _id: true, first_name: true, last_name: true, email: true }
  130. )
  131. .should.equal(true)
  132. })
  133. it('should send the formatted details back to the client', function() {
  134. return this.UserInfoController.sendFormattedPersonalInfo
  135. .calledWith(this.user, this.res, this.next)
  136. .should.equal(true)
  137. })
  138. })
  139. describe('when the user does not exist', function() {
  140. beforeEach(function() {
  141. this.user_id = ObjectId().toString()
  142. this.req.params = { user_id: this.user_id }
  143. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  144. return this.UserInfoController.getPersonalInfo(
  145. this.req,
  146. this.res,
  147. this.next
  148. )
  149. })
  150. it('should return 404 to the client', function() {
  151. return this.res.statusCode.should.equal(404)
  152. })
  153. })
  154. describe('when the user id is invalid', function() {
  155. beforeEach(function() {
  156. this.user_id = 'invalid'
  157. this.req.params = { user_id: this.user_id }
  158. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  159. return this.UserInfoController.getPersonalInfo(
  160. this.req,
  161. this.res,
  162. this.next
  163. )
  164. })
  165. it('should return 400 to the client', function() {
  166. return this.res.statusCode.should.equal(400)
  167. })
  168. })
  169. })
  170. describe('sendFormattedPersonalInfo', function() {
  171. beforeEach(function() {
  172. this.user = {
  173. _id: ObjectId(),
  174. first_name: 'Douglas',
  175. last_name: 'Adams',
  176. email: 'doug@sharelatex.com'
  177. }
  178. this.formattedInfo = {
  179. id: this.user._id.toString(),
  180. first_name: this.user.first_name,
  181. last_name: this.user.last_name,
  182. email: this.user.email
  183. }
  184. this.UserInfoController.formatPersonalInfo = sinon
  185. .stub()
  186. .returns(this.formattedInfo)
  187. return this.UserInfoController.sendFormattedPersonalInfo(
  188. this.user,
  189. this.res
  190. )
  191. })
  192. it('should format the user details for the response', function() {
  193. return this.UserInfoController.formatPersonalInfo
  194. .calledWith(this.user)
  195. .should.equal(true)
  196. })
  197. it('should send the formatted details back to the client', function() {
  198. return this.res.body.should.equal(JSON.stringify(this.formattedInfo))
  199. })
  200. })
  201. describe('formatPersonalInfo', function() {
  202. it('should return the correctly formatted data', function() {
  203. this.user = {
  204. _id: ObjectId(),
  205. first_name: 'Douglas',
  206. last_name: 'Adams',
  207. email: 'doug@sharelatex.com',
  208. password: 'should-not-get-included',
  209. signUpDate: new Date(),
  210. role: 'student',
  211. institution: 'sheffield'
  212. }
  213. return expect(
  214. this.UserInfoController.formatPersonalInfo(this.user)
  215. ).to.deep.equal({
  216. id: this.user._id.toString(),
  217. first_name: this.user.first_name,
  218. last_name: this.user.last_name,
  219. email: this.user.email,
  220. signUpDate: this.user.signUpDate,
  221. role: this.user.role,
  222. institution: this.user.institution
  223. })
  224. })
  225. })
  226. })