mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-21 11:43:11 +00:00
Adds electoral nuclei, leaderships, updates, and WhatsApp invite flows behind campaign auth, plus the consolidated schema migration that Vercel applies on deploy.
126 lines
3.2 KiB
TypeScript
126 lines
3.2 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
cookieSet: vi.fn(),
|
|
login: vi.fn(),
|
|
redirect: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@payload-config', () => ({ default: {} }))
|
|
vi.mock('next/headers', () => ({
|
|
cookies: vi.fn(async () => ({ set: mocks.cookieSet })),
|
|
}))
|
|
vi.mock('next/navigation', () => ({
|
|
redirect: mocks.redirect,
|
|
}))
|
|
vi.mock('payload', () => ({
|
|
getPayload: vi.fn(async () => ({
|
|
collections: {
|
|
campaignUser: {
|
|
config: {
|
|
auth: {
|
|
tokenExpiration: 7200,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
login: mocks.login,
|
|
})),
|
|
}))
|
|
|
|
import { loginCampaign, loginCampaignFormAction } from '@/app/(campaign)/campanha/actions/auth'
|
|
|
|
describe('loginCampaign credential mapping', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mocks.login.mockResolvedValue({ token: 'campaign-token' })
|
|
})
|
|
|
|
it('maps an email identifier to email credentials', async () => {
|
|
await loginCampaign({
|
|
identifier: 'staff@example.com',
|
|
password: 'secret',
|
|
})
|
|
|
|
expect(mocks.login).toHaveBeenCalledWith({
|
|
collection: 'campaignUser',
|
|
data: {
|
|
email: 'staff@example.com',
|
|
password: 'secret',
|
|
},
|
|
})
|
|
})
|
|
|
|
it('normalizes a phone identifier and maps it to username credentials', async () => {
|
|
await loginCampaign({
|
|
identifier: '+55 (71) 99999-1234',
|
|
password: 'secret',
|
|
})
|
|
|
|
expect(mocks.login).toHaveBeenCalledWith({
|
|
collection: 'campaignUser',
|
|
data: {
|
|
password: 'secret',
|
|
username: '71999991234',
|
|
},
|
|
})
|
|
})
|
|
|
|
it('accepts FormData at the action-state boundary', async () => {
|
|
const formData = new FormData()
|
|
formData.set('identifier', '+55 (71) 99999-1234')
|
|
formData.set('password', 'secret')
|
|
|
|
await loginCampaignFormAction({}, formData)
|
|
|
|
expect(mocks.login).toHaveBeenCalledWith({
|
|
collection: 'campaignUser',
|
|
data: {
|
|
password: 'secret',
|
|
username: '71999991234',
|
|
},
|
|
})
|
|
})
|
|
|
|
it('preserves password whitespace at the FormData boundary', async () => {
|
|
const formData = new FormData()
|
|
formData.set('identifier', ' staff@example.com ')
|
|
formData.set('password', ' exact secret ')
|
|
|
|
await loginCampaignFormAction({}, formData)
|
|
|
|
expect(mocks.login).toHaveBeenCalledWith({
|
|
collection: 'campaignUser',
|
|
data: {
|
|
email: 'staff@example.com',
|
|
password: ' exact secret ',
|
|
},
|
|
})
|
|
})
|
|
|
|
it('returns a generic validation error for invalid FormData', async () => {
|
|
const formData = new FormData()
|
|
formData.set('identifier', 'not-an-identifier')
|
|
formData.set('password', '')
|
|
|
|
await expect(loginCampaignFormAction({}, formData)).resolves.toEqual({
|
|
error: 'Dados inválidos.',
|
|
})
|
|
expect(mocks.login).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns a generic authentication error without exposing the failure', async () => {
|
|
mocks.login.mockRejectedValueOnce(new Error('account not found'))
|
|
|
|
await expect(
|
|
loginCampaign({
|
|
identifier: 'staff@example.com',
|
|
password: 'wrong-password',
|
|
}),
|
|
).resolves.toEqual({
|
|
error: 'E-mail, celular ou senha inválidos.',
|
|
})
|
|
})
|
|
})
|