+ IsValidEmail
This commit is contained in:
parent
98f8e21ad2
commit
0b60553c59
@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
@ -58,6 +59,15 @@ func IsStringInSliceOfStrings(slice []string, target string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValidEmail returns true, if s is a email address.
|
||||||
|
func IsValidEmail(s string) bool {
|
||||||
|
// Used Regex instead of mail.ParseAddress(), as the latter incorrectly recognizes samples such as “bad@domain” as a valid address
|
||||||
|
// Shoutout to emailregex.com for the almighty regular expression
|
||||||
|
return regexp.MustCompile(
|
||||||
|
`(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`,
|
||||||
|
).MatchString(s)
|
||||||
|
}
|
||||||
|
|
||||||
// IsValidURL returns true, if s is a valid URL
|
// IsValidURL returns true, if s is a valid URL
|
||||||
func IsValidURL(s string) bool {
|
func IsValidURL(s string) bool {
|
||||||
u, err := url.Parse(s)
|
u, err := url.Parse(s)
|
||||||
|
@ -4,6 +4,42 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestDoesFileExist(t *testing.T) {
|
||||||
|
data := map[string]bool{
|
||||||
|
"/usr/bin/go": true,
|
||||||
|
"/etc/fstab": true,
|
||||||
|
"/etc/os-release": true,
|
||||||
|
"/usr/bin": false,
|
||||||
|
"/home/derschmierigetypvomaldi": false,
|
||||||
|
"/tmp/plainTextPasswords.txt": false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for d, expectedResult := range data {
|
||||||
|
result := DoesFileExist(d)
|
||||||
|
if result != expectedResult {
|
||||||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDoesFolderExist(t *testing.T) {
|
||||||
|
data := map[string]bool{
|
||||||
|
"/home": true,
|
||||||
|
"/tmp": true,
|
||||||
|
"/usr/bin": true,
|
||||||
|
"/home/derschmierigetypvomaldi": false,
|
||||||
|
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
|
||||||
|
"/usr/bin/go": false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for d, expectedResult := range data {
|
||||||
|
result := DoesFolderExist(d)
|
||||||
|
if result != expectedResult {
|
||||||
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsStringInSliceOfStrings(t *testing.T) {
|
func TestIsStringInSliceOfStrings(t *testing.T) {
|
||||||
slice1 := []string{"apple", "banana", "cherry", "date"}
|
slice1 := []string{"apple", "banana", "cherry", "date"}
|
||||||
target1 := "banana"
|
target1 := "banana"
|
||||||
@ -76,36 +112,23 @@ func TestIsValidUUID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDoesFileExist(t *testing.T) {
|
func TestIsValidEmail(t *testing.T) {
|
||||||
data := map[string]bool{
|
data := map[string]bool{
|
||||||
"/usr/bin/go": true,
|
"test@example.com": true,
|
||||||
"/etc/fstab": true,
|
"another.test@domain.co.uk": true,
|
||||||
"/etc/os-release": true,
|
"username@domain.c": true,
|
||||||
"/usr/bin": false,
|
"": false,
|
||||||
"/home/derschmierigetypvomaldi": false,
|
"invalid-email": false,
|
||||||
"/tmp/plainTextPasswords.txt": false,
|
"bad@domain": false,
|
||||||
|
"@missingusername.com": false,
|
||||||
|
"plainaddress": false,
|
||||||
|
"@missingdomain": false,
|
||||||
|
"missingatsign.com": false,
|
||||||
|
"username@.com": false,
|
||||||
}
|
}
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
for d, expectedResult := range data {
|
||||||
result := DoesFileExist(d)
|
result := IsValidEmail(d)
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDoesFolderExist(t *testing.T) {
|
|
||||||
data := map[string]bool{
|
|
||||||
"/home": true,
|
|
||||||
"/tmp": true,
|
|
||||||
"/usr/bin": true,
|
|
||||||
"/home/derschmierigetypvomaldi": false,
|
|
||||||
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
|
|
||||||
"/usr/bin/go": false,
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := DoesFolderExist(d)
|
|
||||||
if result != expectedResult {
|
if result != expectedResult {
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user