|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { RefreshTokensService } from './refresh-tokens.service'; |
| 3 | +import { PrismaService } from '../prisma/prisma.service'; |
| 4 | +import { JwtModule } from '@nestjs/jwt'; |
| 5 | +import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'; |
| 6 | +import { ConfigModule } from '@nestjs/config'; |
| 7 | +import { getExpiryDate } from '../utils/date'; |
| 8 | +import { v4 as uuid } from 'uuid'; |
| 9 | + |
| 10 | +jest.useFakeTimers(); |
| 11 | + |
| 12 | +describe('RefreshTokensService', () => { |
| 13 | + let service: RefreshTokensService; |
| 14 | + let prisma: PrismaService; |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + const module: TestingModule = await Test.createTestingModule({ |
| 18 | + imports: [JwtModule, ConfigModule.forRoot()], |
| 19 | + providers: [ |
| 20 | + RefreshTokensService, |
| 21 | + { |
| 22 | + provide: PrismaService, |
| 23 | + useValue: { |
| 24 | + refreshToken: { |
| 25 | + create: jest.fn(), |
| 26 | + delete: jest.fn(), |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + ], |
| 31 | + }).compile(); |
| 32 | + |
| 33 | + service = module.get<RefreshTokensService>(RefreshTokensService); |
| 34 | + prisma = module.get<PrismaService>(PrismaService); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.clearAllTimers(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('createRefreshToken created', async () => { |
| 42 | + const TWO_WEEK = 14; |
| 43 | + |
| 44 | + const testUserUuid = uuid(); |
| 45 | + const twoWeek = new Date(); |
| 46 | + twoWeek.setDate(twoWeek.getDate() + TWO_WEEK); |
| 47 | + |
| 48 | + (prisma.refreshToken.create as jest.Mock).mockImplementation( |
| 49 | + async ({ data }) => { |
| 50 | + return { |
| 51 | + id: 0, |
| 52 | + token: data.token, |
| 53 | + expiryDate: data.expiryDate, |
| 54 | + userUuid: data.userUuid, |
| 55 | + }; |
| 56 | + }, |
| 57 | + ); |
| 58 | + |
| 59 | + const token = await service.createRefreshToken(testUserUuid); |
| 60 | + |
| 61 | + expect(token.token).toMatch( |
| 62 | + /^[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+$/, |
| 63 | + ); |
| 64 | + expect(token.expiryDate.toISOString()).toBe(twoWeek.toISOString()); |
| 65 | + expect(token.userUuid).toBe(testUserUuid); |
| 66 | + }); |
| 67 | + |
| 68 | + it('deleteRefreshToken deleted', async () => { |
| 69 | + const testToken = { |
| 70 | + id: 0, |
| 71 | + token: 'Token', |
| 72 | + expiryDate: getExpiryDate({ week: 2 }), |
| 73 | + userUuid: 'userId', |
| 74 | + }; |
| 75 | + (prisma.refreshToken.delete as jest.Mock).mockResolvedValue(testToken); |
| 76 | + |
| 77 | + const token = service.deleteRefreshToken(testToken.token); |
| 78 | + |
| 79 | + await expect(token).resolves.toEqual(testToken); |
| 80 | + }); |
| 81 | + |
| 82 | + it('deleteRefreshToken not found', async () => { |
| 83 | + (prisma.refreshToken.delete as jest.Mock).mockRejectedValue( |
| 84 | + new PrismaClientKnownRequestError( |
| 85 | + 'An operation failed because it depends on one or more records that were required but not found. Record to delete not found.', |
| 86 | + { code: 'P2025', clientVersion: '' }, |
| 87 | + ), |
| 88 | + ); |
| 89 | + |
| 90 | + const token = service.deleteRefreshToken('Token'); |
| 91 | + |
| 92 | + await expect(token).resolves.toBeNull(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments