2022-01-24 05:08:05 -05:00
package redactor
2017-10-02 10:32:02 +02:00
import (
2021-03-04 20:08:03 +01:00
"os"
2017-10-02 10:32:02 +02:00
"testing"
2017-11-08 11:40:04 +01:00
"github.com/stretchr/testify/assert"
2020-10-30 12:44:05 +01:00
"github.com/stretchr/testify/require"
2017-10-02 10:32:02 +02:00
)
func Test_doOnJSON ( t * testing . T ) {
2021-03-04 20:08:03 +01:00
baseConfiguration , err := os . ReadFile ( "./testdata/example.json" )
2020-10-30 12:44:05 +01:00
require . NoError ( t , err )
anomConfiguration := doOnJSON ( string ( baseConfiguration ) )
2021-03-04 20:08:03 +01:00
expectedConfiguration , err := os . ReadFile ( "./testdata/expected.json" )
2020-10-30 12:44:05 +01:00
require . NoError ( t , err )
2017-10-02 10:32:02 +02:00
2020-10-30 12:44:05 +01:00
assert . JSONEq ( t , string ( expectedConfiguration ) , anomConfiguration )
2017-10-02 10:32:02 +02:00
}
func Test_doOnJSON_simple ( t * testing . T ) {
testCases := [ ] struct {
name string
input string
expectedOutput string
} {
{
name : "email" ,
input : ` {
"email1" : "goo@example.com" ,
"email2" : "foo.bargoo@example.com" ,
"email3" : "foo.bargoo@example.com.us"
} ` ,
expectedOutput : ` {
"email1" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ,
"email2" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ,
"email3" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
} ` ,
} ,
{
name : "url" ,
input : ` {
"URL" : "foo domain.com foo" ,
"URL" : "foo sub.domain.com foo" ,
"URL" : "foo sub.sub.domain.com foo" ,
2023-09-26 08:28:25 +02:00
"URL" : "foo sub.sub.sub.domain.com.us foo" ,
"URL" : "https://hub.example.com" , "foo" : "bar"
2017-10-02 10:32:02 +02:00
} ` ,
expectedOutput : ` {
"URL" : "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo" ,
"URL" : "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo" ,
"URL" : "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo" ,
2023-09-26 08:28:25 +02:00
"URL" : "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo" ,
"URL" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" , "foo" : "bar"
2017-10-02 10:32:02 +02:00
} ` ,
} ,
}
for _ , test := range testCases {
t . Run ( test . name , func ( t * testing . T ) {
2022-01-24 05:08:05 -05:00
t . Parallel ( )
2017-10-02 10:32:02 +02:00
output := doOnJSON ( test . input )
assert . Equal ( t , test . expectedOutput , output )
} )
}
}