UserInfoControllerTests.js 7.5 KB

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