admin login fixed.
Build & Push / Pipeline Tests (push) Failing after 1m34s
Build & Push / Build & Push Docker Image (push) Has been skipped
Test / Type Check (all packages) (push) Successful in 56s
Test / API Unit Tests (push) Successful in 1m8s
Test / Homepage Unit Tests (push) Successful in 47s
Test / Carplace Unit Tests (push) Successful in 43s
Test / Admin Unit Tests (push) Successful in 46s
Test / Dashboard Unit Tests (push) Failing after 43s
Test / API Integration Tests (push) Successful in 1m8s
Build & Push / Pipeline Tests (push) Failing after 1m34s
Build & Push / Build & Push Docker Image (push) Has been skipped
Test / Type Check (all packages) (push) Successful in 56s
Test / API Unit Tests (push) Successful in 1m8s
Test / Homepage Unit Tests (push) Successful in 47s
Test / Carplace Unit Tests (push) Successful in 43s
Test / Admin Unit Tests (push) Successful in 46s
Test / Dashboard Unit Tests (push) Failing after 43s
Test / API Integration Tests (push) Successful in 1m8s
This commit is contained in:
@@ -1,28 +1,54 @@
|
||||
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
vi.mock('./admin.repo', () => ({
|
||||
findAdminByEmail: vi.fn(),
|
||||
setAdminPasswordReset: vi.fn(),
|
||||
updateAdminLastLogin: vi.fn(),
|
||||
createAuditLog: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('../../services/notificationService', () => ({
|
||||
sendTransactionalEmail: vi.fn().mockResolvedValue(undefined),
|
||||
}))
|
||||
|
||||
const redisStore = new Map<string, string>()
|
||||
|
||||
vi.mock('../../lib/redis', () => ({
|
||||
redis: {
|
||||
on: vi.fn(),
|
||||
get: vi.fn((key: string) => Promise.resolve(redisStore.get(key) ?? null)),
|
||||
set: vi.fn((key: string, value: string) => {
|
||||
redisStore.set(key, value)
|
||||
return Promise.resolve('OK')
|
||||
}),
|
||||
del: vi.fn((key: string) => {
|
||||
const deleted = redisStore.delete(key) ? 1 : 0
|
||||
return Promise.resolve(deleted)
|
||||
}),
|
||||
quit: vi.fn(),
|
||||
duplicate: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
import * as repo from './admin.repo'
|
||||
import { sendTransactionalEmail } from '../../services/notificationService'
|
||||
import { forgotPassword } from './admin.service'
|
||||
import { forgotPassword, login } from './admin.service'
|
||||
|
||||
describe('admin.service forgotPassword', () => {
|
||||
const originalAdminUrl = process.env.ADMIN_URL
|
||||
const originalJwtSecret = process.env.JWT_SECRET
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
redisStore.clear()
|
||||
process.env.ADMIN_URL = 'http://localhost:3000/admin'
|
||||
process.env.JWT_SECRET = 'test-jwt-secret'
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
process.env.ADMIN_URL = originalAdminUrl
|
||||
process.env.JWT_SECRET = originalJwtSecret
|
||||
})
|
||||
|
||||
it('sends the reset email to the canonical stored admin address', async () => {
|
||||
@@ -46,4 +72,55 @@ describe('admin.service forgotPassword', () => {
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('sends an email login code when admin 2FA is enabled', async () => {
|
||||
vi.mocked(repo.findAdminByEmail).mockResolvedValue({
|
||||
id: 'admin_2',
|
||||
email: 'admin@example.test',
|
||||
firstName: 'Amal',
|
||||
lastName: 'Admin',
|
||||
role: 'SUPER_ADMIN',
|
||||
isActive: true,
|
||||
passwordHash: await bcrypt.hash('password123', 4),
|
||||
totpEnabled: true,
|
||||
totpSecret: 'JBSWY3DPEHPK3PXP',
|
||||
} as any)
|
||||
|
||||
await expect(login('admin@example.test', 'password123')).resolves.toEqual({ totpRequired: true })
|
||||
|
||||
expect(sendTransactionalEmail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
to: 'admin@example.test',
|
||||
subject: 'Your RentalDriveGo admin login code',
|
||||
text: expect.stringMatching(/\b\d{6}\b/),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('accepts the emailed admin login code in the 2FA field', async () => {
|
||||
vi.mocked(repo.findAdminByEmail).mockResolvedValue({
|
||||
id: 'admin_3',
|
||||
email: 'admin3@example.test',
|
||||
firstName: 'Mina',
|
||||
lastName: 'Admin',
|
||||
role: 'SUPER_ADMIN',
|
||||
isActive: true,
|
||||
passwordHash: await bcrypt.hash('password123', 4),
|
||||
totpEnabled: true,
|
||||
totpSecret: 'JBSWY3DPEHPK3PXP',
|
||||
} as any)
|
||||
|
||||
await login('admin3@example.test', 'password123')
|
||||
const emailText = vi.mocked(sendTransactionalEmail).mock.calls[0]?.[0]?.text ?? ''
|
||||
const code = emailText.match(/\b\d{6}\b/)?.[0]
|
||||
|
||||
expect(code).toBeTruthy()
|
||||
const result = await login('admin3@example.test', 'password123', code)
|
||||
|
||||
expect(result).toEqual(expect.objectContaining({
|
||||
token: expect.any(String),
|
||||
admin: expect.objectContaining({ id: 'admin_3', email: 'admin3@example.test' }),
|
||||
}))
|
||||
expect(repo.updateAdminLastLogin).toHaveBeenCalledWith('admin_3')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user