DeleteOtherSessionsForUser.spec.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'reflect-metadata'
  2. import { Session } from '../Session/Session'
  3. import { SessionRepositoryInterface } from '../Session/SessionRepositoryInterface'
  4. import { SessionServiceInterface } from '../Session/SessionServiceInterface'
  5. import { DeleteOtherSessionsForUser } from './DeleteOtherSessionsForUser'
  6. describe('DeleteOtherSessionsForUser', () => {
  7. let sessionRepository: SessionRepositoryInterface
  8. let sessionService: SessionServiceInterface
  9. let session: Session
  10. let currentSession: Session
  11. const createUseCase = () => new DeleteOtherSessionsForUser(sessionRepository, sessionService)
  12. beforeEach(() => {
  13. session = {} as jest.Mocked<Session>
  14. session.uuid = '00000000-0000-0000-0000-000000000000'
  15. currentSession = {} as jest.Mocked<Session>
  16. currentSession.uuid = '00000000-0000-0000-0000-000000000001'
  17. sessionRepository = {} as jest.Mocked<SessionRepositoryInterface>
  18. sessionRepository.deleteAllByUserUuidExceptOne = jest.fn()
  19. sessionRepository.findAllByUserUuid = jest.fn().mockReturnValue([session, currentSession])
  20. sessionService = {} as jest.Mocked<SessionServiceInterface>
  21. sessionService.createRevokedSession = jest.fn()
  22. })
  23. it('should delete all sessions except current for a given user', async () => {
  24. const result = await createUseCase().execute({
  25. userUuid: '00000000-0000-0000-0000-000000000000',
  26. currentSessionUuid: '00000000-0000-0000-0000-000000000001',
  27. markAsRevoked: true,
  28. })
  29. expect(result.isFailed()).toBeFalsy()
  30. expect(sessionRepository.deleteAllByUserUuidExceptOne).toHaveBeenCalled()
  31. expect(sessionService.createRevokedSession).toHaveBeenCalledWith(session)
  32. expect(sessionService.createRevokedSession).not.toHaveBeenCalledWith(currentSession)
  33. })
  34. it('should delete all sessions except current for a given user without marking as revoked', async () => {
  35. const result = await createUseCase().execute({
  36. userUuid: '00000000-0000-0000-0000-000000000000',
  37. currentSessionUuid: '00000000-0000-0000-0000-000000000001',
  38. markAsRevoked: false,
  39. })
  40. expect(result.isFailed()).toBeFalsy()
  41. expect(sessionRepository.deleteAllByUserUuidExceptOne).toHaveBeenCalled()
  42. expect(sessionService.createRevokedSession).not.toHaveBeenCalled()
  43. })
  44. it('should not delete any sessions if the user uuid is invalid', async () => {
  45. const result = await createUseCase().execute({
  46. userUuid: 'invalid',
  47. currentSessionUuid: '00000000-0000-0000-0000-000000000001',
  48. markAsRevoked: true,
  49. })
  50. expect(result.isFailed()).toBeTruthy()
  51. expect(sessionRepository.deleteAllByUserUuidExceptOne).not.toHaveBeenCalled()
  52. expect(sessionService.createRevokedSession).not.toHaveBeenCalled()
  53. })
  54. it('should not delete any sessions if the current session uuid is invalid', async () => {
  55. const result = await createUseCase().execute({
  56. userUuid: '00000000-0000-0000-0000-000000000000',
  57. currentSessionUuid: 'invalid',
  58. markAsRevoked: true,
  59. })
  60. expect(result.isFailed()).toBeTruthy()
  61. expect(sessionRepository.deleteAllByUserUuidExceptOne).not.toHaveBeenCalled()
  62. expect(sessionService.createRevokedSession).not.toHaveBeenCalled()
  63. })
  64. })