-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.test.ts
More file actions
4057 lines (3747 loc) · 142 KB
/
core.test.ts
File metadata and controls
4057 lines (3747 loc) · 142 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* core.test.ts - test sqlitecloud commands
*/
import fs from 'fs'
import path from 'path'
import { createHash } from 'crypto'
import { SQLiteCloudTlsConnection } from '../src/drivers/connection-tls'
import { SQLiteCloudError, SQLiteCloudRowset } from '../src/index'
import { SQLiteCloudConnection } from '../src/drivers/connection'
import { CHINOOK_DATABASE_URL, CHINOOK_API_KEY } from './shared'
import { parseconnectionstring } from '../src/drivers/utilities'
jest.retryTimes(3)
// #region tests utilities
const _ = undefined // to use undefined as empty argument
function getConnection() {
return new SQLiteCloudTlsConnection({ connectionstring: CHINOOK_DATABASE_URL }, error => {
if (error) {
console.error(`getChinookTlsConnection - returned error: ${error}`)
}
expect(error).toBeNull()
})
}
const connUsername = parseconnectionstring(CHINOOK_DATABASE_URL).username
const randomName = (length: number = 7): string =>
Array(length + 1)
.join((Math.random().toString(36) + '00000000000000000').slice(2, 18))
.slice(0, length)
const randomDate = (fromTime = new Date(new Date().getTime() + 4 * 60 * 60 * 1000).getTime()) =>
new Date(fromTime + Math.random())
.toISOString()
.replace('T', ' ')
.replace(/\.\d{3}Z/, '')
const randomBool = (): boolean => Math.random() < 0.5
const date = () => expect.stringMatching(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/)
const ip = () => expect.stringMatching(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
const uuid = () => expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
const bool = () => expect.any(Number)
const colseq = () => expect.stringMatching(/^(BINARY|RTRIM|NOCASE)$/)
const screaming_snake_case = () => expect.stringMatching(/^[A-Z]+[_]*[A-Z]*$/)
const regex_IP_UUID_N = /(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)|(^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$)|[0-9]/
const test = (done: jest.DoneCallback, chinook: SQLiteCloudConnection, ok: boolean, expectedResult: any = 'OK', callback?: Function) => {
return (error: SQLiteCloudError | Error | null, results: any) => {
try {
if (ok) {
expect(error).toBeNull()
if (results && results.constructor) {
switch (results.constructor) {
case Array:
if (typeof expectedResult === 'number') {
expect(results).toHaveLength(expectedResult)
} else {
expectedResult.forEach((expRes: any) => expect(results).toContainEqual(expRes))
}
break
case String:
expect(results).toMatch(expectedResult)
break
case Object:
case Number:
case Buffer:
expect(results).toEqual(expectedResult)
break
case SQLiteCloudRowset:
if (expectedResult instanceof Array) {
expect(results).toEqual(expectedResult)
} else {
expect(results).toContainEqual(expectedResult)
}
break
default:
expect(results).toBe(expectedResult)
}
} else {
if (expectedResult && expectedResult.source && expectedResult.source.includes('null')) {
expect(results).toBeNull()
} else {
expect(results).toBe(expectedResult)
}
}
} else {
try {
expect(results).not.toContainEqual(expectedResult)
} catch {
try {
expect(results).toEqual([])
} catch {
try {
expect(error).toBeInstanceOf(SQLiteCloudError)
expect((error as SQLiteCloudError).message).toMatch(
/(not found|doesn\'t exist|does not exist|invalid|unable|fail|cannot|must be unique|unknown|undefined|error|no such|not available|try again later|wrong|has no|is read-only|ended the connection|was already deallocted|already exists)/i
)
expect(results).toBeUndefined()
} catch {
expect(results).toBeFalsy()
expect(error).toBeFalsy()
}
}
}
}
if (callback) callback(results, error)
done()
} catch (error) {
done(error)
} finally {
chinook.close()
}
}
}
// #endregion
describe.each([
['', true],
['COMPRESSED', true],
['NOT_EXISTS', false]
])('tests', (compressed, ok) => {
//chiedere ad andrea compressed??
it(`should${ok ? '' : "n't"} test string`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST STRING ${compressed}`, test(done, chinook, ok, 'Hello World, this is a test string.'))
})
it(`should${ok ? '' : "n't"} test string0`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST STRING0 ${compressed}`, test(done, chinook, ok, ''))
})
it(`should${ok ? '' : "n't"} test zero string`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST ZERO_STRING ${compressed}`, test(done, chinook, ok, 'Hello World, this is a zero-terminated test string.'))
})
it(`should${ok ? '' : "n't"} test rowset`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST ROWSET ${compressed}`, test(done, chinook, ok, { key: expect.any(String), value: expect.any(String) }))
})
it(`should${ok ? '' : "n't"} test rowset chunk`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST ROWSET_CHUNK ${compressed}`, test(done, chinook, ok, { key: expect.any(String) }))
})
it(`should test error`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST ERROR ${compressed}`, test(done, chinook, false))
})
it(`should test exterror`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST EXTERROR ${compressed}`, test(done, chinook, false))
})
it(`should${ok ? '' : "n't"} test array`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST ARRAY ${compressed}`, test(done, chinook, ok, ['Hello World', 123456, 3.1415, null, expect.any(Buffer)]))
})
it(`should${ok ? '' : "n't"} test array0`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST ARRAY0 ${compressed}`, test(done, chinook, ok, 0))
})
it(`should${ok ? '' : "n't"} test integer`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST INTEGER ${compressed}`, test(done, chinook, ok, 123456))
})
it(`should${ok ? '' : "n't"} test integer`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST FLOAT ${compressed}`, test(done, chinook, ok, 3.1415926))
})
it(`should${ok ? '' : "n't"} test blob`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST BLOB ${compressed}`, test(done, chinook, ok, expect.any(Buffer)))
})
it(`should${ok ? '' : "n't"} test blob0`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST BLOB0 ${compressed}`, test(done, chinook, ok, expect.any(Buffer)))
})
it(`should${ok ? '' : "n't"} test json`, done => {
const chinook = getConnection()
chinook.sendCommands(
`TEST JSON ${compressed}`,
test(done, chinook, ok, {
'msg-from': { class: 'soldier', name: 'Wixilav' },
'msg-log': [
'soldier: Boss there is a slight problem with the piece offering to humans',
'supreme-commander: Explain yourself soldier!',
"soldier: Well they don't seem to move anymore...",
'supreme-commander: Oh snap, I came here to see them twerk!'
],
'msg-to': { class: 'supreme-commander', name: '[Redacted]' },
'msg-type': ['0xdeadbeef', 'irc log']
})
)
})
it(`should${ok ? '' : "n't"} test null`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST NULL ${compressed}`, test(done, chinook, ok, null))
})
it(`should${ok ? '' : "n't"} test command`, done => {
const chinook = getConnection()
chinook.sendCommands(`TEST COMMAND ${compressed}`, test(done, chinook, ok, 'PING'))
})
})
describe.each([
['192.168.1.1', 'ROLE', 'READ', true],
['192.168.1.1', 'ROLE', 'NOT_EXIST', false],
['192.168.1.1', 'USER', 'READ', false],
['', 'ROLE', 'READ', false]
//['NOT_EXIST', 'ROLE', 'READ', false] is it right that takes invalid address as valid??
])('allowed ip', (address, type, name, ok) => {
it(`should${ok ? '' : "n't"} add`, done => {
const chinook = getConnection()
//sqlOk(`ADD ALLOWED IP ${address} ${type} ${name}`, chinook, done, ok) prova con .sql
chinook.sendCommands(`ADD ALLOWED IP ${address} ${type} ${name}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} list added`, done => {
const chinook = getConnection()
chinook.sendCommands(`LIST ALLOWED IP ${type} ${name}`, test(done, chinook, ok, { address: address, name: name, type: type.toLowerCase() }))
})
it(`should${ok ? '' : "n't"} remove`, done => {
const chinook = getConnection()
chinook.sendCommands(`REMOVE ALLOWED IP ${address} ${type} ${name}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} list empty`, done => {
const chinook = getConnection()
chinook.sendCommands(`LIST ALLOWED IP ${type} ${name}`, test(done, chinook, false, { address: address, name: name, type: type.toLowerCase() }))
})
})
describe.each([
[randomName(), 'READ', 'chinook', 'artists', true],
[randomName(), 'NOT_EXIST', 'chinook', 'artists', false],
['', 'READ', 'chinook', 'artists', false]
//[randomName(), 'READ', 'NOT_EXIST', 'artists', false] // doesn't check if database exists
//[randomName(), 'READ', 'chinook', 'NOT_EXIST', false] // doesn't check if table exists
])('role', (role, privilege, database, table, ok) => {
it(`should${ok ? '' : "n't"} create`, done => {
const chinook = getConnection()
chinook.sendCommands(`CREATE ROLE ${role} PRIVILEGE ${privilege} DATABASE ${database} TABLE ${table}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} list created`, done => {
const chinook = getConnection()
chinook.sendCommands(`LIST ROLES`, test(done, chinook, ok, { rolename: role, builtin: 0, privileges: privilege, databasename: database, tablename: table }))
})
it(`should${ok ? '' : "n't"} rename`, done => {
const prevRole = role
role = randomName()
const chinook = getConnection()
chinook.sendCommands(`RENAME ROLE ${prevRole} TO ${role}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} list renamed`, done => {
const chinook = getConnection()
chinook.sendCommands(`LIST ROLES`, test(done, chinook, ok, { rolename: role, builtin: 0, privileges: privilege, databasename: database, tablename: table }))
})
it(`should${ok ? '' : "n't"} remove`, done => {
const chinook = getConnection()
chinook.sendCommands(`REMOVE ROLE ${role}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} list empty`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST ROLES`,
test(done, chinook, false, { rolename: role, builtin: 0, privileges: privilege, databasename: database, tablename: table })
)
})
})
describe.each([
['admin', randomName(), randomDate(), _, true],
['admin', randomName(), randomDate(), randomName(), true],
['admin', randomName(), 'WRONG_DATE', _, false],
['NOT_EXIST', randomName(), randomDate(), randomName(), false],
['admin', '', randomDate(), _, false]
])('api key', (username, keyName, expiration, key, ok) => {
let generated_key: string
it(`should${ok ? '' : "n't"} create`, done => {
const chinook = getConnection()
chinook.sendCommands(
`CREATE APIKEY USER ${username} NAME ${keyName} EXPIRATION "${expiration}"${key ? ` KEY ${key}` : ''}`,
test(done, chinook, ok, key ? key : /^[a-zA-Z0-9]{43}$/, (res: string) => (generated_key = res))
)
})
it(`should${ok ? '' : "n't"} switch`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH APIKEY ${generated_key}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} list created`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST APIKEYS USER ${username} ${username == 'admin' ? '; LIST MY APIKEYS' : ''}`,
test(done, chinook, ok, { creation_date: date(), expiration_date: expiration, key: generated_key, name: keyName })
)
})
it(`should${ok ? '' : "n't"} rename`, done => {
const prevKeyName = keyName
keyName = randomName()
const prevExpiration = expiration
expiration = randomDate()
const chinook = getConnection()
chinook.sendCommands(
`SET APIKEY ${generated_key} NAME ${keyName} EXPIRATION "${expiration}"; LIST APIKEYS USER ${username}`,
test(done, chinook, false, { creation_date: date(), expiration_date: prevExpiration, key: generated_key, name: prevKeyName })
)
})
it(`should${ok ? '' : "n't"} list renamed`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST APIKEYS USER ${username} ${username == 'admin' ? '; LIST MY APIKEYS' : ''}`,
test(done, chinook, ok, { creation_date: date(), expiration_date: expiration, key: generated_key, name: keyName })
)
})
it(`should${ok ? '' : "n't"} remove`, done => {
const chinook = getConnection()
chinook.sendCommands(`REMOVE APIKEY ${generated_key}`, test(done, chinook, ok))
})
it(`shouldn't switch`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH APIKEY ${generated_key}`, test(done, chinook, false))
})
it(`should${ok ? '' : "n't"} list empty`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST APIKEYS USER ${username} ${username == 'admin' ? '; LIST MY APIKEYS' : ''}`,
test(done, chinook, false, { creation_date: date(), expiration_date: expiration, key: generated_key, name: keyName })
)
})
})
describe.each([
[parseconnectionstring(CHINOOK_DATABASE_URL).username, parseconnectionstring(CHINOOK_DATABASE_URL).password, true],
[parseconnectionstring(CHINOOK_DATABASE_URL).username, randomName(), false],
[randomName(), parseconnectionstring(CHINOOK_DATABASE_URL).password, false],
[randomName(), randomName(), false],
[randomName(), '', false],
['', randomName(), false],
['', '', false]
])('auth user', (username, password, ok) => {
it(`should${ok ? '' : "n't"} auth`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, ok))
})
})
describe.each([
[_, _, _, _, randomBool(), _, _, _, true],
//[_, _, _, _, randomBool(), _, _, 'NOT_EXIST', false]//not checking node??
['NOT_EXIST', _, _, _, randomBool(), _, _, _, false],
//[_, 'NOT_EXIST', _, _, randomBool(), _, _, _, false], //can't do just to date or the date check is only on FROM?
['NOT_EXIST', 'NOT_EXIST', _, _, randomBool(), _, _, _, false],
[_, _, 9, _, randomBool(), _, _, _, false], //log level 9 doesn't exist
//[_, _, 2, _, randomBool(), _, _, _, true], //leaving it commented to avoid it failing on ci/cd
[_, _, _, 9, randomBool(), _, _, _, false], //log type 9 doesn't exist
//[_, _, _, 5, randomBool(), _, _, _, true], //leaving it commented to avoid it failing on ci/cd
[_, _, _, _, randomBool(), 0, _, _, false], //should fail because limit is 0
[_, _, _, _, randomBool(), 5, _, _, true],
//[_, _, _, _, randomBool(), 5, 10, _, true] can fail on ci/cd
[_, _, _, _, randomBool(), 5, -1, _, false],
[randomDate(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).getTime()), _, _, _, randomBool(), Number.MAX_VALUE, Number.MAX_VALUE, _, false],
[_, randomDate(), _, _, randomBool(), _, _, 999, false], // is it possible to have so many nodes? :)
[randomDate(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).getTime()), randomDate(), _, _, randomBool(), _, _, 1, true]
])('logs', (from, to, level, type, id, limit, cursor, node, ok) => {
it(`should${ok ? '' : "n't"} list log`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST LOG${from ? ` FROM "${from}"` : ''}${to ? ` TO "${to}"` : ''}${level ? ` LEVEL ${level}` : ''}${type ? ` TYPE ${type}` : ''}${id ? ' ID' : ''}${limit != _ ? ` LIMIT ${limit}` : ''}${cursor ? ` CURSOR ${cursor}` : ''}${node ? ` NODE ${node}` : ''}`,
test(done, chinook, ok, {
datetime: date(),
log_type: expect.any(Number),
log_level: expect.any(Number),
description: expect.any(String),
id: id ? expect.any(Number) : undefined,
username: parseconnectionstring(CHINOOK_DATABASE_URL).username,
database: parseconnectionstring(CHINOOK_DATABASE_URL).database,
ip_address: ip(),
connection_id: expect.any(Number)
})
)
})
})
describe.each([
[_, true],
[true, true]
])('cluster', (id, ok) => {
let leader: number
it(`should${ok ? '' : "n't"} get leader`, done => {
const chinook = getConnection()
chinook.sendCommands(
`GET LEADER ${id ? 'ID' : ''}`,
test(
done,
chinook,
ok,
id ? expect.any(Number) : parseconnectionstring(CHINOOK_DATABASE_URL).host + ':' + parseconnectionstring(CHINOOK_DATABASE_URL).port,
(res: any) => {
if (id) leader = res
}
)
)
})
it(`should${ok ? '' : "n't"} list nodes`, done => {
const chinook = getConnection()
chinook.sendCommands(
'LIST NODES',
test(done, chinook, ok, {
id: leader ?? expect.any(Number),
public_addr: leader ? parseconnectionstring(CHINOOK_DATABASE_URL).host : expect.any(String),
port: leader ? parseconnectionstring(CHINOOK_DATABASE_URL).port : expect.any(Number),
cluster_port: leader ? 9860 : expect.any(Number),
status: leader ? 'Leader' : expect.any(String),
progress: expect.any(String),
match: expect.any(Number),
last_activity: date()
})
)
})
it(`shouldn't transfer leadership`, done => {
const chinook = getConnection()
chinook.sendCommands(`TRANSFER LEADERSHIP TO NODE ${leader ? leader + 9999999 : ''}`, test(done, chinook, false))
})
})
describe.each([
[randomName(), randomName(), 'READ', 'chinook.sqlite', 'artists', CHINOOK_API_KEY, true],
[randomName(), randomName(), '', '', '', CHINOOK_API_KEY, true],
[randomName(), randomName(), 'READ', '', '', CHINOOK_API_KEY, true],
[randomName(), randomName(), 'NOT_EXIST', '', '', randomName(), false],
[randomName(), randomName(), '', 'chinook.sqlite', 'artists', CHINOOK_API_KEY, true]
//[randomName(), randomName(), 'READ', 'NOT_EXIST', '', false],
//[randomName(), randomName(), 'READ', '', 'NOT_EXIST', false] core not checking if database or table exists
])('user', (username, password, role, database, table, key, ok) => {
it(`should${ok ? '' : "n't"} create`, done => {
const chinook = getConnection()
chinook.sendCommands(
`CREATE USER ${username} PASSWORD ${password}${role ? ` ROLE ${role}` : ''}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; ${role ? `LIST USERS WITH ROLES${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}` : 'LIST USERS'}`,
test(
done,
chinook,
ok,
role != ''
? {
username: username,
enabled: 1,
roles: role ? role : null,
databasename: database ? database : null,
tablename: table ? table : null
}
: {
username: username,
enabled: 1
}
)
)
})
it(`should${ok ? '' : "n't"} auth`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} verify`, done => {
const chinook = getConnection()
chinook.sendCommands(`VERIFY USER ${username} PASSWORD ${password}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} switch`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH USER ${username}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} set`, done => {
const chinook = getConnection()
chinook.sendCommands(`SET USER ${username}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} auth with apikey`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH APIKEY ${key}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} switch apikey`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH APIKEY ${key}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} auth with hash`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} HASH ${createHash('sha256').update(password).digest('base64')}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} revoke role`, done => {
const gOk = role != '' && ok
const chinook = getConnection()
chinook.sendCommands(
`REVOKE ROLE ${role} USER ${username}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; ${role ? 'LIST USERS WITH ROLES' : 'LIST USERS'}`,
test(
done,
chinook,
gOk,
role != ''
? {
username: username,
enabled: 1,
roles: role ? null : expect.any(String),
databasename: null,
tablename: null
}
: {
username: username,
enabled: 1
}
)
)
})
it(`should${ok ? '' : "n't"} grant role`, done => {
const gOk = role != '' && ok
const chinook = getConnection()
chinook.sendCommands(
`GRANT ROLE ${role} USER ${username}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; ${role ? `LIST USERS WITH ROLES${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}` : 'LIST USERS'}`,
test(
done,
chinook,
gOk,
role != ''
? {
username: username,
enabled: 1,
roles: role ? role : null,
databasename: database ? database : null,
tablename: table ? table : null
}
: {
username: username,
enabled: 1
}
)
)
})
it(`should${ok ? '' : "n't"} disable`, done => {
const chinook = getConnection()
chinook.sendCommands(
`DISABLE USER ${username}; ${role ? `LIST USERS WITH ROLES${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}` : 'LIST USERS'}`,
test(
done,
chinook,
ok,
role != ''
? {
username: username,
enabled: 0,
roles: role ? role : null,
databasename: database ? database : null,
tablename: table ? table : null
}
: {
username: username,
enabled: 0
}
)
)
})
it(`shouldn't auth`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, false))
})
it(`shouldn't verify`, done => {
const chinook = getConnection()
chinook.sendCommands(`VERIFY USER ${username} PASSWORD ${password}`, test(done, chinook, false))
})
it(`shouldn't switch`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH USER ${username}`, test(done, chinook, false))
})
it(`shouldn't set`, done => {
const chinook = getConnection()
chinook.sendCommands(`SET USER ${username}`, test(done, chinook, false))
})
it(`should${ok ? '' : "n't"} enable`, done => {
const chinook = getConnection()
chinook.sendCommands(
`ENABLE USER ${username}; ${role ? `LIST USERS WITH ROLES${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}` : 'LIST USERS'}`,
test(
done,
chinook,
ok,
role != ''
? {
username: username,
enabled: 1,
roles: role ? role : null,
databasename: database ? database : null,
tablename: table ? table : null
}
: {
username: username,
enabled: 1
}
)
)
})
it(`should${ok ? '' : "n't"} auth`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} add allowed ip to verify`, done => {
const chinook = getConnection()
chinook.sendCommands(`ADD ALLOWED IP 1.1.1.1 USER ${username}`, test(done, chinook, ok))
})
it(`shouldn't verify`, done => {
const chinook = getConnection()
chinook.sendCommands(`VERIFY USER ${username} PASSWORD ${password} IP 1.1.1.12`, test(done, chinook, false))
})
it(`should${ok ? '' : "n't"} verify`, done => {
const chinook = getConnection()
chinook.sendCommands(`VERIFY USER ${username} PASSWORD ${password} IP 1.1.1.1`, test(done, chinook, ok))
})
it(`shouldn't auth`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, false))
})
it(`should${ok ? '' : "n't"} remove allowed ip to verify`, done => {
const chinook = getConnection()
chinook.sendCommands(`REMOVE ALLOWED IP 1.1.1.1 USER ${username}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} switch`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH USER ${username}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} set`, done => {
const chinook = getConnection()
chinook.sendCommands(`SET USER ${username}`, test(done, chinook, ok))
})
it(`should get user`, done => {
const chinook = getConnection()
chinook.sendCommands(`GET USER`, test(done, chinook, true, parseconnectionstring(CHINOOK_DATABASE_URL).username))
})
it(`should${ok ? '' : "n't"} rename`, done => {
const chinook = getConnection()
const oldUsername = username
username = randomName()
chinook.sendCommands(
`RENAME USER ${oldUsername} TO ${username}; ${role ? `LIST USERS WITH ROLES${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}` : 'LIST USERS'}`,
test(
done,
chinook,
ok,
role != ''
? {
username: username,
enabled: 1,
roles: role ? role : null,
databasename: database ? database : null,
tablename: table ? table : null
}
: {
username: username,
enabled: 1
}
)
)
})
it(`should${ok ? '' : "n't"} set user password`, done => {
const chinook = getConnection()
password = randomName()
chinook.sendCommands(`SET PASSWORD ${password} USER ${username}; AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} remove`, done => {
const chinook = getConnection()
chinook.sendCommands(
`REMOVE USER ${username}; ${role ? `LIST USERS WITH ROLES${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}` : 'LIST USERS'}`,
test(
done,
chinook,
false,
role != ''
? {
username: username,
enabled: 1,
roles: role ? role : null,
databasename: database ? database : null,
tablename: table ? table : null
}
: {
username: username,
enabled: 1
}
)
)
})
it(`shouldn't auth`, done => {
const chinook = getConnection()
chinook.sendCommands(`AUTH USER ${username} PASSWORD ${password}`, test(done, chinook, false))
})
it(`shouldn't switch`, done => {
const chinook = getConnection()
chinook.sendCommands(`SWITCH USER ${username}`, test(done, chinook, false))
})
it(`shouldn't set`, done => {
const chinook = getConnection()
chinook.sendCommands(`SET USER ${username}`, test(done, chinook, false))
})
it(`should set my password`, done => {
let chinook = getConnection()
const myPassword = randomName()
chinook.sendCommands(`SET MY PASSWORD adminpasswordxxx`, (error: Error | null, results: any) => {
try {
expect(error).toBeNull()
expect(results).toBe('OK')
chinook.close()
//with old pass it should fail
chinook = new SQLiteCloudTlsConnection({ connectionstring: CHINOOK_DATABASE_URL }, (error: any) => {
let cerr = ''
if (error) {
console.error(`getChinookTlsConnection - returned error: ${error}`)
cerr = `getChinookTlsConnection - returned error: ${error}`
}
expect(error).toBeDefined()
expect(cerr).toMatch(/error/i)
})
//try with new pass
chinook = new SQLiteCloudTlsConnection(
{ connectionstring: CHINOOK_DATABASE_URL.replace(parseconnectionstring(CHINOOK_DATABASE_URL).password ?? 'defaultPassword', myPassword) },
(error: any) => {
if (error) {
console.error(`getChinookTlsConnection - returned error: ${error}`)
}
expect(error).toBeNull()
}
)
chinook.sendCommands(`SET MY PASSWORD ${parseconnectionstring(CHINOOK_DATABASE_URL).password}`, (error: Error | null, results: any) => {
expect(error).toBeNull()
expect(results).toBe('OK')
})
} catch (error) {
done(error)
} finally {
chinook.close()
done()
}
})
})
})
describe.each([
[randomName(), randomName(), 'artists', 'chinook.sqlite', true],
['', '', '', '', false]
])('pubsub', (name, message, table, database, ok) => {
it(`should${ok ? '' : "n't"} create`, done => {
const chinook = getConnection()
chinook.sendCommands(`CREATE CHANNEL ${name}; LIST CHANNELS`, test(done, chinook, ok, { chname: name }))
})
it(`should${ok ? '' : "n't"} create without errors`, done => {
const chinook = getConnection()
chinook.sendCommands(`CREATE CHANNEL ${name} IF NOT EXISTS`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} listen`, done => {
//ERROR Data type: | is not defined in SCSP, it isn't supported yet
const chinook = getConnection()
chinook.sendCommands(
`LISTEN ${name}`,
test(
done,
chinook,
ok,
/^PAUTH\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/
)
)
})
it(`should${ok ? '' : "n't"} listen table`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LISTEN TABLE ${table} ${database ? `DATABASE ${database}` : ''}`,
test(
done,
chinook,
ok,
/^PAUTH\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/
)
)
})
it(`should${ok ? '' : "n't"} list pubsub connections`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LISTEN TABLE ${table} ${database ? `DATABASE ${database}` : ''}`,
ok
? (error: any, results: any) => {
expect(error).toBeNull()
expect(results).toMatch(
/^PAUTH\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/
)
chinook.sendCommands(
`LIST PUBSUB CONNECTIONS`,
test(done, chinook, ok, {
id: expect.any(Number),
dbname: database,
chname: table,
client_uuid: uuid()
})
)
}
: test(done, chinook, false)
)
})
it(`should${ok ? '' : "n't"} notify`, done => {
const chinook = getConnection()
chinook.sendCommands(`NOTIFY ${name} "${message}"`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} unlisten`, done => {
const chinook = getConnection()
chinook.sendCommands(`UNLISTEN ${name}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} unlisten table`, done => {
const chinook = getConnection()
chinook.sendCommands(`UNLISTEN TABLE ${table} ${database ? `DATABASE ${database}` : ''}`, test(done, chinook, ok))
})
it(`should${ok ? '' : "n't"} remove`, done => {
const chinook = getConnection()
chinook.sendCommands(`REMOVE CHANNEL ${name}; LIST CHANNELS`, test(done, chinook, false, { chname: name }))
})
})
describe.each([
//don't use pub privilege, it's used by the tests
['READ', randomName(), 'chinook.sqlite', 'artists', true],
['READ', randomName(), '', '', true],
//['READ', randomName(), 'NOT_EXIST', 'NOT_EXIST', false], //no check on table or database name
['READ', 'READ', '', '', false],
['', '', '', '', false],
['', '', 'chinook.sqlite', 'artists', false],
['', 'READ', '', '', false],
['READ', '', '', '', false]
])('privilege', (privilege, role, database, table, ok) => {
it(`should${ok ? '' : "n't"} grant`, done => {
const chinook = getConnection()
chinook.sendCommands(
`CREATE ROLE ${role}; GRANT PRIVILEGE ${privilege} ROLE ${role}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; REMOVE ROLE ${role}`,
test(done, chinook, ok)
)
})
it(`should${ok ? '' : "n't"} list`, done => {
const chinook = getConnection()
chinook.sendCommands(`LIST PRIVILEGES`, test(done, chinook, ok, { name: role == privilege || role == '' ? '' : privilege }))
})
it(`should${ok ? '' : "n't"} revoke`, done => {
const chinook = getConnection()
chinook.sendCommands(
`CREATE ROLE ${role} PRIVILEGE ${privilege}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; REVOKE PRIVILEGE ${privilege} ROLE ${role}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; REMOVE ROLE ${role}`,
test(done, chinook, ok)
)
})
it(`should${ok ? '' : "n't"} set`, done => {
const chinook = getConnection()
chinook.sendCommands(
`CREATE ROLE ${role} PRIVILEGE PUB; SET PRIVILEGE ${privilege} ROLE ${role}${database ? ` DATABASE ${database}` : ''}${table ? ` TABLE ${table}` : ''}; LIST ROLES`,
test(done, chinook, ok, {
rolename: role,
builtin: 0,
privileges: privilege,
databasename: database ? database : null,
tablename: table ? table : null
})
)
})
})
describe.each([
[
100,
'os',
'MEMORY',
_,
'main',
'employees',
'City',
randomDate(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).getTime()),
randomDate(),
'DETAILED',
'PUBSUB',
true
],
['', '', '', ' ' /*NOT_EXIST waiting for the strtol fix*/, _, _, _, _, randomDate(), _, _, false],
[0, 'sqlitecloud_version', 'MEMORY', 1, '', 'albums', _, _, _, '', '', true],
[0, 'sqlitecloud_version', 'MEMORY', 1, '', '', _, randomDate(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).getTime()), randomDate(), '', '', true],
[0, _, _, 999, _, 'NOT_EXIST', 'NOT_EXIST', 'NOT_EXIST', 'NOT_EXIST', _, 'NOT_EXIST', false]
])('general', (sleep, key, memory, node, database, table, column, from, to, detailed, pubsub, ok) => {
it(`should ping`, done => {
const chinook = getConnection()
chinook.sendCommands('PING', test(done, chinook, true, 'PONG'))
})
it(`should${ok ? '' : "n't"} sleep`, done => {
const ms: number = new Date().getMilliseconds()
const chinook = getConnection()
chinook.sendCommands(
`SLEEP ${sleep}`,
test(done, chinook, ok, _, () => {
const newMs: number = new Date().getMilliseconds()
expect(Math.abs(newMs - ms)).toBeGreaterThanOrEqual(typeof sleep == 'number' ? sleep : 0)
})
)
})
it(`should${ok ? '' : "n't"} get info`, done => {
const chinook = getConnection()
chinook.sendCommands(`GET INFO ${key} ${node ? `NODE ${node}` : ''}`, test(done, chinook, ok, /.*/i))
})
it(`should${table && ok ? '' : "n't"} get sql`, done => {
const chinook = getConnection()
chinook.sendCommands(`GET SQL ${table}`, test(done, chinook, table ? ok : false, `CREATE TABLE "${table}"`))
})
it(`should${ok ? '' : "n't"} list commands`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST COMMANDS ${detailed}`,
test(done, chinook, ok, {
command: expect.any(String),
count: expect.any(Number),
avgtime: expect.any(Number),
privileges: detailed == 'DETAILED' ? expect.any(String) : undefined
})
)
})
it(`should${ok ? '' : "n't"} list connections`, done => {
const chinook = getConnection()
chinook.sendCommands(
`LIST CONNECTIONS ${node ? `NODE ${node}` : ''}`,
test(done, chinook, ok, {