Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(studio): scoped pat unit tests (#42646)

kemal.earthb72272651773
diff --git a/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.schemas.test.ts b/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.schemas.test.ts
new file mode 100644
index 00000000000..6ac53f415f3
--- /dev/null
+++ b/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.schemas.test.ts
@@ -0,0 +1,192 @@
+import { describe, it, expect } from 'vitest'
+import { PermissionRowSchema, TokenSchema } from './AccessToken.schemas'
+
+const validTokenData = {
+ tokenName: 'My Token',
+ expiresAt: 'day',
+ resourceAccess: 'all-orgs' as const,
+ permissionRows: [{ resource: 'organization:billing', actions: ['read'] }],
+}
+
+// --- PermissionRowSchema ---
+
+describe('PermissionRowSchema', () => {
+ it('should pass for a valid permission row', () => {
+ const result = PermissionRowSchema.safeParse({
+ resource: 'organization:billing',
+ actions: ['read'],
+ })
+ expect(result.success).toBe(true)
+ })
+
+ it('should fail when resource is empty', () => {
+ const result = PermissionRowSchema.safeParse({
+ resource: '',
+ actions: ['read'],
+ })
+ expect(result.success).toBe(false)
+ if (!result.success) {
+ expect(result.error.issues[0].message).toBe('Please select a resource')
+ }
+ })
+
+ it('should fail when actions is empty', () => {
+ const result = PermissionRowSchema.safeParse({
+ resource: 'organization:billing',
+ actions: [],
+ })
+ expect(result.success).toBe(false)
+ if (!result.success) {
+ expect(result.error.issues[0].message).toBe('Please select at least one action')
+ }
+ })
+
+ it('should fail when resource is missing', () => {
+ const result = PermissionRowSchema.safeParse({ actions: ['read'] })
+ expect(result.success).toBe(false)
+ })
+
+ it('should fail when actions is missing', () => {
+ const result = PermissionRowSchema.safeParse({ resource: 'organization:billing' })
+ expect(result.success).toBe(false)
+ })
+})
+
+// --- TokenSchema ---
+
+describe('TokenSchema', () => {
+ it('should pass for valid token data', () => {
+ const result = TokenSchema.safeParse(validTokenData)
+ expect(result.success).toBe(true)
+ })
+
+ it('should fail when tokenName is empty', () => {
+ const result = TokenSchema.safeParse({ ...validTokenData, tokenName: '' })
+ expect(result.success).toBe(false)
+ if (!result.success) {
+ const nameError = result.error.issues.find((i) => i.path.includes('tokenName'))
+ expect(nameError?.message).toBe('Please enter a name for the token')
+ }
+ })
+
+ it('should fail when tokenName is missing', () => {
+ const { tokenName, ...rest } = validTokenData
+ const result = TokenSchema.safeParse(rest)
+ expect(result.success).toBe(false)
+ })
+
+ it('should fail when permissionRows is empty', () => {
+ const result = TokenSchema.safeParse({ ...validTokenData, permissionRows: [] })
+ expect(result.success).toBe(false)
+ if (!result.success) {
+ const permError = result.error.issues.find((i) => i.path.includes('permissionRows'))
+ expect(permError?.message).toBe('Please configure at least one permission')
+ }
+ })
+
+ it('should fail when resourceAccess is not a valid enum value', () => {
+ const result = TokenSchema.safeParse({ ...validTokenData, resourceAccess: 'invalid' })
+ expect(result.success).toBe(false)
+ })
+
+ it('should accept all valid resourceAccess enum values', () => {
+ for (const value of ['all-orgs', 'selected-orgs', 'selected-projects'] as const) {
+ const result = TokenSchema.safeParse({ ...validTokenData, resourceAccess: value })
+ expect(result.success).toBe(true)
+ }
+ })
+
+ describe('expiresAt preprocessing', () => {
+ it('should convert "never" to undefined', () => {
+ const result = TokenSchema.safeParse({ ...validTokenData, expiresAt: 'never' })
+ expect(result.success).toBe(true)
+ if (result.success) {
+ expect(result.data.expiresAt).toBeUndefined()
+ }
+ })
+
+ it('should pass through other string values', () => {
+ const result = TokenSchema.safeParse({ ...validTokenData, expiresAt: 'day' })
+ expect(result.success).toBe(true)
+ if (result.success) {
+ expect(result.data.expiresAt).toBe('day')
+ }
+ })
+
+ it('should allow expiresAt to be omitted', () => {
+ const { expiresAt, ...rest } = validTokenData
+ const result = TokenSchema.safeParse(rest)
+ expect(result.success).toBe(true)
+ })
+ })
+
+ describe('custom expiry refinement', () => {
+ it('should fail when expiresAt is "custom" and customExpiryDate is not provided', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ expiresAt: 'custom',
+ customExpiryDate: undefined,
+ })
+ expect(result.success).toBe(false)
+ if (!result.success) {
+ const customError = result.error.issues.find((i) => i.path.includes('expiresAt'))
+ expect(customError?.message).toBe('Please select a custom expiry date')
+ }
+ })
+
+ it('should fail when expiresAt is "custom" and customExpiryDate is empty string', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ expiresAt: 'custom',
+ customExpiryDate: '',
+ })
+ expect(result.success).toBe(false)
+ })
+
+ it('should pass when expiresAt is "custom" and customExpiryDate is provided', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ expiresAt: 'custom',
+ customExpiryDate: '2026-12-31T00:00:00Z',
+ })
+ expect(result.success).toBe(true)
+ })
+
+ it('should pass when expiresAt is not "custom" even without customExpiryDate', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ expiresAt: 'day',
+ })
+ expect(result.success).toBe(true)
+ })
+ })
+
+ describe('nested permissionRows validation', () => {
+ it('should fail when a permission row has an empty resource', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ permissionRows: [{ resource: '', actions: ['read'] }],
+ })
+ expect(result.success).toBe(false)
+ })
+
+ it('should fail when a permission row has empty actions', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ permissionRows: [{ resource: 'organization:billing', actions: [] }],
+ })
+ expect(result.success).toBe(false)
+ })
+
+ it('should pass with multiple valid permission rows', () => {
+ const result = TokenSchema.safeParse({
+ ...validTokenData,
+ permissionRows: [
+ { resource: 'organization:billing', actions: ['read'] },
+ { resource: 'organization:members', actions: ['read', 'write'] },
+ ],
+ })
+ expect(result.success).toBe(true)
+ })
+ })
+})
diff --git a/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.utils.test.ts b/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.utils.test.ts
new file mode 100644
index 00000000000..2aacdb8f8ec
--- /dev/null
+++ b/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.utils.test.ts
@@ -0,0 +1,374 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
+import dayjs from 'dayjs'
+import type { BaseToken } from './AccessToken.types'
+
+// Mock PERMISSION_LIST so tests are deterministic and don't break when shared-types updates
+vi.mock('./AccessToken.constants', () => ({
+ PERMISSION_LIST: [
+ {
+ scope: 'organization',
+ resource: 'billing',
+ action: 'read',
+ id: 'org-billing-read',
+ title: 'Read Billing',
+ },
+ {
+ scope: 'organization',
+ resource: 'billing',
+ action: 'write',
+ id: 'org-billing-write',
+ title: 'Manage Billing',
+ },
+ {
+ scope: 'organization',
+ resource: 'members',
+ action: 'read',
+ id: 'org-members-read',
+ title: 'Read Members',
+ },
+ {
+ scope: 'organization',
+ resource: 'members',
+ action: 'write',
+ id: 'org-members-write',
+ title: 'Manage Members',
+ },
+ {
+ scope: 'organization',
+ resource: 'members',
+ action: 'create',
+ id: 'org-members-create',
+ title: 'Create Members',
+ },
+ {
+ scope: 'organization',
+ resource: 'members',
+ action: 'delete',
+ id: 'org-members-delete',
+ title: 'Delete Members',
+ },
+ {
+ scope: 'project',
+ resource: 'database',
+ action: 'read',
+ id: 'proj-db-read',
+ title: 'Read Database',
+ },
+ ],
+}))
+
+import {
+ handleSortChange,
+ filterAndSortTokens,
+ mapPermissionToFGA,
+ getResourcePermissions,
+ getRealAccess,
+ formatAccessText,
+ getExpirationDate,
+} from './AccessToken.utils'
+
+// --- handleSortChange ---
+
+describe('handleSortChange', () => {
+ it('should toggle from asc to desc for the same column', () => {
+ const setSort = vi.fn()
+ handleSortChange('created_at:asc', 'created_at', setSort)
+ expect(setSort).toHaveBeenCalledWith('created_at:desc')
+ })
+
+ it('should reset to created_at:desc when toggling off desc on a non-default column', () => {
+ const setSort = vi.fn()
+ handleSortChange('last_used_at:desc', 'last_used_at', setSort)
+ expect(setSort).toHaveBeenCalledWith('created_at:desc')
+ })
+
+ it('should set new column to asc when switching columns', () => {
+ const setSort = vi.fn()
+ handleSortChange('created_at:desc', 'expires_at', setSort)
+ expect(setSort).toHaveBeenCalledWith('expires_at:asc')
+ })
+
+ it('should reset to created_at:desc when toggling off desc on created_at itself', () => {
+ const setSort = vi.fn()
+ handleSortChange('created_at:desc', 'created_at', setSort)
+ expect(setSort).toHaveBeenCalledWith('created_at:desc')
+ })
+
+ it('should set last_used_at to asc when switching from expires_at', () => {
+ const setSort = vi.fn()
+ handleSortChange('expires_at:asc', 'last_used_at', setSort)
+ expect(setSort).toHaveBeenCalledWith('last_used_at:asc')
+ })
+})
+
+// --- filterAndSortTokens ---
+
+const makeToken = (overrides: Partial<BaseToken> = {}): BaseToken => ({
+ id: '1',
+ name: 'Token',
+ token_alias: 'alias',
+ created_at: '2024-01-01T00:00:00Z',
+ last_used_at: null,
+ expires_at: null,
+ ...overrides,
+})
+
+describe('filterAndSortTokens', () => {
+ const tokens: BaseToken[] = [
+ makeToken({ id: '1', name: 'Alpha', created_at: '2024-01-03T00:00:00Z' }),
+ makeToken({ id: '2', name: 'Beta', created_at: '2024-01-01T00:00:00Z' }),
+ makeToken({ id: '3', name: 'Gamma', created_at: '2024-01-02T00:00:00Z' }),
+ ]
+
+ it('should return undefined when tokens is undefined', () => {
+ expect(filterAndSortTokens(undefined, '', 'created_at:desc')).toBeUndefined()
+ })
+
+ it('should return all tokens when search string is empty', () => {
+ const result = filterAndSortTokens(tokens, '', 'created_at:asc')
+ expect(result).toHaveLength(3)
+ })
+
+ it('should filter tokens by name case-insensitively', () => {
+ const result = filterAndSortTokens(tokens, 'alpha', 'created_at:asc')
+ expect(result).toHaveLength(1)
+ expect(result![0].name).toBe('Alpha')
+ })
+
+ it('should return empty array when no tokens match', () => {
+ const result = filterAndSortTokens(tokens, 'nonexistent', 'created_at:asc')
+ expect(result).toHaveLength(0)
+ })
+
+ it('should sort by created_at ascending', () => {
+ const result = filterAndSortTokens(tokens, '', 'created_at:asc')!
+ expect(result.map((t) => t.id)).toEqual(['2', '3', '1'])
+ })
+
+ it('should sort by created_at descending', () => {
+ const result = filterAndSortTokens(tokens, '', 'created_at:desc')!
+ expect(result.map((t) => t.id)).toEqual(['1', '3', '2'])
+ })
+
+ it('should sort by last_used_at ascending with nulls last', () => {
+ const tokensWithUsage: BaseToken[] = [
+ makeToken({ id: '1', last_used_at: '2024-03-01T00:00:00Z' }),
+ makeToken({ id: '2', last_used_at: null }),
+ makeToken({ id: '3', last_used_at: '2024-01-01T00:00:00Z' }),
+ ]
+ const result = filterAndSortTokens(tokensWithUsage, '', 'last_used_at:asc')!
+ expect(result.map((t) => t.id)).toEqual(['3', '1', '2'])
+ })
+
+ it('should sort by last_used_at descending with nulls last', () => {
+ const tokensWithUsage: BaseToken[] = [
+ makeToken({ id: '1', last_used_at: '2024-01-01T00:00:00Z' }),
+ makeToken({ id: '2', last_used_at: null }),
+ makeToken({ id: '3', last_used_at: '2024-03-01T00:00:00Z' }),
+ ]
+ const result = filterAndSortTokens(tokensWithUsage, '', 'last_used_at:desc')!
+ expect(result.map((t) => t.id)).toEqual(['3', '1', '2'])
+ })
+
+ it('should keep order stable when both last_used_at values are null', () => {
+ const tokensAllNull: BaseToken[] = [
+ makeToken({ id: '1', last_used_at: null }),
+ makeToken({ id: '2', last_used_at: null }),
+ ]
+ const result = filterAndSortTokens(tokensAllNull, '', 'last_used_at:asc')!
+ expect(result).toHaveLength(2)
+ })
+
+ it('should sort by expires_at ascending with nulls last', () => {
+ const tokensWithExpiry: BaseToken[] = [
+ makeToken({ id: '1', expires_at: '2025-06-01T00:00:00Z' }),
+ makeToken({ id: '2', expires_at: null }),
+ makeToken({ id: '3', expires_at: '2025-01-01T00:00:00Z' }),
+ ]
+ const result = filterAndSortTokens(tokensWithExpiry, '', 'expires_at:asc')!
+ expect(result.map((t) => t.id)).toEqual(['3', '1', '2'])
+ })
+
+ it('should sort by expires_at descending with nulls last', () => {
+ const tokensWithExpiry: BaseToken[] = [
+ makeToken({ id: '1', expires_at: '2025-01-01T00:00:00Z' }),
+ makeToken({ id: '2', expires_at: null }),
+ makeToken({ id: '3', expires_at: '2025-06-01T00:00:00Z' }),
+ ]
+ const result = filterAndSortTokens(tokensWithExpiry, '', 'expires_at:desc')!
+ expect(result.map((t) => t.id)).toEqual(['3', '1', '2'])
+ })
+
+ it('should keep order stable when both expires_at values are null', () => {
+ const tokensAllNull: BaseToken[] = [
+ makeToken({ id: '1', expires_at: null }),
+ makeToken({ id: '2', expires_at: null }),
+ ]
+ const result = filterAndSortTokens(tokensAllNull, '', 'expires_at:asc')!
+ expect(result).toHaveLength(2)
+ })
+
+ it('should not mutate the original tokens array', () => {
+ const original = [...tokens]
+ filterAndSortTokens(tokens, '', 'created_at:asc')
+ expect(tokens).toEqual(original)
+ })
+})
+
+// --- mapPermissionToFGA ---
+
+describe('mapPermissionToFGA', () => {
+ it('should return the permission id for a matching scope:resource + action', () => {
+ const result = mapPermissionToFGA('organization:billing', 'read')
+ expect(result).toEqual(['org-billing-read'])
+ })
+
+ it('should return empty array when no permission matches', () => {
+ const result = mapPermissionToFGA('nonexistent:resource', 'read')
+ expect(result).toEqual([])
+ })
+
+ it('should return empty array when action does not match', () => {
+ const result = mapPermissionToFGA('organization:billing', 'delete')
+ expect(result).toEqual([])
+ })
+})
+
+// --- getResourcePermissions ---
+
+describe('getResourcePermissions', () => {
+ it('should always include "no access" with an empty array', () => {
+ const result = getResourcePermissions('organization:billing')
+ expect(result['no access']).toEqual([])
+ })
+
+ it('should include individual actions from PERMISSION_LIST', () => {
+ const result = getResourcePermissions('organization:billing')
+ expect(result['read']).toEqual(['org-billing-read'])
+ expect(result['write']).toEqual(['org-billing-write'])
+ })
+
+ it('should include combined "read-write" when both read and write exist', () => {
+ const result = getResourcePermissions('organization:billing')
+ expect(result['read-write']).toEqual(['org-billing-read', 'org-billing-write'])
+ })
+
+ it('should return only "no access" for an unknown resource', () => {
+ const result = getResourcePermissions('fake:resource')
+ expect(Object.keys(result)).toEqual(['no access'])
+ })
+
+ it('should include all actions for a resource with many permissions', () => {
+ const result = getResourcePermissions('organization:members')
+ expect(result['read']).toEqual(['org-members-read'])
+ expect(result['write']).toEqual(['org-members-write'])
+ expect(result['create']).toEqual(['org-members-create'])
+ expect(result['delete']).toEqual(['org-members-delete'])
+ expect(result['read-write']).toEqual(['org-members-read', 'org-members-write'])
+ })
+})
+
+// --- getRealAccess ---
+
+describe('getRealAccess', () => {
+ it('should return "no access" when token has no matching permissions', () => {
+ expect(getRealAccess('organization:billing', [])).toBe('no access')
+ })
+
+ it('should return "no access" when permissions do not match the resource', () => {
+ expect(getRealAccess('organization:billing', ['unrelated-id'])).toBe('no access')
+ })
+
+ it('should return the single action when only one matches', () => {
+ expect(getRealAccess('organization:billing', ['org-billing-read'])).toBe('read')
+ })
+
+ it('should return "read-write" when both read and write permissions match', () => {
+ expect(getRealAccess('organization:billing', ['org-billing-read', 'org-billing-write'])).toBe(
+ 'read-write'
+ )
+ })
+
+ it('should join multiple granted actions with hyphens when not exactly read+write', () => {
+ expect(
+ getRealAccess('organization:members', [
+ 'org-members-read',
+ 'org-members-write',
+ 'org-members-create',
+ ])
+ ).toBe('read-write-create')
+ })
+
+ it('should return single action for write-only access', () => {
+ expect(getRealAccess('organization:billing', ['org-billing-write'])).toBe('write')
+ })
+})
+
+// --- formatAccessText ---
+
+describe('formatAccessText', () => {
+ it('should return "No access" for "no access"', () => {
+ expect(formatAccessText('no access')).toBe('No access')
+ })
+
+ it('should capitalize a single word', () => {
+ expect(formatAccessText('read')).toBe('Read')
+ })
+
+ it('should capitalize each hyphen-separated word', () => {
+ expect(formatAccessText('read-write')).toBe('Read-Write')
+ })
+
+ it('should handle multi-segment actions', () => {
+ expect(formatAccessText('read-write-delete')).toBe('Read-Write-Delete')
+ })
+})
+
+// --- getExpirationDate ---
+
+describe('getExpirationDate', () => {
+ const FIXED_DATE = new Date('2025-06-15T12:00:00.000Z')
+
+ beforeEach(() => {
+ vi.useFakeTimers()
+ vi.setSystemTime(FIXED_DATE)
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('should return a date exactly 1 hour from now for "hour"', () => {
+ const result = getExpirationDate('hour')!
+ expect(result).toBe(dayjs(FIXED_DATE).add(1, 'hours').toISOString())
+ })
+
+ it('should return a date exactly 1 day from now for "day"', () => {
+ const result = getExpirationDate('day')!
+ expect(result).toBe(dayjs(FIXED_DATE).add(1, 'day').toISOString())
+ })
+
+ it('should return a date exactly 7 days from now for "week"', () => {
+ const result = getExpirationDate('week')!
+ expect(result).toBe(dayjs(FIXED_DATE).add(7, 'days').toISOString())
+ })
+
+ it('should return a date exactly 30 days from now for "month"', () => {
+ const result = getExpirationDate('month')!
+ expect(result).toBe(dayjs(FIXED_DATE).add(30, 'days').toISOString())
+ })
+
+ it('should return undefined for "never"', () => {
+ expect(getExpirationDate('never')).toBeUndefined()
+ })
+
+ it('should return undefined for an unknown key', () => {
+ expect(getExpirationDate('unknown')).toBeUndefined()
+ })
+
+ it('should return a valid ISO string', () => {
+ const result = getExpirationDate('hour')!
+ expect(dayjs(result).isValid()).toBe(true)
+ expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T/)
+ })
+})
diff --git a/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/Permissions/Permissions.utils.test.ts b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/Permissions/Permissions.utils.test.ts
new file mode 100644
index 00000000000..0fa531cdc96
--- /dev/null
+++ b/apps/studio/components/interfaces/Account/AccessTokens/Scoped/Form/Permissions/Permissions.utils.test.ts
@@ -0,0 +1,127 @@
+import { describe, it, expect } from 'vitest'
+import { sortActions, togglePermissionResource } from './Permissions.utils'
+import type { PermissionResource, PermissionRow } from './Permissions.types'
+
+// --- sortActions ---
+
+describe('sortActions', () => {
+ it('should sort actions by priority order: read, write, create, delete', () => {
+ expect(sortActions(['delete', 'create', 'write', 'read'])).toEqual([
+ 'read',
+ 'write',
+ 'create',
+ 'delete',
+ ])
+ })
+
+ it('should return empty array for empty input', () => {
+ expect(sortActions([])).toEqual([])
+ })
+
+ it('should keep a single action as-is', () => {
+ expect(sortActions(['write'])).toEqual(['write'])
+ })
+
+ it('should place unknown actions after the priority ones', () => {
+ expect(sortActions(['custom-action', 'read', 'delete'])).toEqual([
+ 'read',
+ 'delete',
+ 'custom-action',
+ ])
+ })
+
+ it('should preserve relative order of multiple unknown actions', () => {
+ expect(sortActions(['zeta', 'alpha', 'read'])).toEqual(['read', 'zeta', 'alpha'])
+ })
+
+ it('should handle only unknown actions', () => {
+ expect(sortActions(['foo', 'bar'])).toEqual(['foo', 'bar'])
+ })
+
+ it('should handle a subset of priority actions', () => {
+ expect(sortActions(['delete', 'read'])).toEqual(['read', 'delete'])
+ })
+
+ it('should not mutate the original array', () => {
+ const original = ['delete', 'read']
+ sortActions(original)
+ expect(original).toEqual(['delete', 'read'])
+ })
+})
+
+// --- togglePermissionResource ---
+
+describe('togglePermissionResource', () => {
+ const billingResource: PermissionResource = {
+ resource: 'organization:billing',
+ title: 'Billing',
+ actions: ['read', 'write'],
+ }
+
+ const membersResource: PermissionResource = {
+ resource: 'organization:members',
+ title: 'Members',
+ actions: ['read', 'write', 'create', 'delete'],
+ }
+
+ const storageResource: PermissionResource = {
+ resource: 'project:storage',
+ title: 'Storage',
+ actions: ['write', 'create'],
+ }
+
+ it('should add a resource with "read" as the default action when available', () => {
+ const result = togglePermissionResource([], billingResource)
+ expect(result).toEqual([{ resource: 'organization:billing', actions: ['read'] }])
+ })
+
+ it('should add a resource with the first action as default when "read" is not available', () => {
+ const result = togglePermissionResource([], storageResource)
+ expect(result).toEqual([{ resource: 'project:storage', actions: ['write'] }])
+ })
+
+ it('should remove a resource if it is already in the list', () => {
+ const existing: PermissionRow[] = [
+ { resource: 'organization:billing', actions: ['read'] },
+ { resource: 'organization:members', actions: ['read', 'write'] },
+ ]
+ const result = togglePermissionResource(existing, billingResource)
+ expect(result).toEqual([{ resource: 'organization:members', actions: ['read', 'write'] }])
+ })
+
+ it('should not modify other rows when removing a resource', () => {
+ const existing: PermissionRow[] = [
+ { resource: 'organization:billing', actions: ['read'] },
+ { resource: 'organization:members', actions: ['read', 'write'] },
+ ]
+ const result = togglePermissionResource(existing, billingResource)
+ expect(result).toHaveLength(1)
+ expect(result[0]).toEqual({ resource: 'organization:members', actions: ['read', 'write'] })
+ })
+
+ it('should append to existing rows when adding', () => {
+ const existing: PermissionRow[] = [{ resource: 'organization:billing', actions: ['read'] }]
+ const result = togglePermissionResource(existing, membersResource)
+ expect(result).toHaveLength(2)
+ expect(result[1]).toEqual({ resource: 'organization:members', actions: ['read'] })
+ })
+
+ it('should not mutate the original array', () => {
+ const existing: PermissionRow[] = [{ resource: 'organization:billing', actions: ['read'] }]
+ const original = [...existing]
+ togglePermissionResource(existing, membersResource)
+ expect(existing).toEqual(original)
+ })
+
+ it('should toggle off then on again correctly', () => {
+ const step1 = togglePermissionResource([], billingResource)
+ expect(step1).toHaveLength(1)
+
+ const step2 = togglePermissionResource(step1, billingResource)
+ expect(step2).toHaveLength(0)
+
+ const step3 = togglePermissionResource(step2, billingResource)
+ expect(step3).toHaveLength(1)
+ expect(step3[0]).toEqual({ resource: 'organization:billing', actions: ['read'] })
+ })
+})