-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathservice_test.go
1732 lines (1257 loc) · 45.6 KB
/
service_test.go
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
package main
import (
"encoding/json"
"fmt"
"github.com/alicebob/miniredis"
"github.com/xd-deng/rediseen/types"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
)
func Test_generateAddr(t *testing.T) {
var testService service
testService.loadConfigFromEnv()
if testService.bindAddress != "localhost:8000" {
t.Error("bindAddress is not created for default set-up correctly.")
}
os.Setenv("REDISEEN_HOST", "0.0.0.0")
testService.loadConfigFromEnv()
if testService.bindAddress != "0.0.0.0:8000" {
t.Error("bindAddress is not created for customized set-up correctly.")
}
os.Setenv("REDISEEN_PORT", "80")
testService.loadConfigFromEnv()
if testService.bindAddress != "0.0.0.0:80" {
t.Error("bindAddress is not created for customized set-up correctly.")
}
os.Unsetenv("REDISEEN_HOST")
os.Unsetenv("REDISEEN_PORT")
testService.loadConfigFromEnv()
if testService.bindAddress != "localhost:8000" {
t.Error("bindAddress is not created for default set-up correctly.")
}
}
func Test_configCheck_no_redis_uri(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "No valid Redis URI is provided") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_invalid_redis_uri(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "mysql://a:b@localhost:8888/db")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "is not valid") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_no_db_exposed(t *testing.T) {
originalDbExposed := os.Getenv("REDISEEN_DB_EXPOSED")
os.Setenv("REDISEEN_DB_EXPOSED", "")
defer os.Setenv("REDISEEN_DB_EXPOSED", originalDbExposed)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "REDISEEN_DB_EXPOSED is not configured") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_invalid_db_exposed_1(t *testing.T) {
originalDbExposed := os.Getenv("REDISEEN_DB_EXPOSED")
os.Setenv("REDISEEN_DB_EXPOSED", "-1")
defer os.Setenv("REDISEEN_DB_EXPOSED", originalDbExposed)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "REDISEEN_DB_EXPOSED provided can not be parsed properly") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_invalid_db_exposed_2(t *testing.T) {
originalDbExposed := os.Getenv("REDISEEN_DB_EXPOSED")
os.Setenv("REDISEEN_DB_EXPOSED", "1;-2;10")
defer os.Setenv("REDISEEN_DB_EXPOSED", originalDbExposed)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "REDISEEN_DB_EXPOSED provided can not be parsed properly") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_no_key_pattern_specified(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6379")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
originalKeyPatternAllowed := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalKeyPatternAllowed)
originalKeyPatternAllowAll := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL", "")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL", originalKeyPatternAllowAll)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "You have not specified any key pattern to allow being accessed") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
if !strings.Contains(err.Error(), "To allow ALL keys to be accessed,") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_conflicting_key_pattern_specified(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6379")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
originalKeyPatternAllowed := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "^key:[.]*")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalKeyPatternAllowed)
originalKeyPatternAllowAll := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL", "true")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL", originalKeyPatternAllowAll)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "You have specified both") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
if !strings.Contains(err.Error(), "which is conflicting.") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_all_key_exposed(t *testing.T) {
originalValue1 := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL", "true")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSE_ALL", originalValue1)
originalValue2 := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalValue2)
var testService service
err := testService.loadConfigFromEnv()
if err != nil {
t.Error("Not expecting error but got error")
}
}
func Test_configCheck_bad_regex(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6379")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
originalKeyPatternAllowed := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "^key:[.*")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalKeyPatternAllowed)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "REDISEEN_KEY_PATTERN_EXPOSED can not be compiled as regular expression") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_configCheck_good_config_without_auth_config(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6379")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
originalKeyPatternAllowed := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "^key:[.]*")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalKeyPatternAllowed)
var testService service
err := testService.loadConfigFromEnv()
if err != nil {
t.Error("Not expecting error but got error")
}
}
func Test_configCheck_good_config_with_auth_config(t *testing.T) {
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6379")
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
os.Setenv("REDISEEN_API_KEY", "RandomKey")
defer os.Unsetenv("REDISEEN_API_KEY")
originalKeyPatternAllowed := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "^key:[.]*")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalKeyPatternAllowed)
var testService service
err := testService.loadConfigFromEnv()
if err != nil {
t.Error("Not expecting error but got error")
}
}
func Test_configCheck_connection_failure(t *testing.T) {
originalTestMode := os.Getenv("REDISEEN_TEST_MODE")
os.Setenv("REDISEEN_TEST_MODE", "")
defer os.Setenv("REDISEEN_TEST_MODE", originalTestMode)
var testService service
err := testService.loadConfigFromEnv()
if err == nil {
t.Error("Expecting error but got nil")
}
if !strings.Contains(err.Error(), "Initial talking to Redis failed.") {
t.Error(fmt.Sprintf("Error contents `%s` is not what's expected", err.Error()))
}
}
func Test_validateDbExposeConfig_valid_cases(t *testing.T) {
var err error
testStrings := []string{"*", "8", "1-10", "1;3;8", "1;3-7;18", "1;3-7;10-100"}
for _, s := range testStrings {
err = validateDbExposeConfig(s)
if err != nil {
t.Error(fmt.Sprintf("checkDbExpose() failed for '%s'", s))
}
}
}
func Test_validateDbExposeConfig_invalid_cases(t *testing.T) {
var err error
testStrings := []string{"-1;18", "a;18", "1,18", ";1;18", "1;18;", "1;-5", "1;-5;10"}
for _, s := range testStrings {
err = validateDbExposeConfig(s)
if err == nil {
t.Error(fmt.Sprintf("checkDbExpose() passed WRONGLY for '%s'", s))
}
}
}
func Test_parseDbExposed_1(t *testing.T) {
result := parseDbExposed("1;3;9;100")
for _, i := range []int{1, 3, 9, 100} {
_, ok := result[i]
if !ok {
t.Error("parsing wrongly")
}
}
for _, i := range []int{2, 8, 99, 101} {
_, ok := result[i]
if ok {
t.Error("parsing wrongly")
}
}
}
func Test_parseDbExposed_2(t *testing.T) {
result := parseDbExposed("1;3-9;12-15;100")
for _, i := range []int{1, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 100} {
_, ok := result[i]
if !ok {
t.Error("parsing wrongly")
}
}
for _, i := range []int{2, 10, 16, 99, 101} {
_, ok := result[i]
if ok {
t.Error("parsing wrongly")
}
}
}
func Test_parseDbExposed_3(t *testing.T) {
result := parseDbExposed("0")
_, ok := result[0]
if !ok {
t.Error("parsing wrongly")
}
for i := 1; i <= 100; i++ {
_, ok := result[i]
if ok {
t.Error("parsing wrongly")
}
}
}
func Test_dbCheck(t *testing.T) {
// Test Environment Variable: REDISEEN_DB_EXPOSED=0-5
var testService service
testService.loadConfigFromEnv()
for i := 0; i <= 5; i++ {
if testService.dbCheck(i) == false {
t.Error("something is wrong with dbCheck()")
}
}
for _, i := range []int{6, 10, 8, 16, 99, 101} {
if testService.dbCheck(i) == true {
t.Error("something is wrong with dbCheck()")
}
}
}
func Test_dbCheck_expose_all(t *testing.T) {
originalDbExposed := os.Getenv("REDISEEN_DB_EXPOSED")
os.Setenv("REDISEEN_DB_EXPOSED", "*")
defer os.Setenv("REDISEEN_DB_EXPOSED", originalDbExposed)
var testService service
testService.loadConfigFromEnv()
for i := 0; i <= 100; i++ {
if testService.dbCheck(i) == false {
t.Error("something is wrong with dbCheck()")
}
}
}
func compareAndShout(t *testing.T, expected interface{}, actual interface{}) {
if expected != actual {
t.Error("Expecting\n", expected, "\ngot\n", actual)
}
}
func Test_service_wrong_usage(t *testing.T) {
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
expectedCode := 400
expectedError := "Usage: /info, /info/<info_section>, /metrics, /<db>, /<db>/<key>, /<db>/<key>/<index>, or /<db>/<key>/<field>"
casesToTest := []string{"/0/", "/0/key:1/", "/0/key:1/1/", "/0/key:1/1/1", "/0/key:1/1/1/", "/0/key:1/1/1/1"}
var res *http.Response
for _, suffix := range casesToTest {
res, _ = http.Get(s.URL + suffix)
if res.StatusCode != expectedCode {
t.Error("Expecting\n", expectedCode, "\ngot\n", res.StatusCode)
}
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
compareAndShout(t, expectedError, result.Error)
}
}
func Test_service_non_integer_db_provided(t *testing.T) {
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/a/key")
expectedCode := 400
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
expectedError := "Provide an integer for DB"
compareAndShout(t, expectedError, result.Error)
}
func Test_service_redis_conn_refused(t *testing.T) {
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/key:1")
expectedCode := 500
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
expectedError := "connection refused"
if !strings.Contains(result.Error, expectedError) {
t.Error("Expecting to contain \n", expectedError, "\ngot\n", result.Error)
}
}
func Test_service_non_existent_key(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/key:1")
expectedCode := 404
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
expectedError := "Key provided does not exist."
compareAndShout(t, expectedError, result.Error)
}
func Test_service_validate_case_sensitive_1(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("KEY:1", "fake content")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/key:1")
expectedCode := 404
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
expectedError := "Key provided does not exist."
compareAndShout(t, expectedError, result.Error)
}
func Test_service_validate_case_sensitive_2(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key", "fake content")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
originalKeyPatternExposed := os.Getenv("REDISEEN_KEY_PATTERN_EXPOSED")
os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", "KEY")
defer os.Setenv("REDISEEN_KEY_PATTERN_EXPOSED", originalKeyPatternExposed)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/KEY")
expectedCode := 404
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
expectedError := "Key provided does not exist."
compareAndShout(t, expectedError, result.Error)
}
// Check listing-keys feature
// Taking keys which are NOT exposed into consideration as well (they should NOT be counted or returned)
func Test_service_list_keys_by_db_1(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
testSlice1 := []string{"hi", "world", "test", "done"}
for i, v := range testSlice1 {
mr.Set(fmt.Sprintf("key:%v", i), v)
}
testSlice2 := []string{"r", "a", "n", "d", "o", "m"}
for i, v := range testSlice2 {
mr.HSet(fmt.Sprintf("key:%v", i+len(testSlice1)), v, v)
}
testSlice3 := []string{"list", "type"}
for i, v := range testSlice3 {
mr.Lpush(fmt.Sprintf("key:%v", i+len(testSlice1)+len(testSlice2)), v)
}
testSlice4 := []string{"no", "access", "key"}
for i, v := range testSlice4 {
mr.HSet(fmt.Sprintf("no_access_key:%v", i+len(testSlice1)+len(testSlice2)+len(testSlice3)), v, v)
}
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.KeyListType
json.Unmarshal(resultStr, &result)
compareAndShout(t, len(testSlice1)+len(testSlice2)+len(testSlice3), len(result.Keys))
compareAndShout(t, len(testSlice1)+len(testSlice2)+len(testSlice3), result.Count)
if result.Total > 1000 {
t.Error("Listing keys function should return <= 1000 keys")
}
}
// Check listing-keys feature
// Check the situation where more than 1000 keys are exposed
// `Total` should be the total number, BUT only up to 1000 keys should be returned.
// `Count` should be up to 1000 as well
func Test_service_list_keys_by_db_2(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
n := 2000
for i := 0; i < 2000; i++ {
mr.Set(fmt.Sprintf("key:%v", i), string(rune(i)))
}
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.KeyListType
json.Unmarshal(resultStr, &result)
compareAndShout(t, 1000, len(result.Keys))
for i := 0; i < 1000; i++ {
compareAndShout(t, "string", result.Keys[i].Type)
}
compareAndShout(t, 1000, result.Count)
compareAndShout(t, n, result.Total)
}
func Test_service_list_keys_by_db_key_type_list(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
n := 2000
for i := 0; i < n; i++ {
mr.Lpush(fmt.Sprintf("key:%v", i), string(rune(i)))
}
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.KeyListType
json.Unmarshal(resultStr, &result)
compareAndShout(t, 1000, len(result.Keys))
for i := 0; i < 1000; i++ {
compareAndShout(t, "list", result.Keys[i].Type)
}
compareAndShout(t, 1000, result.Count)
compareAndShout(t, n, result.Total)
}
func Test_service_list_keys_by_db_key_type_hash(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
n := 500
for i := 0; i < n; i++ {
mr.HSet(fmt.Sprintf("key:%v", i), string(rune(i)), string(rune(i)))
}
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.KeyListType
json.Unmarshal(resultStr, &result)
compareAndShout(t, n, len(result.Keys))
for i := 0; i < n; i++ {
compareAndShout(t, "hash", result.Keys[i].Type)
}
compareAndShout(t, n, result.Count)
compareAndShout(t, n, result.Total)
}
func Test_service_list_keys_by_db_key_type_mixed(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key:string", "string")
mr.HSet("key:hash", "k", "v")
mr.Lpush("key:list", "element")
mr.SetAdd("key:set", "hi")
mr.ZAdd("key:zset", 5, "hi")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.KeyListType
json.Unmarshal(resultStr, &result)
for _, k := range result.Keys {
switch k.Key {
case "key_string":
compareAndShout(t, "string", k.Type)
case "key_hash":
compareAndShout(t, "hash", k.Type)
case "key_list":
compareAndShout(t, "list", k.Type)
case "key_set":
compareAndShout(t, "set", k.Type)
case "key_zset":
compareAndShout(t, "zset", k.Type)
}
}
compareAndShout(t, 5, len(result.Keys))
compareAndShout(t, 5, result.Count)
compareAndShout(t, 5, result.Total)
}
func Test_service_string_type(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key:1", "hi")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/key:1")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ResponseType
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "hi", result.Value)
}
func Test_service_string_check_by_index(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key:1", "Developer")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/key:1/0")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ResponseType
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "D", result.Value)
res, _ = http.Get(s.URL + "/0/key:1/4")
expectedCode = 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "l", result.Value)
res, _ = http.Get(s.URL + "/0/`key:1`/5")
expectedCode = 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "o", result.Value)
res, _ = http.Get(s.URL + "/0/`key:1`/`6`")
expectedCode = 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "p", result.Value)
}
func Test_service_string_check_by_index_wrong_index(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key:1", "Developer")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/key:1/x")
compareAndShout(t, 400, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ErrorType
json.Unmarshal(resultStr, &result)
expectedError := "wrong type for index/field"
compareAndShout(t, expectedError, result.Error)
}
func Test_service_string_type_with_slash_in_key(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key:/1", "hi")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/`key:/1`")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ResponseType
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "hi", result.Value)
}
func Test_service_string_type_with_slash_and_backtick_in_key(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
mr.Set("key:`/1", "hi")
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)
var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()
res, _ := http.Get(s.URL + "/0/`key:`/1`")
expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result types.ResponseType
json.Unmarshal(resultStr, &result)
compareAndShout(t, "string", result.ValueType)
compareAndShout(t, "hi", result.Value)
}
// Validate if listing-keys feature returns 200 if the DB in client request IS exposed
func Test_service_list_keys_for_db_with_access(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", fmt.Sprintf("redis://:@%s", mr.Addr()))
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)