Skip to content
Snippets Groups Projects
Commit c91fd974 authored by James Page's avatar James Page
Browse files

Add NormalizeEmail and ValidateIPAddress functions

Added function to normalise an email address. Currently setup with the default rules for how some email domains allow plus-addressing and other variations, but we can extend it if we need to.
Added function to validate and clean an IP address.
Added tests for both functions, as well as for StripEmail.
parent 85d6c66d
No related branches found
Tags v1.217.0
No related merge requests found
......@@ -3,11 +3,13 @@ package utils
import (
"bytes"
emailverifier "github.com/AfterShip/email-verifier"
normalizer "github.com/dimuska139/go-email-normalizer/v2"
"github.com/jinzhu/now"
"github.com/mohae/deepcopy"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
"math"
"net"
"net/url"
"os"
"reflect"
......@@ -149,6 +151,11 @@ func StripEmail(email string) (strippedEmail string, strippedDomain string) {
return strippedEmail, strippedDomain
}
func NormalizeEmail(email string) string {
emailNormalizer := normalizer.NewNormalizer()
return emailNormalizer.Normalize(email)
}
// IsUrlStrict Returns whether a URL is valid in a strict way (Must have scheme and host)
func IsUrlStrict(str string) bool {
u, err := url.Parse(str)
......@@ -258,3 +265,12 @@ func DetermineDaysLeft(fromDateLocal time.Time, toDateLocal time.Time) int {
toDate := now.With(toDateLocal).EndOfDay()
return int(math.Floor(toDate.Sub(fromDate).Hours() / 24))
}
func ValidateIPAddress(ipAddress string) (cleanedIPAddress string, err error) {
ipAddress = strings.ToLower(strings.TrimSpace(ipAddress))
ip := net.ParseIP(ipAddress)
if ip == nil {
return "", errors.Error("invalid IP address")
}
return ipAddress, nil
}
package utils
import (
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"testing"
)
func TestStripEmail(t *testing.T) {
tests := []struct {
name string
email string
wantStripped string
wantDomain string
}{
{
name: "Test with + symbol",
email: "test+extra@gmail.com",
wantStripped: "test@gmail.com",
wantDomain: "gmail.com",
},
{
name: "Test without + symbol",
email: "test@gmail.com",
wantStripped: "test@gmail.com",
wantDomain: "gmail.com",
},
{
name: "Test with multiple + symbols",
email: "test+extra+more@gmail.com",
wantStripped: "test@gmail.com",
wantDomain: "gmail.com",
},
{
name: "Test with different domain",
email: "test+extra@yahoo.com",
wantStripped: "test@yahoo.com",
wantDomain: "yahoo.com",
},
{
name: "Test with subdomain",
email: "test+extra@mail.example.com",
wantStripped: "test@mail.example.com",
wantDomain: "mail.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotStripped, gotDomain := StripEmail(tt.email)
if gotStripped != tt.wantStripped {
t.Errorf("StripEmail() gotStripped = %v, want %v", gotStripped, tt.wantStripped)
}
if gotDomain != tt.wantDomain {
t.Errorf("StripEmail() gotDomain = %v, want %v", gotDomain, tt.wantDomain)
}
})
}
}
func TestNormalizeEmail(t *testing.T) {
tests := []struct {
name string
email string
wantNormalized string
}{
{
name: "Test with + symbol",
email: "test+extra@gmail.com",
wantNormalized: "test@gmail.com",
},
{
name: "Test without + symbol",
email: "test@gmail.com",
wantNormalized: "test@gmail.com",
},
{
name: "Test with multiple + symbols",
email: "test+extra+more@gmail.com",
wantNormalized: "test@gmail.com",
},
{
name: "Test with different domain",
email: "test-extra@yahoo.com",
wantNormalized: "testextra@yahoo.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotNormalized := NormalizeEmail(tt.email)
if gotNormalized != tt.wantNormalized {
t.Errorf("NormalizeEmail() gotNormalized = %v, want %v", gotNormalized, tt.wantNormalized)
}
})
}
}
func TestValidateIPAddress(t *testing.T) {
tests := []struct {
name string
ip string
want string
err error
}{
{
name: "Test with valid IPv4",
ip: "192.168.1.1",
want: "192.168.1.1",
err: nil,
},
{
name: "Test with valid IPv6",
ip: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
want: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
err: nil,
},
{
name: "Test with invalid IP",
ip: "999.999.999.999",
want: "",
err: errors.Error("invalid IP address"),
},
{
name: "Test with empty string",
ip: "",
want: "",
err: errors.Error("invalid IP address"),
},
{
name: "Test with non-IP string",
ip: "not an ip address",
want: "",
err: errors.Error("invalid IP address"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ValidateIPAddress(tt.ip)
if got != tt.want {
t.Errorf("ValidateIPAddress() got = %v, want %v", got, tt.want)
}
if err != nil && err.Error() != tt.err.Error() {
t.Errorf("ValidateIPAddress() err = %v, want %v", err, tt.err)
}
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment