1
1
import { expect } from 'chai' ;
2
- import { SECRET_KIND , redactSecrets } from './secrets' ;
2
+ import { SECRET_KIND } from './secrets' ;
3
3
import type { SecretKind } from './secrets' ;
4
+ import { redact } from './index' ;
4
5
5
- describe ( 'secret redaction on a string ' , function ( ) {
6
+ describe ( 'dictionary-based secret redaction' , function ( ) {
6
7
for ( const kind of SECRET_KIND ) {
7
8
it ( `redacts content of kind '${ kind } '` , function ( ) {
8
9
const secret = '123456' ;
9
10
const content = '123456' ;
10
11
11
- const redacted = redactSecrets ( content , [ { value : secret , kind : kind } ] ) ;
12
+ const redacted = redact ( content , [ { value : secret , kind : kind } ] ) ;
12
13
13
14
expect ( redacted ) . equal ( `<${ kind } >` ) ;
14
15
} ) ;
15
16
}
16
17
17
18
for ( const invalidValue of [ null , undefined , false , 0 ] ) {
18
19
it ( `returns itself on an invalid value like ${ String ( invalidValue ) } ` , function ( ) {
19
- expect ( redactSecrets ( invalidValue as any , [ ] ) ) . equal ( invalidValue ) ;
20
+ expect ( redact ( invalidValue as any , [ ] ) ) . equal ( invalidValue ) ;
20
21
} ) ;
21
22
}
22
23
24
+ it ( 'redacts multiple coincidences of a secret' , function ( ) {
25
+ const secret = '123456' ;
26
+ const content = '123456 abc 123456' ;
27
+
28
+ const redacted = redact ( content , [ { value : secret , kind : 'password' } ] ) ;
29
+
30
+ expect ( redacted ) . to . equal ( '<password> abc <password>' ) ;
31
+ } ) ;
32
+
23
33
it ( 'rejects unknown types' , function ( ) {
24
34
expect ( ( ) =>
25
- redactSecrets ( 'some content to redact' , [
35
+ redact ( 'some content to redact' , [
26
36
{
27
37
value : 'some' ,
28
38
kind : 'invalid' as SecretKind ,
@@ -35,9 +45,7 @@ describe('secret redaction on a string', function () {
35
45
const secret = '123456' ;
36
46
const content = '.123456.' ;
37
47
38
- const redacted = redactSecrets ( content , [
39
- { value : secret , kind : 'password' } ,
40
- ] ) ;
48
+ const redacted = redact ( content , [ { value : secret , kind : 'password' } ] ) ;
41
49
42
50
expect ( redacted ) . equal ( '.<password>.' ) ;
43
51
} ) ;
@@ -46,9 +54,7 @@ describe('secret redaction on a string', function () {
46
54
const secret = '123456' ;
47
55
const content = 'abc123456def' ;
48
56
49
- const redacted = redactSecrets ( content , [
50
- { value : secret , kind : 'password' } ,
51
- ] ) ;
57
+ const redacted = redact ( content , [ { value : secret , kind : 'password' } ] ) ;
52
58
53
59
expect ( redacted ) . equal ( 'abc123456def' ) ;
54
60
} ) ;
@@ -57,9 +63,7 @@ describe('secret redaction on a string', function () {
57
63
const secret = '.+' ;
58
64
const content = '.abcdef.' ;
59
65
60
- const redacted = redactSecrets ( content , [
61
- { value : secret , kind : 'password' } ,
62
- ] ) ;
66
+ const redacted = redact ( content , [ { value : secret , kind : 'password' } ] ) ;
63
67
64
68
expect ( redacted ) . equal ( '.abcdef.' ) ;
65
69
} ) ;
@@ -68,9 +72,7 @@ describe('secret redaction on a string', function () {
68
72
const secret = 'abc' ;
69
73
const content = [ 'abc' , 'cbd' ] ;
70
74
71
- const redacted = redactSecrets ( content , [
72
- { value : secret , kind : 'password' } ,
73
- ] ) ;
75
+ const redacted = redact ( content , [ { value : secret , kind : 'password' } ] ) ;
74
76
75
77
expect ( redacted ) . deep . equal ( [ '<password>' , 'cbd' ] ) ;
76
78
} ) ;
@@ -81,7 +83,7 @@ describe('secret redaction on a string', function () {
81
83
82
84
const content = { pwd : pwdSecret , usr : usrSecret } ;
83
85
84
- const redacted = redactSecrets ( content , [
86
+ const redacted = redact ( content , [
85
87
{ value : pwdSecret , kind : 'password' } ,
86
88
{ value : usrSecret , kind : 'user' } ,
87
89
] ) ;
0 commit comments