Skip to content

Commit 9ac9b0a

Browse files
committed
fix: some tests and policies error return with invalid params
1 parent ecdbe1e commit 9ac9b0a

File tree

8 files changed

+53
-80
lines changed

8 files changed

+53
-80
lines changed

src/lib/PostgresMetaPolicies.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export default class PostgresMetaPolicies {
112112
command?: string
113113
roles?: string[]
114114
}): Promise<PostgresMetaResult<PostgresPolicy>> {
115+
if (!table || !name) {
116+
return { data: null, error: { message: 'Missing required name or table parameter' } }
117+
}
115118
const definitionClause = definition === undefined ? '' : `USING (${definition})`
116119
const checkClause = check === undefined ? '' : `WITH CHECK (${check})`
117120
const sql = `

test/server/config.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@ test('config version endpoint', async () => {
1414
expect(data.version).toContain('PostgreSQL')
1515
})
1616

17-
// Skip tests for endpoints that don't exist
18-
test.skip('config max_result_size endpoint', async () => {
19-
const res = await app.inject({
20-
method: 'GET',
21-
path: '/config/max_result_size',
22-
})
23-
expect(res.statusCode).toBe(200)
24-
})
25-
26-
test.skip('config health endpoint', async () => {
27-
const res = await app.inject({
28-
method: 'GET',
29-
path: '/config/health',
30-
})
31-
expect(res.statusCode).toBe(200)
32-
})
33-
3417
test('config with invalid endpoint', async () => {
3518
const res = await app.inject({
3619
method: 'GET',

test/server/extensions.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ test('extension list filtering', async () => {
1212
expect(extensions.length).toBeLessThanOrEqual(5)
1313
})
1414

15-
test('extension list with specific included schema', async () => {
16-
const res = await app.inject({
17-
method: 'GET',
18-
path: '/extensions?includedSchemas=public',
19-
})
20-
expect(res.statusCode).toBe(200)
21-
const extensions = res.json()
22-
expect(Array.isArray(extensions)).toBe(true)
23-
// Just make sure we get extensions, don't check schema as it depends on environment
24-
expect(extensions.length).toBeGreaterThanOrEqual(0)
25-
})
26-
2715
test('extension with invalid id', async () => {
2816
const res = await app.inject({
2917
method: 'GET',

test/server/format.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ test('format SQL query', async () => {
1010
expect(res.statusCode).toBe(200)
1111
expect(res.headers['content-type']).toContain('text/plain')
1212
const formattedQuery = res.body
13-
expect(formattedQuery).toContain('SELECT')
14-
expect(formattedQuery).toContain('FROM')
15-
expect(formattedQuery).toContain('WHERE')
13+
expect(formattedQuery).toMatchInlineSnapshot(`
14+
"SELECT
15+
id,
16+
name
17+
FROM
18+
users
19+
WHERE
20+
status = 'ACTIVE'
21+
"
22+
`)
1623
})
1724

1825
test('format complex SQL query', async () => {
@@ -26,7 +33,24 @@ test('format complex SQL query', async () => {
2633
})
2734
expect(res.statusCode).toBe(200)
2835
expect(res.headers['content-type']).toContain('text/plain')
29-
expect(res.body).toBeTruthy()
36+
expect(res.body).toMatchInlineSnapshot(`
37+
"SELECT
38+
u.id,
39+
u.name,
40+
p.title,
41+
p.created_at
42+
FROM
43+
users u
44+
JOIN posts p ON u.id = p.user_id
45+
WHERE
46+
u.status = 'ACTIVE'
47+
AND p.published = true
48+
ORDER BY
49+
p.created_at DESC
50+
LIMIT
51+
10
52+
"
53+
`)
3054
})
3155

3256
test('format invalid SQL query', async () => {
@@ -35,12 +59,17 @@ test('format invalid SQL query', async () => {
3559
path: '/query/format',
3660
payload: { query: 'SELECT FROM WHERE;' },
3761
})
38-
// Even invalid SQL can be formatted
3962
expect(res.statusCode).toBe(200)
4063
expect(res.headers['content-type']).toContain('text/plain')
41-
expect(res.body).toBeTruthy()
64+
expect(res.body).toMatchInlineSnapshot(`
65+
"SELECT
66+
FROM
67+
WHERE;
68+
"
69+
`)
4270
})
4371

72+
// TODO(andrew): Those should return 400 error code for invalid parameter
4473
test('format empty query', async () => {
4574
const res = await app.inject({
4675
method: 'POST',

test/server/generators.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test('typescript generator route', async () => {
88
})
99
expect(res.statusCode).toBe(200)
1010
expect(res.headers['content-type']).toContain('text/plain')
11-
expect(res.body).toBeTruthy()
11+
expect(res.body).contain('public')
1212
})
1313

1414
test('go generator route', async () => {
@@ -34,22 +34,18 @@ test('swift generator route', async () => {
3434
test('generator routes with includedSchemas parameter', async () => {
3535
const res = await app.inject({
3636
method: 'GET',
37-
path: '/generators/typescript?includedSchemas=public',
37+
path: '/generators/typescript?included_schemas=private',
3838
})
3939
expect(res.statusCode).toBe(200)
4040
expect(res.headers['content-type']).toContain('text/plain')
41-
expect(res.body).toBeTruthy()
41+
// the only schema is excluded database should be empty
42+
expect(res.body).toContain('Database = {}')
4243
})
4344

44-
// Skip this test as the OpenAPI endpoint is not implemented
45-
test.skip('openapi generator route', async () => {
45+
test('invalid generator route', async () => {
4646
const res = await app.inject({
4747
method: 'GET',
4848
path: '/generators/openapi',
4949
})
50-
expect(res.statusCode).toBe(200)
51-
expect(res.headers['content-type']).toContain('application/json')
52-
const body = JSON.parse(res.body)
53-
expect(body.openapi).toBeTruthy()
54-
expect(body.paths).toBeTruthy()
50+
expect(res.statusCode).toBe(404)
5551
})

test/server/policies.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('policy list filtering', async () => {
1515
test('policy list with specific included schema', async () => {
1616
const res = await app.inject({
1717
method: 'GET',
18-
path: '/policies?includedSchemas=public',
18+
path: '/policies?included_schema=public',
1919
})
2020
expect(res.statusCode).toBe(200)
2121
const policies = res.json()
@@ -48,8 +48,7 @@ test('create policy with missing required field', async () => {
4848
command: 'PERMISSIVE',
4949
},
5050
})
51-
// The API returns 500 instead of 400 for invalid parameters
52-
expect(res.statusCode).toBe(500)
51+
expect(res.statusCode).toBe(400)
5352
})
5453

5554
test('update policy with invalid id', async () => {

test/server/publications.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ test('create publication with invalid options', async () => {
3434
},
3535
})
3636
// API accepts invalid type and converts it
37+
// TODO: This should error out with invalid parameter
3738
expect(res.statusCode).toBe(200)
3839
})
3940

test/server/types.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@ test('type list with specific included schema', async () => {
2626
})
2727
})
2828

29-
// Skip this test as it seems array types aren't included in the way we expect
30-
test.skip('type list with includeArrayTypes parameter', async () => {
31-
const res = await app.inject({
32-
method: 'GET',
33-
path: '/types?includeArrayTypes=true',
34-
})
35-
expect(res.statusCode).toBe(200)
36-
const types = res.json()
37-
expect(Array.isArray(types)).toBe(true)
38-
// Should include array types
39-
const arrayTypes = types.filter((type) => type.name.startsWith('_'))
40-
expect(arrayTypes.length).toBeGreaterThan(0)
41-
})
42-
4329
test('type list excluding array types', async () => {
4430
const res = await app.inject({
4531
method: 'GET',
@@ -61,28 +47,16 @@ test('type with invalid id', async () => {
6147
expect(res.statusCode).toBe(404)
6248
})
6349

64-
// Skip enum test as we're having issues finding a valid enum type
65-
test.skip('type with enum values', async () => {
50+
test('type with enum values', async () => {
6651
// Find an enum type first
6752
const listRes = await app.inject({
6853
method: 'GET',
69-
path: '/types?filter.type_type=eq.e',
54+
path: '/types',
7055
})
7156
expect(listRes.statusCode).toBe(200)
72-
const enumTypes = listRes.json()
57+
const types = listRes.json()
58+
const enumType = types.find((t) => t.name === 'meme_status')
7359

74-
if (enumTypes.length > 0) {
75-
const enumTypeId = enumTypes[0].id
76-
const res = await app.inject({
77-
method: 'GET',
78-
path: `/types/${enumTypeId}`,
79-
})
80-
expect(res.statusCode).toBe(200)
81-
const type = res.json()
82-
expect(type).toHaveProperty('enums')
83-
expect(Array.isArray(type.enums)).toBe(true)
84-
} else {
85-
// Skip if no enum types are found
86-
console.log('No enum types found, skipping enum values test')
87-
}
60+
expect(Array.isArray(enumType.enums)).toBe(true)
61+
expect(enumType.enums.length).toBeGreaterThan(0)
8862
})

0 commit comments

Comments
 (0)