36 lines
1.7 KiB
TypeScript
36 lines
1.7 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import type { RoleRecord, RolePermissionRecord } from '../src/api'
|
|
import { projectAccessOf, projectAccessPatch } from '../src/rolePermissions'
|
|
|
|
const role = (permissions: RolePermissionRecord[]): RoleRecord => ({
|
|
roleId: 'role_engineer', name: '软件工程师', description: '', enabled: true, roleVersion: 3, bindingCount: 0, permissions,
|
|
})
|
|
const row = (allowed: boolean, capAllowed: boolean, actionId = 'project.create'): RolePermissionRecord =>
|
|
({ actionId, allowed, capAllowed, permissionVersion: 1 })
|
|
|
|
describe('project access normalization', () => {
|
|
it('requires both default and cap on the project.create row', () => {
|
|
expect(projectAccessOf(role([row(true, true)]))).toBe(true)
|
|
expect(projectAccessOf(role([row(false, true)]))).toBe(false)
|
|
expect(projectAccessOf(role([row(true, false)]))).toBe(false)
|
|
})
|
|
|
|
it('treats a missing project.create row as no access', () => {
|
|
expect(projectAccessOf(role([]))).toBe(false)
|
|
expect(projectAccessOf(role([row(true, true, 'document.read')]))).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('project access patch', () => {
|
|
it('emits a single project.create change with default and cap moving together', () => {
|
|
expect(projectAccessPatch(role([]), true)).toEqual([{ actionId: 'project.create', allowed: true, capAllowed: true }])
|
|
expect(projectAccessPatch(role([row(true, true)]), false)).toEqual([{ actionId: 'project.create', allowed: false, capAllowed: false }])
|
|
})
|
|
|
|
it('returns an empty patch when the effective value is unchanged', () => {
|
|
expect(projectAccessPatch(role([row(true, true)]), true)).toEqual([])
|
|
expect(projectAccessPatch(role([row(false, true)]), false)).toEqual([])
|
|
expect(projectAccessPatch(role([]), false)).toEqual([])
|
|
})
|
|
})
|