UserInfoController.test.mjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.mjs'
  4. import MockRequest from '../helpers/MockRequest.mjs'
  5. import mongodb from 'mongodb-legacy'
  6. const modulePath = '../../../../app/src/Features/User/UserInfoController.mjs'
  7. const { ObjectId } = mongodb
  8. describe('UserInfoController', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.UserDeleter = { deleteUser: sinon.stub().callsArgWith(1) }
  11. ctx.UserUpdater = { updatePersonalInfo: sinon.stub() }
  12. ctx.UserGetter = {
  13. promises: {
  14. getUserFeatures: sinon.stub(),
  15. },
  16. }
  17. vi.doMock('mongodb-legacy', () => ({
  18. default: { ObjectId },
  19. }))
  20. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  21. default: ctx.UserGetter,
  22. }))
  23. vi.doMock('../../../../app/src/Features/User/UserUpdater', () => ({
  24. default: ctx.UserUpdater,
  25. }))
  26. vi.doMock('../../../../app/src/Features/User/UserDeleter', () => ({
  27. default: ctx.UserDeleter,
  28. }))
  29. vi.doMock(
  30. '../../../../app/src/Features/Authentication/SessionManager',
  31. () => ({
  32. default: (ctx.SessionManager = {
  33. getLoggedInUserId: sinon.stub(),
  34. }),
  35. })
  36. )
  37. ctx.UserInfoController = (await import(modulePath)).default
  38. ctx.req = new MockRequest(vi)
  39. ctx.res = new MockResponse(vi)
  40. ctx.next = sinon.stub()
  41. })
  42. describe('getPersonalInfo', function () {
  43. describe('when the user exists with mongo id', function () {
  44. beforeEach(function (ctx) {
  45. ctx.user_id = new ObjectId().toString()
  46. ctx.user = { _id: new ObjectId(ctx.user_id) }
  47. ctx.req.params = { user_id: ctx.user_id }
  48. ctx.UserGetter.getUser = sinon.stub().callsArgWith(2, null, ctx.user)
  49. ctx.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
  50. ctx.UserInfoController.getPersonalInfo(ctx.req, ctx.res, ctx.next)
  51. })
  52. it('should look up the user in the database', function (ctx) {
  53. ctx.UserGetter.getUser
  54. .calledWith(
  55. { _id: new ObjectId(ctx.user_id) },
  56. { _id: true, first_name: true, last_name: true, email: true }
  57. )
  58. .should.equal(true)
  59. })
  60. })
  61. describe('when the user exists with overleaf id', function () {
  62. beforeEach(function (ctx) {
  63. ctx.user_id = 12345
  64. ctx.user = {
  65. _id: new ObjectId(),
  66. overleaf: {
  67. id: ctx.user_id,
  68. },
  69. }
  70. ctx.req.params = { user_id: ctx.user_id.toString() }
  71. ctx.UserGetter.getUser = sinon.stub().callsArgWith(2, null, ctx.user)
  72. ctx.UserInfoController.getPersonalInfo(ctx.req, ctx.res, ctx.next)
  73. })
  74. it('should look up the user in the database', function (ctx) {
  75. ctx.UserGetter.getUser
  76. .calledWith(
  77. { 'overleaf.id': ctx.user_id },
  78. { _id: true, first_name: true, last_name: true, email: true }
  79. )
  80. .should.equal(true)
  81. })
  82. })
  83. describe('when the user does not exist', function () {
  84. beforeEach(function (ctx) {
  85. ctx.user_id = new ObjectId().toString()
  86. ctx.req.params = { user_id: ctx.user_id }
  87. ctx.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  88. ctx.UserInfoController.getPersonalInfo(ctx.req, ctx.res, ctx.next)
  89. })
  90. it('should return 404 to the client', function (ctx) {
  91. ctx.res.statusCode.should.equal(404)
  92. })
  93. })
  94. describe('when the user id is invalid', function () {
  95. beforeEach(function (ctx) {
  96. ctx.user_id = 'invalid'
  97. ctx.req.params = { user_id: ctx.user_id }
  98. ctx.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  99. ctx.UserInfoController.getPersonalInfo(ctx.req, ctx.res, ctx.next)
  100. })
  101. it('should return 400 to the client', function (ctx) {
  102. ctx.res.statusCode.should.equal(400)
  103. })
  104. })
  105. })
  106. describe('sendFormattedPersonalInfo', function () {
  107. beforeEach(function (ctx) {
  108. ctx.user = {
  109. _id: new ObjectId(),
  110. first_name: 'Douglas',
  111. last_name: 'Adams',
  112. email: 'doug@overleaf.com',
  113. }
  114. ctx.formattedInfo = {
  115. id: ctx.user._id.toString(),
  116. first_name: ctx.user.first_name,
  117. last_name: ctx.user.last_name,
  118. email: ctx.user.email,
  119. }
  120. ctx.UserInfoController.sendFormattedPersonalInfo(ctx.user, ctx.res)
  121. })
  122. it('should send the formatted details back to the client', function (ctx) {
  123. ctx.res.body.should.equal(JSON.stringify(ctx.formattedInfo))
  124. })
  125. })
  126. describe('formatPersonalInfo', function () {
  127. it('should return the correctly formatted data', function (ctx) {
  128. ctx.user = {
  129. _id: new ObjectId(),
  130. first_name: 'Douglas',
  131. last_name: 'Adams',
  132. email: 'doug@overleaf.com',
  133. password: 'should-not-get-included',
  134. signUpDate: new Date(),
  135. role: 'student',
  136. institution: 'sheffield',
  137. }
  138. expect(ctx.UserInfoController.formatPersonalInfo(ctx.user)).to.deep.equal(
  139. {
  140. id: ctx.user._id.toString(),
  141. first_name: ctx.user.first_name,
  142. last_name: ctx.user.last_name,
  143. email: ctx.user.email,
  144. signUpDate: ctx.user.signUpDate,
  145. role: ctx.user.role,
  146. institution: ctx.user.institution,
  147. }
  148. )
  149. })
  150. })
  151. describe('getUserFeatures', function () {
  152. describe('when the user is logged in', function () {
  153. beforeEach(async function (ctx) {
  154. ctx.user_id = new ObjectId().toString()
  155. ctx.features = {
  156. collaborators: 10,
  157. trackChanges: true,
  158. references: true,
  159. }
  160. ctx.SessionManager.getLoggedInUserId.returns(ctx.user_id)
  161. ctx.UserGetter.promises.getUserFeatures.resolves(ctx.features)
  162. await ctx.UserInfoController.getUserFeatures(ctx.req, ctx.res, ctx.next)
  163. })
  164. it('should fetch the user features', function (ctx) {
  165. expect(ctx.UserGetter.promises.getUserFeatures.callCount).to.equal(1)
  166. expect(
  167. ctx.UserGetter.promises.getUserFeatures.calledWith(ctx.user_id)
  168. ).to.equal(true)
  169. })
  170. it('should return the features as JSON', function (ctx) {
  171. expect(ctx.res.json).toHaveBeenCalledTimes(1)
  172. expect(ctx.res.json).toHaveBeenCalledWith(ctx.features)
  173. })
  174. })
  175. describe('when the user is not logged in', function () {
  176. beforeEach(async function (ctx) {
  177. ctx.SessionManager.getLoggedInUserId.returns(null)
  178. await ctx.UserInfoController.getUserFeatures(ctx.req, ctx.res, ctx.next)
  179. })
  180. it('should call next with an error', function (ctx) {
  181. expect(ctx.next.callCount).to.equal(1)
  182. expect(ctx.next.firstCall.args[0]).to.be.an.instanceof(Error)
  183. expect(ctx.next.firstCall.args[0].message).to.equal(
  184. 'User is not logged in'
  185. )
  186. })
  187. })
  188. describe('when fetching features fails', function () {
  189. beforeEach(async function (ctx) {
  190. ctx.user_id = new ObjectId().toString()
  191. ctx.error = new Error('something went wrong')
  192. ctx.SessionManager.getLoggedInUserId.returns(ctx.user_id)
  193. ctx.UserGetter.promises.getUserFeatures.rejects(ctx.error)
  194. await ctx.UserInfoController.getUserFeatures(ctx.req, ctx.res, ctx.next)
  195. })
  196. it('should call next with the error', function (ctx) {
  197. expect(ctx.next.callCount).to.equal(1)
  198. expect(ctx.next.firstCall.args[0]).to.equal(ctx.error)
  199. })
  200. })
  201. })
  202. })