2024-09-13 19:11:41 +00:00
|
|
|
import 'reflect-metadata'
|
|
|
|
|
import { Argon2id } from 'oslo/password'
|
|
|
|
|
import { container } from 'tsyringe'
|
|
|
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
|
|
|
|
|
import { HashingService } from '../services/hashing.service'
|
|
|
|
|
import { TokensService } from '../services/tokens.service'
|
2024-08-13 23:42:10 +00:00
|
|
|
|
|
|
|
|
describe('TokensService', () => {
|
2024-09-13 19:11:41 +00:00
|
|
|
let service: TokensService
|
|
|
|
|
const hashingService = vi.mocked(HashingService.prototype)
|
2024-08-13 23:42:10 +00:00
|
|
|
|
|
|
|
|
beforeAll(() => {
|
2024-09-13 19:11:41 +00:00
|
|
|
service = container.register<HashingService>(HashingService, { useValue: hashingService }).resolve(TokensService)
|
|
|
|
|
})
|
2024-08-13 23:42:10 +00:00
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
|
vi.resetAllMocks()
|
2024-09-13 19:11:41 +00:00
|
|
|
})
|
2024-08-13 23:42:10 +00:00
|
|
|
|
|
|
|
|
describe('Generate Token', () => {
|
2024-09-13 19:11:41 +00:00
|
|
|
const hashedPassword = new Argon2id().hash('111')
|
2024-08-13 23:42:10 +00:00
|
|
|
|
2024-09-13 19:11:41 +00:00
|
|
|
hashingService.hash = vi.fn().mockResolvedValue(hashedPassword)
|
|
|
|
|
hashingService.verify = vi.fn().mockResolvedValue(true)
|
2024-08-13 23:42:10 +00:00
|
|
|
|
2024-09-13 19:11:41 +00:00
|
|
|
const spy_hashingService_hash = vi.spyOn(hashingService, 'hash')
|
|
|
|
|
const spy_hashingService_verify = vi.spyOn(hashingService, 'verify')
|
2024-08-13 23:42:10 +00:00
|
|
|
|
|
|
|
|
it('should resolve', async () => {
|
2024-09-13 19:11:41 +00:00
|
|
|
expect(service.createHashedToken('111')).resolves.string
|
2024-08-13 23:42:10 +00:00
|
|
|
})
|
|
|
|
|
it('should generate a token that is verifiable', async () => {
|
2024-09-13 19:11:41 +00:00
|
|
|
const token = await service.createHashedToken('111')
|
|
|
|
|
expect(token).not.toBeUndefined()
|
|
|
|
|
expect(token).not.toBeNull()
|
|
|
|
|
const verifiable = await service.verifyHashedToken(token, '111')
|
|
|
|
|
expect(verifiable).toBeTruthy()
|
|
|
|
|
})
|
2024-08-13 23:42:10 +00:00
|
|
|
|
|
|
|
|
it('should generate a hashed token', async () => {
|
2024-09-13 19:11:41 +00:00
|
|
|
expect(spy_hashingService_hash).toHaveBeenCalledTimes(2)
|
2024-08-13 23:42:10 +00:00
|
|
|
})
|
|
|
|
|
it('should verify a hashed token', async () => {
|
2024-09-13 19:11:41 +00:00
|
|
|
expect(spy_hashingService_verify).toHaveBeenCalledTimes(1)
|
2024-08-13 23:42:10 +00:00
|
|
|
})
|
2024-09-13 19:11:41 +00:00
|
|
|
})
|
|
|
|
|
})
|