github.com/mailgun/holster/v4@v4.20.0/anonymize/anonymize_test.go (about)

     1  package anonymize
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/suite"
     7  )
     8  
     9  type AnonymizeSuite struct {
    10  	suite.Suite
    11  }
    12  
    13  func TestAnonymizeSuite(t *testing.T) {
    14  	suite.Run(t, new(AnonymizeSuite))
    15  }
    16  
    17  func (s *AnonymizeSuite) TestAnonymizeWithNoSecretsReturnsUnchangedString() {
    18  	anonimized, err := Anonymize("Hello Dear User", nil, "")
    19  	s.Nil(err)
    20  	s.Equal("Hello Dear User", anonimized)
    21  	anonimized, err = Anonymize("Hello Dear User", nil)
    22  	s.Nil(err)
    23  	s.Equal("Hello Dear User", anonimized)
    24  }
    25  
    26  func (s *AnonymizeSuite) TestAnonymizeWithNoSecretsInStringReturnsUnchangedString() {
    27  	anonimized, err := Anonymize("Hello Dear User", nil, "John")
    28  	s.Nil(err)
    29  	s.Equal("Hello Dear User", anonimized)
    30  }
    31  
    32  func (s *AnonymizeSuite) TestAnonymizeEscapesSecrets() {
    33  	anonimized, err := Anonymize(
    34  		"Hello Dear User", nil, `\s`)
    35  	s.Nil(err)
    36  	s.Equal("Hello Dear User", anonimized)
    37  }
    38  
    39  func (s *AnonymizeSuite) TestAnonymizeSquashesAdjacentSecrets() {
    40  	anonimized, err := Anonymize(
    41  		"Hello Иван Иванов ivan ivanov foo.bar",
    42  		nil,
    43  		`"Иван Иванов" <ivan.ivanov@foo.bar>`)
    44  	s.Nil(err)
    45  	s.Equal("Hello xxx", anonimized)
    46  }
    47  
    48  func (s *AnonymizeSuite) TestAnonymizeNames() {
    49  	subjects := map[string]string{
    50  		"ALEX MIA - Welcome to YORK":               "xxx MIA - Welcome to YORK",
    51  		"MIA ALEX - Welcome to YORK":               "MIA xxx - Welcome to YORK",
    52  		"MIA ALEX, Welcome to YORK":                "MIA xxx, Welcome to YORK",
    53  		"MIA ALEX, welcome to YORK":                "MIA xxx, welcome to YORK",
    54  		"Mia ALEX - Welcome to YORK":               "Mia xxx - Welcome to YORK",
    55  		"ALEX Mia - Welcome to YORK":               "xxx Mia - Welcome to YORK",
    56  		"Alex Mia - Welcome to YORK":               "xxx Mia - Welcome to YORK",
    57  		"Mia Alex, Welcome to YORK":                "Mia xxx, Welcome to YORK",
    58  		"ALEX MIA and BORNE ROY, Welcome to YORK":  "xxx MIA and BORNE xxx, Welcome to YORK",
    59  		"Weekly Facebook Page":                     "Weekly Facebook Page",
    60  		"Robert and ROY are coming, are you Alex?": "xxx and xxx are coming, are you xxx?",
    61  	}
    62  	names := []string{"Alex", "Roy", "Robert"}
    63  	for subject, expected := range subjects {
    64  		anonimized, err := Anonymize(subject, names, `ivan.ivanov@foo.bar`)
    65  		s.Nil(err)
    66  		s.Equal(expected, anonimized)
    67  		anonimized, err = Anonymize(subject, names)
    68  		s.Nil(err)
    69  		s.Equal(expected, anonimized)
    70  	}
    71  }