AnnotatedSessionController.spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import 'reflect-metadata'
  2. import * as express from 'express'
  3. import { AnnotatedSessionController } from './AnnotatedSessionController'
  4. import { results } from 'inversify-express-utils'
  5. import { DeletePreviousSessionsForUser } from '../../Domain/UseCase/DeletePreviousSessionsForUser'
  6. import { DeleteSessionForUser } from '../../Domain/UseCase/DeleteSessionForUser'
  7. import { RefreshSessionToken } from '../../Domain/UseCase/RefreshSessionToken'
  8. describe('AnnotatedSessionController', () => {
  9. let deleteSessionForUser: DeleteSessionForUser
  10. let deletePreviousSessionsForUser: DeletePreviousSessionsForUser
  11. let refreshSessionToken: RefreshSessionToken
  12. let request: express.Request
  13. let response: express.Response
  14. const createController = () =>
  15. new AnnotatedSessionController(deleteSessionForUser, deletePreviousSessionsForUser, refreshSessionToken)
  16. beforeEach(() => {
  17. deleteSessionForUser = {} as jest.Mocked<DeleteSessionForUser>
  18. deleteSessionForUser.execute = jest.fn().mockReturnValue({ success: true })
  19. deletePreviousSessionsForUser = {} as jest.Mocked<DeletePreviousSessionsForUser>
  20. deletePreviousSessionsForUser.execute = jest.fn()
  21. refreshSessionToken = {} as jest.Mocked<RefreshSessionToken>
  22. refreshSessionToken.execute = jest.fn()
  23. request = {
  24. body: {},
  25. } as jest.Mocked<express.Request>
  26. response = {
  27. locals: {},
  28. } as jest.Mocked<express.Response>
  29. response.status = jest.fn().mockReturnThis()
  30. response.setHeader = jest.fn()
  31. response.send = jest.fn()
  32. })
  33. it('should refresh session tokens', async () => {
  34. request.body.access_token = '123'
  35. request.body.refresh_token = '234'
  36. refreshSessionToken.execute = jest.fn().mockReturnValue({
  37. success: true,
  38. sessionPayload: {
  39. access_token: '1231',
  40. refresh_token: '2341',
  41. access_expiration: 123123,
  42. refresh_expiration: 123123,
  43. },
  44. })
  45. const httpResult = <results.JsonResult>await createController().refresh(request, response)
  46. const result = await httpResult.executeAsync()
  47. expect(await result.content.readAsStringAsync()).toEqual(
  48. '{"session":{"access_token":"1231","refresh_token":"2341","access_expiration":123123,"refresh_expiration":123123}}',
  49. )
  50. })
  51. it('should return bad request if tokens are missing from refresh token request', async () => {
  52. const httpResponse = <results.JsonResult>await createController().refresh(request, response)
  53. expect(httpResponse.statusCode).toEqual(400)
  54. })
  55. it('should return bad request upon failed tokens refreshing', async () => {
  56. request.body.access_token = '123'
  57. request.body.refresh_token = '234'
  58. refreshSessionToken.execute = jest.fn().mockReturnValue({
  59. success: false,
  60. errorTag: 'test',
  61. errorMessage: 'something bad happened',
  62. })
  63. const httpResponse = <results.JsonResult>await createController().refresh(request, response)
  64. expect(httpResponse.json).toEqual({
  65. error: {
  66. tag: 'test',
  67. message: 'something bad happened',
  68. },
  69. })
  70. expect(httpResponse.statusCode).toEqual(400)
  71. })
  72. it('should delete a specific session for current user', async () => {
  73. response.locals = {
  74. user: {
  75. uuid: '123',
  76. },
  77. session: {
  78. uuid: '234',
  79. },
  80. }
  81. request.body.uuid = '123'
  82. const httpResult = <results.JsonResult>await createController().deleteSession(request, response)
  83. const result = await httpResult.executeAsync()
  84. expect(deleteSessionForUser.execute).toBeCalledWith({
  85. userUuid: '123',
  86. sessionUuid: '123',
  87. })
  88. expect(result.statusCode).toEqual(204)
  89. })
  90. it('should not delete a specific session is current session has read only access', async () => {
  91. response.locals = {
  92. user: {
  93. uuid: '123',
  94. },
  95. session: {
  96. uuid: '234',
  97. },
  98. }
  99. request.body.uuid = '123'
  100. response.locals.readOnlyAccess = true
  101. const httpResponse = <results.JsonResult>await createController().deleteSession(request, response)
  102. const result = await httpResponse.executeAsync()
  103. expect(deleteSessionForUser.execute).not.toHaveBeenCalled()
  104. expect(result.statusCode).toEqual(401)
  105. })
  106. it('should not delete a specific session if request is missing params', async () => {
  107. response.locals = {
  108. user: {
  109. uuid: '123',
  110. },
  111. session: {
  112. uuid: '234',
  113. },
  114. }
  115. const httpResponse = <results.JsonResult>await createController().deleteSession(request, response)
  116. expect(deleteSessionForUser.execute).not.toHaveBeenCalled()
  117. expect(httpResponse.statusCode).toEqual(400)
  118. })
  119. it('should not delete a specific session if it is the current session', async () => {
  120. response.locals = {
  121. user: {
  122. uuid: '123',
  123. },
  124. session: {
  125. uuid: '234',
  126. },
  127. }
  128. request.body.uuid = '234'
  129. const httpResponse = <results.JsonResult>await createController().deleteSession(request, response)
  130. expect(deleteSessionForUser.execute).not.toHaveBeenCalled()
  131. expect(httpResponse.statusCode).toEqual(400)
  132. })
  133. it('should respond with failure if deleting a specific session fails', async () => {
  134. response.locals = {
  135. user: {
  136. uuid: '123',
  137. },
  138. session: {
  139. uuid: '234',
  140. },
  141. }
  142. request.body.uuid = '123'
  143. deleteSessionForUser.execute = jest.fn().mockReturnValue({ success: false })
  144. const httpResponse = <results.JsonResult>await createController().deleteSession(request, response)
  145. expect(httpResponse.statusCode).toEqual(400)
  146. })
  147. it('should delete all sessions except current for current user', async () => {
  148. response.locals = {
  149. user: {
  150. uuid: '123',
  151. },
  152. session: {
  153. uuid: '234',
  154. },
  155. }
  156. const httpResult = <results.JsonResult>await createController().deleteAllSessions(request, response)
  157. const result = await httpResult.executeAsync()
  158. expect(deletePreviousSessionsForUser.execute).toHaveBeenCalledWith({
  159. userUuid: '123',
  160. currentSessionUuid: '234',
  161. })
  162. expect(result.statusCode).toEqual(204)
  163. })
  164. it('should not delete all sessions if current sessions has read only access', async () => {
  165. response.locals = {
  166. user: {
  167. uuid: '123',
  168. },
  169. session: {
  170. uuid: '234',
  171. },
  172. }
  173. response.locals.readOnlyAccess = true
  174. const httpResponse = <results.JsonResult>await createController().deleteAllSessions(request, response)
  175. const result = await httpResponse.executeAsync()
  176. expect(deletePreviousSessionsForUser.execute).not.toHaveBeenCalled()
  177. expect(result.statusCode).toEqual(401)
  178. })
  179. it('should return unauthorized if current user is missing', async () => {
  180. response.locals = {
  181. session: {
  182. uuid: '234',
  183. },
  184. }
  185. const httpResponse = <results.JsonResult>await createController().deleteAllSessions(request, response)
  186. expect(httpResponse.json).toEqual({ error: { message: 'No session exists with the provided identifier.' } })
  187. expect(httpResponse.statusCode).toEqual(401)
  188. })
  189. })