| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- import { afterAll, beforeAll, expect, test } from 'vitest'
- import pgMeta from '../src/index'
- import { cleanupRoot, createTestDatabase } from './db/utils'
- beforeAll(async () => {
- // Any global setup if needed
- })
- afterAll(async () => {
- await cleanupRoot()
- })
- const withTestDatabase = (
- name: string,
- fn: (db: Awaited<ReturnType<typeof createTestDatabase>>) => Promise<void>
- ) => {
- test(name, async () => {
- const db = await createTestDatabase()
- try {
- await fn(db)
- } finally {
- await db.cleanup()
- }
- })
- }
- withTestDatabase('list table privileges', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tablePrivileges.list()
- const res = zod.parse(await executeQuery(sql))
- expect(
- res.find(({ schema, name }) => schema === 'public' && name === 'todos')
- ).toMatchInlineSnapshot(
- { relation_id: expect.any(Number) },
- `
- {
- "kind": "table",
- "name": "todos",
- "privileges": [
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "INSERT",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "SELECT",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "UPDATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "DELETE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRUNCATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "REFERENCES",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRIGGER",
- },
- ],
- "relation_id": Any<Number>,
- "schema": "public",
- }
- `
- )
- })
- withTestDatabase('revoke & grant table privileges', async ({ executeQuery }) => {
- // Get initial table privileges
- const { sql: listSql, zod: listZod } = await pgMeta.tablePrivileges.list()
- const listRes = listZod.parse(await executeQuery(listSql))
- const { relation_id } = listRes.find(
- ({ schema, name }) => schema === 'public' && name === 'todos'
- )!
- // Revoke all privileges
- const { sql: revokeSql } = pgMeta.tablePrivileges.revoke([
- {
- relationId: relation_id,
- grantee: 'postgres',
- privilegeType: 'ALL',
- },
- ])
- await executeQuery(revokeSql)
- // Verify privileges were revoked
- const { sql: retrieveSql, zod: retrieveZod } = await pgMeta.tablePrivileges.retrieve({
- id: relation_id,
- })
- let privs = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(privs).toMatchInlineSnapshot(
- { relation_id: expect.any(Number) },
- `
- {
- "kind": "table",
- "name": "todos",
- "privileges": [],
- "relation_id": Any<Number>,
- "schema": "public",
- }
- `
- )
- // Grant all privileges back
- const { sql: grantSql } = pgMeta.tablePrivileges.grant([
- {
- relationId: relation_id,
- grantee: 'postgres',
- privilegeType: 'ALL',
- },
- ])
- await executeQuery(grantSql)
- // Verify privileges were granted
- const { sql: verifyGrantSql } = await pgMeta.tablePrivileges.retrieve({ id: relation_id })
- privs = retrieveZod.parse((await executeQuery(verifyGrantSql))[0])
- expect(privs).toMatchInlineSnapshot(
- { relation_id: expect.any(Number) },
- `
- {
- "kind": "table",
- "name": "todos",
- "privileges": [
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRIGGER",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "REFERENCES",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRUNCATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "DELETE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "UPDATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "SELECT",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "INSERT",
- },
- ],
- "relation_id": Any<Number>,
- "schema": "public",
- }
- `
- )
- })
- withTestDatabase(
- 'revoke & grant table privileges w/ quoted table name',
- async ({ executeQuery }) => {
- // Create test role and schema
- await executeQuery(`create role r; create schema "s 1"; create table "s 1"."t 1"();`)
- // Get table privileges
- const { sql: listSql, zod: listZod } = await pgMeta.tablePrivileges.list()
- const listRes = listZod.parse(await executeQuery(listSql))
- const { relation_id } = listRes.find(({ schema, name }) => schema === 's 1' && name === 't 1')!
- // Grant all privileges
- const { sql: grantSql } = pgMeta.tablePrivileges.grant([
- {
- relationId: relation_id,
- grantee: 'r',
- privilegeType: 'ALL',
- },
- ])
- await executeQuery(grantSql)
- // Verify privileges were granted
- const { sql: retrieveSql, zod: retrieveZod } = await pgMeta.tablePrivileges.retrieve({
- id: relation_id,
- })
- let privs = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(privs).toMatchInlineSnapshot(
- { relation_id: expect.any(Number) },
- `
- {
- "kind": "table",
- "name": "t 1",
- "privileges": [
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRIGGER",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "REFERENCES",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRUNCATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "DELETE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "UPDATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "SELECT",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "INSERT",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRIGGER",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "REFERENCES",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRUNCATE",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "DELETE",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "UPDATE",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "SELECT",
- },
- {
- "grantee": "r",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "INSERT",
- },
- ],
- "relation_id": Any<Number>,
- "schema": "s 1",
- }
- `
- )
- // Revoke all privileges
- const { sql: revokeSql } = pgMeta.tablePrivileges.revoke([
- {
- relationId: relation_id,
- grantee: 'r',
- privilegeType: 'ALL',
- },
- ])
- await executeQuery(revokeSql)
- // Verify privileges were revoked
- const { sql: verifyRevokeSql } = await pgMeta.tablePrivileges.retrieve({ id: relation_id })
- privs = retrieveZod.parse((await executeQuery(verifyRevokeSql))[0])
- expect(privs).toMatchInlineSnapshot(
- { relation_id: expect.any(Number) },
- `
- {
- "kind": "table",
- "name": "t 1",
- "privileges": [
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRIGGER",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "REFERENCES",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "TRUNCATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "DELETE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "UPDATE",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "SELECT",
- },
- {
- "grantee": "postgres",
- "grantor": "postgres",
- "is_grantable": false,
- "privilege_type": "INSERT",
- },
- ],
- "relation_id": Any<Number>,
- "schema": "s 1",
- }
- `
- )
- }
- )
|