mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-16 20:13:04 +00:00
436 seeded Praças (municipality-based; per-TSE-zone in Salvador/Camaçari) become the operational unit; roles renamed to coordinator/advisor/leader with advisors scoped to administered Praças. Leadership is one record per person across many Praças/organizations; vote pledges split leader-declared vs staff-estimated (estimates never serialized to leaders). Adds organizations, demand workflow with staff-only cost/receipts, action-plan results with deputy presence, manual political trend, multi-candidate comparison and the diverging red/white/blue map mode. Migration 20260721_020109_remodel_plazas deliberately resets the campaign vertical in production (public site untouched); test suite ported to the new model (unit 187 / int 306 / e2e 9 green).
276 lines
9.7 KiB
TypeScript
276 lines
9.7 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
import { getPayload, type Field, type Payload, type PayloadRequest } from 'payload'
|
|
|
|
import { CampaignUser as CampaignUserCollection } from '@/collections/CampaignUser'
|
|
import type { CampaignUser } from '@/payload-types'
|
|
import config from '@/payload.config'
|
|
|
|
import {
|
|
type CampaignFixtures,
|
|
withCampaignFixtures,
|
|
} from '../helpers/campaignFixtures'
|
|
|
|
let payload: Payload
|
|
|
|
// Builds the engaged Contact → Leadership link that grants contextual phone access.
|
|
const createEngagedPhoneAccessGraph = async (
|
|
fixtures: CampaignFixtures,
|
|
coordinator: CampaignUser,
|
|
leader: CampaignUser,
|
|
plaza: number,
|
|
) => {
|
|
const contact = await fixtures.createContact({
|
|
name: fixtures.value('Contato telefone'),
|
|
phone: fixtures.phone(),
|
|
})
|
|
return fixtures.createLeadership({
|
|
contact,
|
|
plazas: [plaza],
|
|
user: leader,
|
|
supportStatus: 'engajado',
|
|
createdBy: coordinator,
|
|
})
|
|
}
|
|
|
|
const phoneField = (): Extract<Field, { name: string }> => {
|
|
const field = CampaignUserCollection.fields.find(
|
|
(candidate): candidate is Extract<Field, { name: string }> =>
|
|
'name' in candidate && candidate.name === 'phone',
|
|
)
|
|
if (!field) throw new Error('Campo phone não configurado.')
|
|
return field
|
|
}
|
|
|
|
describe('campaign user contact phone', () => {
|
|
beforeAll(async () => {
|
|
payload = await getPayload({ config: await config })
|
|
})
|
|
|
|
it('normalizes the optional non-auth contact phone', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const user = await fixtures.createCampaignUser('advisor', {
|
|
phone: '+55 (71) 99999-1234',
|
|
})
|
|
|
|
expect(user.phone).toBe('71999991234')
|
|
expect(user.username).toBeNull()
|
|
expect(phoneField()).toMatchObject({
|
|
name: 'phone',
|
|
label: 'Celular de contato',
|
|
type: 'text',
|
|
index: true,
|
|
})
|
|
})
|
|
})
|
|
|
|
it('lets an engaged leader read only advisors from accessible plazas', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const advisorPhone = fixtures.phone()
|
|
const foreignPhone = fixtures.phone()
|
|
const advisor = await fixtures.createCampaignUser('advisor', {
|
|
phone: advisorPhone,
|
|
})
|
|
const foreignAdvisor = await fixtures.createCampaignUser('advisor', {
|
|
phone: foreignPhone,
|
|
})
|
|
const leader = await fixtures.createCampaignUser('leader', {
|
|
phone: fixtures.phone(),
|
|
})
|
|
const foreignLeader = await fixtures.createCampaignUser('leader', {
|
|
phone: fixtures.phone(),
|
|
})
|
|
const plaza = await fixtures.getPlaza()
|
|
const foreignPlaza = await fixtures.getPlaza()
|
|
await fixtures.assignPlazaAdvisors(plaza, [advisor])
|
|
await fixtures.assignPlazaAdvisors(foreignPlaza, [foreignAdvisor])
|
|
await createEngagedPhoneAccessGraph(fixtures, coordinator, leader, plaza.id)
|
|
|
|
const visibleAdvisor = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: advisor.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: leader,
|
|
overrideAccess: false,
|
|
})
|
|
const hiddenForeignAdvisor = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: foreignAdvisor.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: leader,
|
|
overrideAccess: false,
|
|
})
|
|
const hiddenFromUnlinkedLeader = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: advisor.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: foreignLeader,
|
|
overrideAccess: false,
|
|
})
|
|
|
|
expect(visibleAdvisor.phone).toBe(advisorPhone)
|
|
expect(hiddenForeignAdvisor.phone).toBeUndefined()
|
|
expect(hiddenFromUnlinkedLeader.phone).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
it('lets an advisor read a colleague advisor of a shared plaza only', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const colleaguePhone = fixtures.phone()
|
|
const foreignPhone = fixtures.phone()
|
|
const viewer = await fixtures.createCampaignUser('advisor', { phone: fixtures.phone() })
|
|
const colleague = await fixtures.createCampaignUser('advisor', { phone: colleaguePhone })
|
|
const foreignAdvisor = await fixtures.createCampaignUser('advisor', {
|
|
phone: foreignPhone,
|
|
})
|
|
const shared = await fixtures.getPlaza()
|
|
const foreignPlaza = await fixtures.getPlaza()
|
|
await fixtures.assignPlazaAdvisors(shared, [viewer, colleague])
|
|
await fixtures.assignPlazaAdvisors(foreignPlaza, [foreignAdvisor])
|
|
|
|
const visibleColleague = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: colleague.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: viewer,
|
|
overrideAccess: false,
|
|
})
|
|
const hiddenForeign = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: foreignAdvisor.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: viewer,
|
|
overrideAccess: false,
|
|
})
|
|
|
|
expect(visibleColleague.phone).toBe(colleaguePhone)
|
|
expect(hiddenForeign.phone).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
it('does not leak foreign phones through direct list API reads', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const advisorPhone = fixtures.phone()
|
|
const foreignPhone = fixtures.phone()
|
|
const advisor = await fixtures.createCampaignUser('advisor', {
|
|
phone: advisorPhone,
|
|
})
|
|
const foreignAdvisor = await fixtures.createCampaignUser('advisor', {
|
|
phone: foreignPhone,
|
|
})
|
|
const leader = await fixtures.createCampaignUser('leader', {
|
|
phone: fixtures.phone(),
|
|
})
|
|
const plaza = await fixtures.getPlaza()
|
|
await fixtures.assignPlazaAdvisors(plaza, [advisor])
|
|
await createEngagedPhoneAccessGraph(fixtures, coordinator, leader, plaza.id)
|
|
|
|
const result = await payload.find({
|
|
collection: 'campaignUser',
|
|
where: { id: { in: [advisor.id, foreignAdvisor.id] } },
|
|
depth: 0,
|
|
pagination: false,
|
|
select: { name: true, phone: true },
|
|
user: leader,
|
|
overrideAccess: false,
|
|
})
|
|
const byId = new Map(result.docs.map((doc) => [doc.id, doc]))
|
|
|
|
expect(byId.get(advisor.id)?.phone).toBe(advisorPhone)
|
|
expect(byId.get(foreignAdvisor.id)?.phone).toBeUndefined()
|
|
expect(JSON.stringify(result.docs)).not.toContain(foreignPhone)
|
|
})
|
|
})
|
|
|
|
it('allows the record owner, coordinator, and Payload admin to read contact phone', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const phone = fixtures.phone()
|
|
const owner = await fixtures.createCampaignUser('leader', { phone })
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const admin = await fixtures.createAdminUser({
|
|
email: `${fixtures.value('phone-admin')}@example.com`,
|
|
password: fixtures.value('password'),
|
|
})
|
|
|
|
for (const viewer of [owner, coordinator, admin]) {
|
|
const visible = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: owner.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: viewer,
|
|
overrideAccess: false,
|
|
})
|
|
expect(visible.phone).toBe(phone)
|
|
}
|
|
})
|
|
})
|
|
|
|
it('lets advisor and leader read contact phones of coordinator users', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const coordinatorPhone = fixtures.phone()
|
|
const coordinator = await fixtures.createCampaignUser('coordinator', {
|
|
phone: coordinatorPhone,
|
|
})
|
|
const advisor = await fixtures.createCampaignUser('advisor', {
|
|
phone: fixtures.phone(),
|
|
})
|
|
const leader = await fixtures.createCampaignUser('leader', {
|
|
phone: fixtures.phone(),
|
|
})
|
|
const plaza = await fixtures.getPlaza()
|
|
await fixtures.assignPlazaAdvisors(plaza, [advisor])
|
|
await createEngagedPhoneAccessGraph(fixtures, coordinator, leader, plaza.id)
|
|
|
|
for (const viewer of [advisor, leader]) {
|
|
const visible = await payload.findByID({
|
|
collection: 'campaignUser',
|
|
id: coordinator.id,
|
|
depth: 0,
|
|
select: { name: true, phone: true },
|
|
user: viewer,
|
|
overrideAccess: false,
|
|
})
|
|
expect(visible.phone).toBe(coordinatorPhone)
|
|
}
|
|
})
|
|
})
|
|
|
|
it('allows owner and coordinator field updates but protects create from ordinary users', async () => {
|
|
await withCampaignFixtures(payload, async (fixtures) => {
|
|
const owner = await fixtures.createCampaignUser('leader', {
|
|
phone: fixtures.phone(),
|
|
})
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const field = phoneField()
|
|
if (field.type !== 'text') throw new Error('Campo phone deve ser text.')
|
|
const ownerReq = {
|
|
context: {},
|
|
payload,
|
|
user: owner,
|
|
} as unknown as PayloadRequest
|
|
const coordinatorReq = {
|
|
context: {},
|
|
payload,
|
|
user: coordinator,
|
|
} as unknown as PayloadRequest
|
|
|
|
await expect(field.access?.update?.({ id: owner.id, req: ownerReq } as never)).resolves.toBe(
|
|
true,
|
|
)
|
|
await expect(
|
|
field.access?.update?.({ id: owner.id, req: coordinatorReq } as never),
|
|
).resolves.toBe(true)
|
|
await expect(field.access?.create?.({ req: ownerReq } as never)).resolves.toBe(false)
|
|
await expect(field.access?.create?.({ req: coordinatorReq } as never)).resolves.toBe(true)
|
|
})
|
|
})
|
|
})
|