2
2
* database.test.ts - test driver api
3
3
*/
4
4
5
- import { SQLiteCloudRowset , SQLiteCloudRow , SQLiteCloudError , sanitizeSQLiteIdentifier } from '../src/index'
6
- import {
7
- getTestingDatabase ,
8
- getTestingDatabaseAsync ,
9
- getChinookDatabase ,
10
- removeDatabase ,
11
- removeDatabaseAsync ,
12
- LONG_TIMEOUT ,
13
- getChinookWebsocketConnection
14
- } from './shared'
5
+ import { describe , expect , it } from '@jest/globals'
15
6
import { RowCountCallback } from '../src/drivers/types'
16
- import { expect , describe , it } from '@jest/globals '
17
- import { Database } from 'sqlite3 '
7
+ import { SQLiteCloudError , SQLiteCloudRow , SQLiteCloudRowset , sanitizeSQLiteIdentifier } from '../src/index '
8
+ import { LONG_TIMEOUT , getChinookDatabase , getTestingDatabase , getTestingDatabaseAsync , removeDatabase , removeDatabaseAsync } from './shared '
18
9
19
10
//
20
11
// utility methods to setup and destroy temporary test databases
@@ -44,7 +35,6 @@ describe('Database.run', () => {
44
35
expect ( context . totalChanges ) . toBe ( 22 )
45
36
expect ( context . finalized ) . toBe ( 1 )
46
37
47
- done ( )
48
38
removeDatabase ( database , error => {
49
39
expect ( error ) . toBeNull ( )
50
40
done ( )
@@ -103,7 +93,6 @@ describe('Database.run', () => {
103
93
expect ( context . totalChanges ) . toBe ( 22 )
104
94
expect ( context . finalized ) . toBe ( 1 )
105
95
106
- done ( )
107
96
removeDatabase ( database , error => {
108
97
expect ( error ) . toBeNull ( )
109
98
done ( )
@@ -317,7 +306,7 @@ describe('Database.sql (async)', () => {
317
306
const results = await database . sql ( 'SELECT * FROM people WHERE name = ?' , 'Emma Johnson' )
318
307
expect ( results ) . toHaveLength ( 1 )
319
308
} finally {
320
- database ?. close ( )
309
+ await removeDatabaseAsync ( database )
321
310
}
322
311
} )
323
312
@@ -337,7 +326,7 @@ describe('Database.sql (async)', () => {
337
326
hobby : 'Collecting clouds'
338
327
} )
339
328
} finally {
340
- database ?. close ( )
329
+ await removeDatabaseAsync ( database )
341
330
}
342
331
} )
343
332
@@ -487,56 +476,98 @@ describe('Database.sql (async)', () => {
487
476
488
477
describe ( 'should sanitize identifiers' , ( ) => {
489
478
it ( 'should sanitize database name and run the query' , async ( ) => {
490
- const database = await getTestingDatabaseAsync ( )
479
+ let database
480
+ try {
481
+ database = await getTestingDatabaseAsync ( )
491
482
492
- const databaseName = sanitizeSQLiteIdentifier ( database . getConfiguration ( ) . database || '' )
493
- await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
483
+ const databaseName = sanitizeSQLiteIdentifier ( database . getConfiguration ( ) . database || '' )
484
+ await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
485
+ } finally {
486
+ await removeDatabaseAsync ( database )
487
+ }
494
488
} )
495
489
496
490
it ( 'should sanitize table name and run the query' , async ( ) => {
497
- const database = await getTestingDatabaseAsync ( )
491
+ let database
492
+ try {
493
+ database = await getTestingDatabaseAsync ( )
498
494
499
- const table = sanitizeSQLiteIdentifier ( 'people' )
500
- await expect ( database . sql ( `SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
495
+ const table = sanitizeSQLiteIdentifier ( 'people' )
496
+ await expect ( database . sql ( `SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
497
+ } finally {
498
+ await removeDatabaseAsync ( database )
499
+ }
501
500
} )
502
501
503
502
it ( 'should sanitize SQL Injection as table name' , async ( ) => {
504
- const database = await getTestingDatabaseAsync ( )
505
- const databaseName = database . getConfiguration ( ) . database
503
+ let database
504
+ try {
505
+ database = await getTestingDatabaseAsync ( )
506
+ const databaseName = database . getConfiguration ( ) . database
506
507
507
- const sanitizedDBName = sanitizeSQLiteIdentifier ( `${ databaseName } ; SELECT * FROM people; -- ` )
508
- await expect ( database . sql ( `USE DATABASE ${ sanitizedDBName } ` ) ) . rejects . toThrow (
509
- `Database name contains invalid characters (${ databaseName } ; SELECT * FROM people; --).`
510
- )
508
+ const sanitizedDBName = sanitizeSQLiteIdentifier ( `${ databaseName } ; SELECT * FROM people; -- ` )
509
+ await expect ( database . sql ( `USE DATABASE ${ sanitizedDBName } ` ) ) . rejects . toThrow (
510
+ `Database name contains invalid characters (${ databaseName } ; SELECT * FROM people; --).`
511
+ )
511
512
512
- const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
513
- await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
513
+ const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
514
+ await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
515
+ } finally {
516
+ await removeDatabaseAsync ( database )
517
+ }
514
518
} )
515
519
} )
516
520
517
521
it ( 'should throw exception when using table name as binding' , async ( ) => {
518
- const database = await getTestingDatabaseAsync ( )
519
- const table = 'people'
520
- await expect ( database . sql `SELECT * FROM ${ table } ` ) . rejects . toThrow ( 'near "?": syntax error' )
522
+ let database
523
+ try {
524
+ database = await getTestingDatabaseAsync ( )
525
+ const table = 'people'
526
+ await expect ( database . sql `SELECT * FROM ${ table } ` ) . rejects . toThrow ( 'near "?": syntax error' )
527
+ } finally {
528
+ await removeDatabaseAsync ( database )
529
+ }
521
530
} )
522
531
523
- it ( 'should built in commands accept bindings' , async ( ) => {
524
- const database = await getTestingDatabaseAsync ( )
532
+ it ( 'should commands accept bindings' , async ( ) => {
533
+ let database
534
+ try {
535
+ database = await getTestingDatabaseAsync ( )
536
+
537
+ const databaseName = database . getConfiguration ( ) . database || ''
538
+ await expect ( database . sql `USE DATABASE ${ databaseName } ` ) . resolves . toBe ( 'OK' )
539
+
540
+ const databaseNameInjectSQL = `${ databaseName } ; SELECT * FROM people`
541
+ await expect ( database . sql `USE DATABASE ${ databaseNameInjectSQL } ` ) . rejects . toThrow ( `Database name contains invalid characters (${ databaseNameInjectSQL } ).` )
542
+
543
+ let key = 'logo_level'
544
+ let value = 'debug'
545
+ await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
525
546
526
- const databaseName = database . getConfiguration ( ) . database || ''
527
- await expect ( database . sql `USE DATABASE ${ databaseName } ` ) . resolves . toBe ( 'OK' )
547
+ key = 'logo_level'
548
+ value = 'debug; DROP TABLE people'
549
+ await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
550
+ const result = await database . sql `SELECT * FROM people`
551
+ expect ( result . length ) . toBeGreaterThan ( 0 )
552
+ } finally {
553
+ await removeDatabaseAsync ( database )
554
+ }
555
+ } )
528
556
529
- const databaseNameInjectSQL = `${ databaseName } ; SELECT * FROM people`
530
- await expect ( database . sql `USE DATABASE ${ databaseNameInjectSQL } ` ) . rejects . toThrow ( `Database name contains invalid characters (${ databaseNameInjectSQL } ).` )
557
+ it ( 'binding should work with unicode character' , async ( ) => {
558
+ let database
559
+ try {
560
+ database = await getTestingDatabaseAsync ( )
561
+ const name = 'unicorn-🦄'
531
562
532
- let key = 'logo_level'
533
- let value = 'debug'
534
- await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
563
+ let results = await database . sql ( 'INSERT INTO people (name, age, hobby) VALUES (?, 11, "");' , name )
564
+ expect ( results . changes ) . toEqual ( 1 )
535
565
536
- key = 'logo_level'
537
- value = 'debug; DROP TABLE people'
538
- await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
539
- const result = await database . sql `SELECT * FROM people`
540
- expect ( result . length ) . toBeGreaterThan ( 0 )
566
+ results = await database . sql ( 'SELECT * FROM people WHERE name = ?;' , name )
567
+ expect ( results ) . toHaveLength ( 1 )
568
+ expect ( results [ 0 ] . name ) . toEqual ( name )
569
+ } finally {
570
+ await removeDatabaseAsync ( database )
571
+ }
541
572
} )
542
573
} )
0 commit comments