44
55import { SQLiteCloudError } from '../src/index'
66import { SQLiteCloudConnection , anonimizeCommand } from '../src/connection'
7+ import { parseConnectionString } from '../src/utilities'
78import {
9+ //
810 CHINOOK_DATABASE_URL ,
911 LONG_TIMEOUT ,
10- getTestingConfig ,
1112 getChinookConfig ,
1213 getChinookWebsocketConnection ,
13- // clearTestingDatabasesAsync,
1414 WARN_SPEED_MS ,
1515 EXPECT_SPEED_MS
1616} from './shared'
@@ -32,28 +32,11 @@ describe('connection-ws', () => {
3232 it ( 'should connect' , ( ) => {
3333 // ...in beforeEach
3434 } )
35- /*
36- it(
37- 'should drop all old testing databases',
38- async () => {
39- await clearTestingDatabasesAsync()
40- },
41- LONG_TIMEOUT
42- )
43- */
44- it ( 'should add self signed certificate for localhost connections' , ( ) => {
45- const localConfig = getChinookConfig ( 'sqlitecloud://admin:xxx@localhost:8850/chinook.db' )
46- expect ( localConfig . host ) . toBe ( 'localhost' )
47- expect ( localConfig . tlsOptions ?. ca ) . toBeTruthy ( )
48-
49- const remoteConfig = getChinookConfig ( 'sqlitecloud://admin:xxx@sqlitecloud.io:8850/chinook.db' )
50- expect ( remoteConfig . host ) . toBe ( 'sqlitecloud.io' )
51- expect ( remoteConfig . tlsOptions ) . toBeFalsy ( )
52- } )
5335
5436 it ( 'should connect with config object string' , done => {
5537 const configObj = getChinookConfig ( )
56- const connection = new SQLiteCloudWebsocketConnection ( configObj )
38+ configObj . websocketOptions = { useWebsocket : true }
39+ const connection = new SQLiteCloudConnection ( configObj )
5740 expect ( connection ) . toBeDefined ( )
5841 connection . sendCommands ( 'TEST STRING' , ( error , results ) => {
5942 connection . close ( )
@@ -62,13 +45,33 @@ describe('connection-ws', () => {
6245 } )
6346 } )
6447
48+ it ( 'should not connect with incorrect credentials' , done => {
49+ const configObj = getChinookConfig ( )
50+ configObj . connectionString ?. replace ( configObj . password as string , 'wrongpassword' )
51+ configObj . password = 'wrongpassword'
52+ configObj . websocketOptions = { useWebsocket : true }
53+
54+ // should attemp connection and return error
55+ const connection = new SQLiteCloudConnection ( configObj )
56+ expect ( connection ) . toBeDefined ( )
57+ connection . sendCommands ( 'TEST STRING' , ( error , results ) => {
58+ expect ( error ) . toBeDefined ( )
59+ expect ( error ) . toBeInstanceOf ( SQLiteCloudError )
60+ expect ( ( error as any ) . message ) . toBe ( 'SQLiteCloudError: Authentication failed.' )
61+
62+ connection . close ( )
63+ expect ( connection . connected ) . toBe ( false )
64+ done ( )
65+ } )
66+ } )
67+
6568 it ( 'should connect with connection string' , done => {
6669 if ( CHINOOK_DATABASE_URL . indexOf ( 'localhost' ) > 0 ) {
6770 // skip this test when running locally since it requires a self-signed certificate
6871 done ( )
6972 }
7073
71- const conn = new SQLiteCloudWebsocketConnection ( CHINOOK_DATABASE_URL , error => {
74+ const conn = new SQLiteCloudConnection ( CHINOOK_DATABASE_URL , error => {
7275 expect ( error ) . toBeNull ( )
7376 expect ( conn . connected ) . toBe ( true )
7477
@@ -80,27 +83,6 @@ describe('connection-ws', () => {
8083 } )
8184 expect ( conn ) . toBeDefined ( )
8285 } )
83-
84- it ( 'should throw when connection string lacks credentials' , done => {
85- // use valid connection string but without credentials
86- const testingConfig = getTestingConfig ( )
87- delete testingConfig . username
88- delete testingConfig . password
89-
90- try {
91- const conn = new SQLiteCloudWebsocketConnection ( testingConfig )
92- } catch ( error ) {
93- expect ( error ) . toBeDefined ( )
94- expect ( error ) . toBeInstanceOf ( SQLiteCloudError )
95- const sqliteCloudError = error as SQLiteCloudError
96- expect ( sqliteCloudError . message ) . toBe ( 'The user, password and host arguments must be specified.' )
97- expect ( sqliteCloudError . errorCode ) . toBe ( 'ERR_MISSING_ARGS' )
98- expect ( sqliteCloudError . externalErrorCode ) . toBeUndefined ( )
99- expect ( sqliteCloudError . offsetCode ) . toBeUndefined ( )
100-
101- done ( )
102- }
103- } )
10486 } )
10587
10688 describe ( 'send test commands' , ( ) => {
@@ -299,30 +281,30 @@ describe('connection-ws', () => {
299281 } ,
300282 LONG_TIMEOUT
301283 )
302-
284+ /* TODO RESTORE TEST
303285 it('should apply short timeout', done => {
304- // this operation sends 150 packets and cannot complete in 20ms
305- const database = getChinookWebsocketConnection (
306- error => {
307- if ( error ) {
286+ // apply shorter timeout
287+ const configObj = parseConnectionString(CHINOOK_DATABASE_URL + '?timeout=20')
288+ configObj.websocketOptions = { useWebsocket: true }
289+ const database = new SQLiteCloudConnection(configObj, error => {
290+ if (error) {
291+ expect(error).toBeInstanceOf(SQLiteCloudError)
292+ expect((error as any).message).toBe('Request timed out')
293+ done()
294+ database.close()
295+ } else {
296+ // this operation sends 150 packets and cannot complete in 20ms
297+ database.sendCommands('TEST ROWSET_CHUNK', (error, results) => {
308298 expect(error).toBeInstanceOf(SQLiteCloudError)
309299 expect((error as any).message).toBe('Request timed out')
310300 done()
311301 database.close()
312- } else {
313- database . sendCommands ( 'TEST ROWSET_CHUNK' , ( error , results ) => {
314- expect ( error ) . toBeInstanceOf ( SQLiteCloudError )
315- expect ( ( error as any ) . message ) . toBe ( 'Request timed out' )
316- done ( )
317- database . close ( )
318- } )
319- }
320- } ,
321- { timeout : 20 }
322- )
302+ })
303+ }
304+ })
323305 })
324306 })
325-
307+ */
326308 describe ( 'send select commands' , ( ) => {
327309 it ( 'should LIST METADATA' , done => {
328310 chinook . sendCommands ( 'LIST METADATA;' , ( error , results ) => {
0 commit comments