Skip to content

Commit 5c504da

Browse files
committed
add some test...
1 parent d71e986 commit 5c504da

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎cdk-postgresql/test/lambda.integration.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ afterAll(async () => {
8181
// starts from a blank cluster:
8282
beforeEach(() => cluster.reset());
8383

84-
test("read access covers existing/RLS and future tables from both creators without writes", async () => {
84+
test.each([true, false, undefined])("read access covers existing/RLS and future tables without writes (BypassRls: %s)", async (bypassRls) => {
8585
const client = await cluster.connectTo(DB_DEFAULT_DB);
8686
try {
8787
await client.query(`
@@ -92,6 +92,8 @@ test("read access covers existing/RLS and future tables from both creators witho
9292
CREATE TABLE public.items (id int);
9393
INSERT INTO public.items VALUES (1);
9494
ALTER TABLE public.items ENABLE ROW LEVEL SECURITY;
95+
CREATE TABLE public.ordinary (id int);
96+
INSERT INTO public.ordinary VALUES (1);
9597
RESET ROLE;
9698
`);
9799
const event = {
@@ -106,7 +108,8 @@ test("read access covers existing/RLS and future tables from both creators witho
106108
Username: DB_MASTER_USERNAME, PasswordArn: masterPasswordArn, SSLMode: "disable",
107109
},
108110
Role: "reader", Schema: "public",
109-
CreatorRoles: ["app", DB_MASTER_USERNAME], BypassRls: true,
111+
CreatorRoles: ["app", DB_MASTER_USERNAME],
112+
...(bypassRls === undefined ? {} : { BypassRls: bypassRls }),
110113
},
111114
} as CloudFormationCustomResourceEvent;
112115
await expect(handler({
@@ -127,14 +130,19 @@ test("read access covers existing/RLS and future tables from both creators witho
127130
RESET ROLE;
128131
SET ROLE reader;
129132
`);
130-
for (const table of ["items", "from_master", "from_app"]) {
133+
expect((await client.query(
134+
"SELECT rolbypassrls FROM pg_roles WHERE rolname = current_user"
135+
)).rows[0].rolbypassrls).toBe(bypassRls === true);
136+
expect((await client.query("SELECT * FROM public.items")).rowCount).toBe(bypassRls ? 1 : 0);
137+
for (const table of ["ordinary", "from_master", "from_app"]) {
131138
expect((await client.query(`SELECT * FROM public.${table}`)).rowCount).toBe(1);
132139
}
133140
await expect(client.query("INSERT INTO public.items VALUES (4)")).rejects.toThrow(/permission denied/);
134141
await expect(client.query("UPDATE public.items SET id = 4")).rejects.toThrow(/permission denied/);
135142
await expect(client.query("DELETE FROM public.items")).rejects.toThrow(/permission denied/);
136143
await handler({ ...event, RequestType: "Delete", PhysicalResourceId: "ReadAccess" });
137-
expect((await client.query("SELECT * FROM public.items")).rowCount).toBe(1);
144+
expect((await client.query("SELECT * FROM public.items")).rowCount).toBe(bypassRls ? 1 : 0);
145+
expect((await client.query("SELECT * FROM public.ordinary")).rowCount).toBe(1);
138146
} finally {
139147
await client.end();
140148
}

0 commit comments

Comments
 (0)