Shumiland/tests/unit/access/access.test.ts
Vadym Samoilenko 9b41fa447a
Some checks are pending
CI / Type Check (push) Waiting to run
CI / Lint (push) Waiting to run
CI / Unit Tests (push) Waiting to run
Deploy / Build & Push Image (push) Waiting to run
Deploy / Deploy to VPS (push) Blocked by required conditions
feat: complete backend B1-B7 — Payload CMS, ezy payments, leads, deploy
- B1: Next.js 15 + Payload CMS 3.0 + Postgres 16, ESLint, Prettier, Husky, Vitest
- B2: 9 collections, 6 globals, 12 Page Builder blocks, access control, slugify/revalidate hooks
- B3: ezy.com.ua payments, Binotel HMAC webhook, leads API, Telegram bot, Resend email, rate limiting
- B4: Tariffs collection with ezy API sync (cron + manual), dynamic pricing source-of-truth
- B5: 13 test files covering unit libs and all API routes
- B6: Dockerfile multi-stage, docker-compose.prod.yml, nginx.conf SSL, GitHub Actions CI/CD, health endpoint
- B7: docs/admin-guide-ua.md (marketer guide), docs/deploy.md (VPS instructions), README quickstart

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-09 19:14:54 +01:00

62 lines
2.2 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { isAdmin } from '@/access/isAdmin'
import { isAdminOrEditor } from '@/access/isAdminOrEditor'
import { isAuthenticatedOrPublished } from '@/access/isAuthenticatedOrPublished'
// Minimal Request stub required by Payload's Access type
function makeReq(user: { role: string } | null): { req: { user: { role: string } | null } } {
return { req: { user } }
}
describe('isAdmin', () => {
it('returns true for admin role', () => {
expect(isAdmin(makeReq({ role: 'admin' }) as never)).toBe(true)
})
it('returns false for editor role', () => {
expect(isAdmin(makeReq({ role: 'editor' }) as never)).toBe(false)
})
it('returns false for user role', () => {
expect(isAdmin(makeReq({ role: 'user' }) as never)).toBe(false)
})
it('returns false when user is null (unauthenticated)', () => {
expect(isAdmin(makeReq(null) as never)).toBe(false)
})
})
describe('isAdminOrEditor', () => {
it('returns true for admin role', () => {
expect(isAdminOrEditor(makeReq({ role: 'admin' }) as never)).toBe(true)
})
it('returns true for editor role', () => {
expect(isAdminOrEditor(makeReq({ role: 'editor' }) as never)).toBe(true)
})
it('returns false for user role', () => {
expect(isAdminOrEditor(makeReq({ role: 'user' }) as never)).toBe(false)
})
it('returns false when user is null (unauthenticated)', () => {
expect(isAdminOrEditor(makeReq(null) as never)).toBe(false)
})
})
describe('isAuthenticatedOrPublished', () => {
it('returns true for any authenticated user', () => {
expect(isAuthenticatedOrPublished(makeReq({ role: 'admin' }) as never)).toBe(true)
expect(isAuthenticatedOrPublished(makeReq({ role: 'editor' }) as never)).toBe(true)
expect(isAuthenticatedOrPublished(makeReq({ role: 'user' }) as never)).toBe(true)
})
it('returns a Payload where clause for unauthenticated requests', () => {
const result = isAuthenticatedOrPublished(makeReq(null) as never)
expect(result).toEqual({ _status: { equals: 'published' } })
})
it('does not return true for unauthenticated requests', () => {
expect(isAuthenticatedOrPublished(makeReq(null) as never)).not.toBe(true)
})
})