Repository navigation
Expand file tree
/
Copy pathindex.test.ts
More file actions
129 lines (112 loc) · 3.83 KB
/
Copy pathindex.test.ts
File metadata and controls
129 lines (112 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { isQueryAllowed } from './index'
import type { DataSource } from '../types'
import type { StarbaseDBConfiguration } from '../handler'
vi.mock('node-sql-parser', () => {
const Parser = vi.fn().mockImplementation(() => ({
astify: vi.fn((sql: string) => ({ type: 'select', sql })),
}))
return { Parser }
})
const mockDataSource = {
source: 'internal',
rpc: {
executeQuery: vi.fn(),
},
} as unknown as DataSource
const adminConfig: StarbaseDBConfiguration = {
outerbaseApiKey: 'key',
role: 'admin',
features: { allowlist: true, rls: false },
}
const clientConfig: StarbaseDBConfiguration = {
outerbaseApiKey: 'key',
role: 'client',
features: { allowlist: true, rls: false },
}
beforeEach(() => {
vi.clearAllMocks()
})
describe('isQueryAllowed', () => {
it('should return true when allowlist feature is disabled', async () => {
const result = await isQueryAllowed({
sql: 'SELECT 1',
isEnabled: false,
dataSource: mockDataSource,
config: clientConfig,
})
expect(result).toBe(true)
})
it('should return true for admin role regardless of allowlist', async () => {
const result = await isQueryAllowed({
sql: 'SELECT * FROM sensitive_table',
isEnabled: true,
dataSource: mockDataSource,
config: adminConfig,
})
expect(result).toBe(true)
})
it('should return an Error when no SQL is provided', async () => {
vi.mocked(mockDataSource.rpc.executeQuery).mockResolvedValue([
{ sql_statement: 'SELECT 1', source: 'internal' },
])
const result = await isQueryAllowed({
sql: '',
isEnabled: true,
dataSource: mockDataSource,
config: clientConfig,
})
expect(result).toBeInstanceOf(Error)
})
it('should allow query matching the allowlist', async () => {
vi.mocked(mockDataSource.rpc.executeQuery).mockResolvedValue([
{ sql_statement: 'SELECT * FROM users', source: 'internal' },
])
const result = await isQueryAllowed({
sql: 'SELECT * FROM users',
isEnabled: true,
dataSource: mockDataSource,
config: clientConfig,
})
expect(result).toBe(true)
})
it('should throw when query is not in allowlist', async () => {
vi.mocked(mockDataSource.rpc.executeQuery).mockResolvedValue([
{ sql_statement: 'SELECT * FROM users', source: 'internal' },
])
await expect(
isQueryAllowed({
sql: 'DROP TABLE users',
isEnabled: true,
dataSource: mockDataSource,
config: clientConfig,
})
).rejects.toThrow()
})
it('should return empty allowlist when loadAllowlist query fails', async () => {
vi.mocked(mockDataSource.rpc.executeQuery).mockRejectedValue(
new Error('DB connection failed')
)
await expect(
isQueryAllowed({
sql: 'SELECT 1',
isEnabled: true,
dataSource: mockDataSource,
config: clientConfig,
})
).rejects.toThrow()
})
it('should filter allowlist by data source', async () => {
vi.mocked(mockDataSource.rpc.executeQuery).mockResolvedValue([
{ sql_statement: 'SELECT 1', source: 'external' },
{ sql_statement: 'SELECT * FROM users', source: 'internal' },
])
const result = await isQueryAllowed({
sql: 'SELECT * FROM users',
isEnabled: true,
dataSource: mockDataSource,
config: clientConfig,
})
expect(result).toBe(true)
})
})