22 * built-in-commands.test.ts - test sqlitecloud built-in commands
33 */
44
5+ import { createHash } from 'crypto'
56import {
67 _ ,
78 CHINOOK_DATABASE_URL ,
@@ -18,7 +19,8 @@ import {
1819 colseq ,
1920 screaming_snake_case ,
2021 regex_IP_UUID_N ,
21- test
22+ test ,
23+ CHINOOK_API_KEY
2224} from './shared'
2325
2426describe . each ( [
@@ -194,26 +196,27 @@ describe.each([
194196} )
195197
196198describe . each ( [
197- [ 'admin' , randomName ( ) , randomDate ( ) , true ] ,
198- [ 'admin' , randomName ( ) , 'WRONG_DATE' , false ] ,
199- [ 'NOT_EXIST' , randomName ( ) , randomDate ( ) , false ] ,
200- [ 'admin' , '' , randomDate ( ) , false ]
201- ] ) ( 'api key' , ( username , keyName , expiration , ok ) => {
202- let key : string
199+ [ 'admin' , randomName ( ) , randomDate ( ) , _ , true ] ,
200+ [ 'admin' , randomName ( ) , randomDate ( ) , randomName ( ) , true ] ,
201+ [ 'admin' , randomName ( ) , 'WRONG_DATE' , _ , false ] ,
202+ [ 'NOT_EXIST' , randomName ( ) , randomDate ( ) , randomName ( ) , false ] ,
203+ [ 'admin' , '' , randomDate ( ) , _ , false ]
204+ ] ) ( 'api key' , ( username , keyName , expiration , key , ok ) => {
205+ let generated_key : string
203206
204207 it ( `should${ ok ? '' : "n't" } create` , done => {
205208 const chinook = getConnection ( )
206209 chinook . sendCommands (
207- `CREATE APIKEY USER ${ username } NAME ${ keyName } EXPIRATION "${ expiration } "` ,
208- test ( done , chinook , ok , / ^ [ a - z A - Z 0 - 9 ] { 43 } $ / , ( res : string ) => ( key = res ) )
210+ `CREATE APIKEY USER ${ username } NAME ${ keyName } EXPIRATION "${ expiration } "${ key ? ` KEY ${ key } ` : '' } ` ,
211+ test ( done , chinook , ok , key ? key : / ^ [ a - z A - Z 0 - 9 ] { 43 } $ / , ( res : string ) => ( generated_key = res ) )
209212 )
210213 } )
211214
212215 it ( `should${ ok ? '' : "n't" } list created` , done => {
213216 const chinook = getConnection ( )
214217 chinook . sendCommands (
215218 `LIST APIKEYS USER ${ username } ${ username == 'admin' ? '; LIST MY APIKEYS' : '' } ` ,
216- test ( done , chinook , ok , { creation_date : date ( ) , expiration_date : expiration , key : key , name : keyName } )
219+ test ( done , chinook , ok , { creation_date : date ( ) , expiration_date : expiration , key : generated_key , name : keyName } )
217220 )
218221 } )
219222
@@ -224,29 +227,29 @@ describe.each([
224227 expiration = randomDate ( )
225228 const chinook = getConnection ( )
226229 chinook . sendCommands (
227- `SET APIKEY ${ key } NAME ${ keyName } EXPIRATION "${ expiration } "; LIST APIKEYS USER ${ username } ` ,
228- test ( done , chinook , false , { creation_date : date ( ) , expiration_date : prevExpiration , key : key , name : prevKeyName } )
230+ `SET APIKEY ${ generated_key } NAME ${ keyName } EXPIRATION "${ expiration } "; LIST APIKEYS USER ${ username } ` ,
231+ test ( done , chinook , false , { creation_date : date ( ) , expiration_date : prevExpiration , key : generated_key , name : prevKeyName } )
229232 )
230233 } )
231234
232235 it ( `should${ ok ? '' : "n't" } list renamed` , done => {
233236 const chinook = getConnection ( )
234237 chinook . sendCommands (
235238 `LIST APIKEYS USER ${ username } ${ username == 'admin' ? '; LIST MY APIKEYS' : '' } ` ,
236- test ( done , chinook , ok , { creation_date : date ( ) , expiration_date : expiration , key : key , name : keyName } )
239+ test ( done , chinook , ok , { creation_date : date ( ) , expiration_date : expiration , key : generated_key , name : keyName } )
237240 )
238241 } )
239242
240243 it ( `should${ ok ? '' : "n't" } remove` , done => {
241244 const chinook = getConnection ( )
242- chinook . sendCommands ( `REMOVE APIKEY ${ key } ` , test ( done , chinook , ok ) )
245+ chinook . sendCommands ( `REMOVE APIKEY ${ generated_key } ` , test ( done , chinook , ok ) )
243246 } )
244247
245248 it ( `should${ ok ? '' : "n't" } list empty` , done => {
246249 const chinook = getConnection ( )
247250 chinook . sendCommands (
248251 `LIST APIKEYS USER ${ username } ${ username == 'admin' ? '; LIST MY APIKEYS' : '' } ` ,
249- test ( done , chinook , false , { creation_date : date ( ) , expiration_date : expiration , key : key , name : keyName } )
252+ test ( done , chinook , false , { creation_date : date ( ) , expiration_date : expiration , key : generated_key , name : keyName } )
250253 )
251254 } )
252255} )
@@ -347,14 +350,14 @@ describe.each([
347350} )
348351
349352describe . each ( [
350- [ randomName ( ) , randomName ( ) , 'READ' , 'chinook.sqlite' , 'artists' , true ] ,
351- [ randomName ( ) , randomName ( ) , '' , '' , '' , true ] ,
352- [ randomName ( ) , randomName ( ) , 'READ' , '' , '' , true ] ,
353- [ randomName ( ) , randomName ( ) , 'NOT_EXIST' , '' , '' , false ] ,
354- [ randomName ( ) , randomName ( ) , '' , 'chinook.sqlite' , 'artists' , true ]
353+ [ randomName ( ) , randomName ( ) , 'READ' , 'chinook.sqlite' , 'artists' , CHINOOK_API_KEY , true ] ,
354+ [ randomName ( ) , randomName ( ) , '' , '' , '' , CHINOOK_API_KEY , true ] ,
355+ [ randomName ( ) , randomName ( ) , 'READ' , '' , '' , CHINOOK_API_KEY , true ] ,
356+ [ randomName ( ) , randomName ( ) , 'NOT_EXIST' , '' , '' , randomName ( ) , false ] ,
357+ [ randomName ( ) , randomName ( ) , '' , 'chinook.sqlite' , 'artists' , CHINOOK_API_KEY , true ]
355358 //[randomName(), randomName(), 'READ', 'NOT_EXIST', '', false],
356359 //[randomName(), randomName(), 'READ', '', 'NOT_EXIST', false] core not checking if database or table exists
357- ] ) ( 'user' , ( username , password , role , database , table , ok ) => {
360+ ] ) ( 'user' , ( username , password , role , database , table , key , ok ) => {
358361 it ( `should${ ok ? '' : "n't" } create` , done => {
359362 const chinook = getConnection ( )
360363 chinook . sendCommands (
@@ -384,6 +387,16 @@ describe.each([
384387 chinook . sendCommands ( `AUTH USER ${ username } PASSWORD ${ password } ` , test ( done , chinook , ok ) )
385388 } )
386389
390+ it ( `should${ ok ? '' : "n't" } auth with apikey` , done => {
391+ const chinook = getConnection ( )
392+ chinook . sendCommands ( `AUTH APIKEY ${ key } ` , test ( done , chinook , ok ) )
393+ } )
394+
395+ it ( `should${ ok ? '' : "n't" } auth with hash` , done => {
396+ const chinook = getConnection ( )
397+ chinook . sendCommands ( `AUTH USER ${ username } HASH ${ createHash ( 'sha256' ) . update ( password ) . digest ( 'base64' ) } ` , test ( done , chinook , ok ) )
398+ } )
399+
387400 it ( `should${ ok ? '' : "n't" } revoke role` , done => {
388401 const gOk = role != '' && ok
389402 const chinook = getConnection ( )
@@ -603,14 +616,44 @@ describe.each([
603616 chinook . sendCommands ( `CREATE CHANNEL ${ name } IF NOT EXISTS` , test ( done , chinook , ok ) )
604617 } )
605618
606- /* it(`should${ok ? '' : "n't"} listen`, done => { //ERROR Data type: | is not defined in SCSP, it isn't supported yet
619+ it ( `should${ ok ? '' : "n't" } listen` , done => {
620+ //ERROR Data type: | is not defined in SCSP, it isn't supported yet
607621 const chinook = getConnection ( )
608- chinook.sendCommands(`LISTEN ${name}`, test(done, chinook, ok))
622+ chinook . sendCommands (
623+ `LISTEN ${ name } ` ,
624+ test (
625+ done ,
626+ chinook ,
627+ ok ,
628+ / ^ P A U T H \s ( [ a - f 0 - 9 ] { 8 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 12 } ) \s ( [ a - f 0 - 9 ] { 8 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 12 } ) $ /
629+ )
630+ )
609631 } )
610632 it ( `should${ ok ? '' : "n't" } listen table` , done => {
611633 const chinook = getConnection ( )
612- chinook.sendCommands(`LISTEN TABLE ${table} ${database ? `DATABASE ${database}` : ''}`, test(done, chinook, ok))
613- }) */
634+ chinook . sendCommands (
635+ `LISTEN TABLE ${ table } ${ database ? `DATABASE ${ database } ` : '' } ` ,
636+ test (
637+ done ,
638+ chinook ,
639+ ok ,
640+ / ^ P A U T H \s ( [ a - f 0 - 9 ] { 8 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 12 } ) \s ( [ a - f 0 - 9 ] { 8 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 4 } - [ a - f 0 - 9 ] { 12 } ) $ /
641+ )
642+ )
643+ } )
644+
645+ it . skip ( `should${ ok ? '' : "n't" } list pubsub connections` , done => {
646+ const chinook = getConnection ( )
647+ chinook . sendCommands (
648+ `LIST PUBSUB CONNECTIONS` ,
649+ test ( done , chinook , ok , {
650+ id : expect . any ( Number ) ,
651+ dbname : database ,
652+ chname : table ,
653+ client_uuid : uuid ( )
654+ } )
655+ )
656+ } )
614657
615658 it ( `should${ ok ? '' : "n't" } notify` , done => {
616659 const chinook = getConnection ( )
0 commit comments