-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.test.ts
More file actions
206 lines (169 loc) · 6.05 KB
/
utilities.test.ts
File metadata and controls
206 lines (169 loc) · 6.05 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// utilities.test.ts
//
import { SQLiteCloudError } from '../src/index'
import { getInitializationCommands, parseconnectionstring, sanitizeSQLiteIdentifier } from '../src/drivers/utilities'
import { getTestingDatabaseName } from './shared'
import { expect, describe, it } from '@jest/globals'
describe('parseconnectionstring', () => {
it('should parse connection string', () => {
const connectionstring = 'sqlitecloud://user:password@host:1234/database?option1=xxx&option2=yyy'
const config = parseconnectionstring(connectionstring)
expect(config).toEqual({
username: 'user',
password: 'password',
host: 'host',
port: 1234,
database: 'database',
option1: 'xxx',
option2: 'yyy'
})
})
it('should parse connection string without database or options', () => {
const connectionstring = 'sqlitecloud://user:password@host:1234'
const config = parseconnectionstring(connectionstring)
expect(config).toEqual({
username: 'user',
password: 'password',
host: 'host',
port: 1234
})
})
it('should parse options regardless of case', () => {
// NOTE: apiKey intentionally mixedCase here...
const connectionstring1 = 'sqlitecloud://host?apiKey=xxx'
const config1 = parseconnectionstring(connectionstring1)
expect(config1).toEqual({
host: 'host',
apikey: 'xxx'
})
const connectionstring2 = 'sqlitecloud://host?apikey=yyy'
const config2 = parseconnectionstring(connectionstring2)
expect(config2).toEqual({
host: 'host',
apikey: 'yyy'
})
const connectionstring4 = 'sqlitecloud://host?apiKey=yyy&maxRows=42'
const config4 = parseconnectionstring(connectionstring4)
expect(config4).toEqual({
host: 'host',
apikey: 'yyy',
maxrows: 42 // only parsing here, validation is later in validateConfiguration
})
})
it('should parse connection string without port', () => {
const connectionstring = 'sqlitecloud://user:password@host'
const config = parseconnectionstring(connectionstring)
expect(config).toEqual({
username: 'user',
password: 'password',
host: 'host'
})
})
it('should throw SQLiteCloudError if the connection string is invalid', () => {
const connectionstring = 'not a valid url'
expect(() => {
parseconnectionstring(connectionstring)
}).toThrow(SQLiteCloudError)
})
it('should handle connection strings without port', () => {
const connectionstring = 'sqlitecloud://user:password@host/database?option1=xxx&option2=yyy'
const result = parseconnectionstring(connectionstring)
expect(result).toEqual({
username: 'user',
password: 'password',
host: 'host',
port: undefined,
database: 'database',
option1: 'xxx',
option2: 'yyy'
})
})
it('should handle connection strings without options', () => {
const connectionstring = 'sqlitecloud://user:password@host:1234/database'
const config = parseconnectionstring(connectionstring)
expect(config).toEqual({
username: 'user',
password: 'password',
host: 'host',
port: 1234,
database: 'database'
})
})
it('should handle url encoded password', () => {
const connectionstring = 'sqlitecloud://user:pass%25word@host:1234/database'
const config = parseconnectionstring(connectionstring)
expect(config).toEqual({
username: 'user',
password: 'pass%word',
host: 'host',
port: 1234,
database: 'database'
})
})
it('should parse connection with api key', () => {
const apikey = 'mIiLARzKm9XBVllbAzkB1wqrgijJ3Gx0X5z1Agm3xBo'
const connectionstring = `sqlitecloud://host:1234/database?apikey=${apikey}`
const config = parseconnectionstring(connectionstring)
expect(config.apikey).toBe(apikey)
expect(config.username).toBeUndefined()
expect(config.password).toBeUndefined()
expect(config.password_hashed).toBeUndefined()
expect(config).toEqual({
apikey,
host: 'host',
port: 1234,
database: 'database'
})
})
it('should parse connection with insecure as bool or number', () => {
let connectionstring = `sqlitecloud://host:1234/database?insecure=true`
let config = parseconnectionstring(connectionstring)
expect(config.insecure).toBe(true)
connectionstring = `sqlitecloud://host:1234/database?insecure=1`
config = parseconnectionstring(connectionstring)
expect(config.insecure).toBe(true)
connectionstring = `sqlitecloud://host:1234/database?insecure=0`
config = parseconnectionstring(connectionstring)
expect(config.insecure).toBe(false)
})
it('should parse connection with timeout as number', () => {
let connectionstring = `sqlitecloud://host:1234/database?timeout=123`
let config = parseconnectionstring(connectionstring)
expect(config.timeout).toBe(123)
})
})
describe('getTestingDatabaseName', () => {
it('should generate readable database names', () => {
const database = getTestingDatabaseName('benchkmark')
expect(database).toBeTruthy()
})
})
describe('sanitizeSQLiteIdentifier()', () => {
it('should trim and escape valid identifier', () => {
const identifier = ' valid_identifier '
const sanitized = sanitizeSQLiteIdentifier(identifier)
expect(sanitized).toBe('"valid_identifier"')
})
it('valid indentifier', () => {
const identifier = "a_colName1"
const sanitized = sanitizeSQLiteIdentifier(identifier)
expect(sanitized).toBe('"a_colName1"')
})
it('should double quotes for sql injection', () => {
const identifier = ' chinook.sql; DROP TABLE "albums" '
const sanitized = sanitizeSQLiteIdentifier(identifier)
expect(sanitized).toBe('"chinook.sql; DROP TABLE \"\"albums\"\""')
})
})
describe('getInitializationCommands()', () => {
it('should return commands with auth token command', () => {
const config = {
token: 'mytoken',
database: 'mydb',
}
const result = getInitializationCommands(config)
expect(result).toContain('AUTH TOKEN mytoken;')
expect(result).not.toContain('AUTH APIKEY')
})
})