AuthMiddleware.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'reflect-metadata'
  2. import * as winston from 'winston'
  3. import { AuthMiddleware } from './AuthMiddleware'
  4. import { NextFunction, Request, Response } from 'express'
  5. import { sign } from 'jsonwebtoken'
  6. import { RoleName } from '@standardnotes/common'
  7. describe('AuthMiddleware', () => {
  8. let logger: winston.Logger
  9. const jwtSecret = 'auth_jwt_secret'
  10. let request: Request
  11. let response: Response
  12. let next: NextFunction
  13. const createMiddleware = () => new AuthMiddleware(jwtSecret, logger)
  14. beforeEach(() => {
  15. logger = {} as jest.Mocked<winston.Logger>
  16. logger.info = jest.fn()
  17. logger.debug = jest.fn()
  18. logger.warn = jest.fn()
  19. logger.error = jest.fn()
  20. request = {
  21. headers: {},
  22. } as jest.Mocked<Request>
  23. request.header = jest.fn()
  24. response = {
  25. locals: {},
  26. } as jest.Mocked<Response>
  27. response.status = jest.fn().mockReturnThis()
  28. response.send = jest.fn()
  29. next = jest.fn()
  30. })
  31. it('should authorize user from an auth JWT token if present', async () => {
  32. const authToken = sign(
  33. {
  34. user: { uuid: '123' },
  35. session: { uuid: '234' },
  36. roles: [
  37. {
  38. uuid: '1-2-3',
  39. name: RoleName.CoreUser,
  40. },
  41. {
  42. uuid: '2-3-4',
  43. name: RoleName.ProUser,
  44. },
  45. ],
  46. analyticsId: 123,
  47. permissions: [],
  48. },
  49. jwtSecret,
  50. { algorithm: 'HS256' },
  51. )
  52. request.header = jest.fn().mockReturnValue(authToken)
  53. await createMiddleware().handler(request, response, next)
  54. expect(response.locals.user).toEqual({ uuid: '123' })
  55. expect(response.locals.roleNames).toEqual(['CORE_USER', 'PRO_USER'])
  56. expect(response.locals.session).toEqual({ uuid: '234' })
  57. expect(response.locals.readOnlyAccess).toBeFalsy()
  58. expect(response.locals.analyticsId).toEqual(123)
  59. expect(next).toHaveBeenCalled()
  60. })
  61. it('should authorize user from an auth JWT token if present with read only access', async () => {
  62. const authToken = sign(
  63. {
  64. user: { uuid: '123' },
  65. session: {
  66. uuid: '234',
  67. readonly_access: true,
  68. },
  69. roles: [
  70. {
  71. uuid: '1-2-3',
  72. name: RoleName.CoreUser,
  73. },
  74. {
  75. uuid: '2-3-4',
  76. name: RoleName.ProUser,
  77. },
  78. ],
  79. analyticsId: 123,
  80. permissions: [],
  81. },
  82. jwtSecret,
  83. { algorithm: 'HS256' },
  84. )
  85. request.header = jest.fn().mockReturnValue(authToken)
  86. await createMiddleware().handler(request, response, next)
  87. expect(response.locals.user).toEqual({ uuid: '123' })
  88. expect(response.locals.roleNames).toEqual(['CORE_USER', 'PRO_USER'])
  89. expect(response.locals.session).toEqual({ uuid: '234', readonly_access: true })
  90. expect(response.locals.readOnlyAccess).toBeTruthy()
  91. expect(response.locals.analyticsId).toEqual(123)
  92. expect(next).toHaveBeenCalled()
  93. })
  94. it('should not authorize user from an auth JWT token if it is invalid', async () => {
  95. const authToken = sign(
  96. {
  97. user: { uuid: '123' },
  98. session: { uuid: '234' },
  99. roles: [],
  100. permissions: [],
  101. },
  102. jwtSecret,
  103. { algorithm: 'HS256', notBefore: '2 days' },
  104. )
  105. request.header = jest.fn().mockReturnValue(authToken)
  106. await createMiddleware().handler(request, response, next)
  107. expect(response.status).toHaveBeenCalledWith(401)
  108. expect(next).not.toHaveBeenCalled()
  109. })
  110. it('should not authorize if authorization header is missing', async () => {
  111. await createMiddleware().handler(request, response, next)
  112. expect(response.status).toHaveBeenCalledWith(401)
  113. expect(next).not.toHaveBeenCalled()
  114. })
  115. })