Files
RenewCMS/web/src/composables/__tests__/useSchemaGate.spec.ts
T

60 lines
1.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import { ApiError } from '@/composables/useApi'
import { useContentTypes } from '@/composables/useContentTypes'
import { useSchemaGate, type SchemaGateScope } from '@/composables/useSchemaGate'
import { useContentTypesStore } from '@/stores/contentTypes'
import { useSessionStore } from '@/stores/session'
vi.mock('@/composables/useContentTypes', () => ({ useContentTypes: vi.fn() }))
const listContentTypes = vi.fn()
const listBuiltinTypes = vi.fn()
beforeEach(() => {
setActivePinia(createPinia())
listContentTypes.mockReset()
listBuiltinTypes.mockReset()
vi.mocked(useContentTypes).mockReturnValue({ listContentTypes, listBuiltinTypes })
const session = useSessionStore()
session.$patch({ status: 'ready', session: { role: 'owner' } })
})
async function gate(scope: SchemaGateScope) {
const store = useContentTypesStore()
await store.load({ builtin: true })
return useSchemaGate(scope)
}
describe('schema gate forbidden', () => {
it.each([
['custom', true],
['builtin', false],
['all', true],
] as const)('scope %s with custom 403 is %s', async (scope, expected) => {
listContentTypes.mockRejectedValue(new ApiError(403))
listBuiltinTypes.mockResolvedValue([])
expect((await gate(scope)).forbidden.value).toBe(expected)
})
it.each([
['custom', false],
['builtin', true],
['all', true],
] as const)('scope %s with builtin 403 is %s', async (scope, expected) => {
listContentTypes.mockResolvedValue([])
listBuiltinTypes.mockRejectedValue(new ApiError(403))
expect((await gate(scope)).forbidden.value).toBe(expected)
})
it('is forbidden for a session that cannot manage content, whatever the scope', async () => {
listContentTypes.mockResolvedValue([])
listBuiltinTypes.mockResolvedValue([])
useSessionStore().$patch({ session: { role: 'user' } })
expect((await gate('custom')).forbidden.value).toBe(true)
})
})