Skip to content

Commit

Permalink
Fixing #83 by handling invalid e-mail names
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswdr committed Jan 27, 2022
1 parent 4c5f5d0 commit 0c4e5f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestRandomLetter(t *testing.T) {
Expect(t, 1, len(value))
}

func TestRandomStringWithLength(t *testing.T){
func TestRandomStringWithLength(t *testing.T) {
f := New()
length := f.IntBetween(97, 1000)
value := f.RandomStringWithLength(length)
Expand Down
24 changes: 15 additions & 9 deletions internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package faker

import (
"net/http"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -39,6 +40,13 @@ type Internet struct {
Faker *Faker
}

func (i Internet) transformIntoValidEmailName(name string) string {
name = strings.ToLower(name)
onlyValidCharacters := regexp.MustCompile(`[^a-z0-9._%+\-]+`)
name = onlyValidCharacters.ReplaceAllString(name, "_")
return name
}

// User returns a fake user for Internet
func (i Internet) User() string {
user := i.Faker.RandomStringElement(userFormats)
Expand All @@ -47,12 +55,12 @@ func (i Internet) User() string {

// {{firstName}}
if strings.Contains(user, "{{firstName}}") {
user = strings.Replace(user, "{{firstName}}", strings.ToLower(p.FirstName()), 1)
user = strings.Replace(user, "{{firstName}}", i.transformIntoValidEmailName(p.FirstName()), 1)
}

// {{lastName}}
if strings.Contains(user, "{{lastName}}") {
user = strings.Replace(user, "{{lastName}}", strings.ToLower(p.LastName()), 1)
user = strings.Replace(user, "{{lastName}}", i.transformIntoValidEmailName(p.LastName()), 1)
}

return user
Expand Down Expand Up @@ -86,7 +94,7 @@ func (i Internet) Email() string {

// {{user}}
if strings.Contains(email, "{{user}}") {
email = strings.Replace(email, "{{user}}", i.User(), 1)
email = strings.Replace(email, "{{user}}", i.transformIntoValidEmailName(i.User()), 1)
}

// {{domain}}
Expand All @@ -106,25 +114,23 @@ func (i Internet) Email() string {
func (i Internet) FreeEmail() string {
domain := i.Faker.RandomStringElement(freeEmailDomain)

return strings.Join([]string{i.User(), domain}, "@")
return strings.Join([]string{i.transformIntoValidEmailName(i.User()), domain}, "@")
}

// SafeEmail returns a fake safe email address for Internet
func (i Internet) SafeEmail() string {
return strings.Join([]string{i.User(), i.SafeEmailDomain()}, "@")
return strings.Join([]string{i.transformIntoValidEmailName(i.User()), i.SafeEmailDomain()}, "@")
}

// CompanyEmail returns a fake company email address for Internet
func (i Internet) CompanyEmail() string {
c := i.Faker.Company()

companyName := c.Name()
companyName = strings.ToLower(companyName)
companyName = strings.Replace(companyName, " ", ".", -1)
companyName := i.transformIntoValidEmailName(c.Name())

domain := strings.Join([]string{companyName, i.Domain()}, ".")

return strings.Join([]string{i.User(), domain}, "@")
return strings.Join([]string{i.transformIntoValidEmailName(i.User()), domain}, "@")
}

// TLD returns a fake tld for Internet
Expand Down
10 changes: 10 additions & 0 deletions internet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package faker

import (
"net/mail"
"strings"
"testing"
)
Expand All @@ -13,6 +14,11 @@ func TestUser(t *testing.T) {
Expect(t, false, strings.Contains(user, " "))
}

func isValidEmail(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}

func TestDomain(t *testing.T) {
i := New().Internet()

Expand All @@ -32,6 +38,7 @@ func TestEmail(t *testing.T) {
split := strings.Split(email, "@")

Expect(t, 2, len(split))
Expect(t, true, isValidEmail(email))
}

func TestFreeEmail(t *testing.T) {
Expand All @@ -41,6 +48,7 @@ func TestFreeEmail(t *testing.T) {
split := strings.Split(email, "@")

Expect(t, 2, len(split))
Expect(t, true, isValidEmail(email))
}

func TestSafeEmail(t *testing.T) {
Expand All @@ -50,6 +58,7 @@ func TestSafeEmail(t *testing.T) {
split := strings.Split(email, "@")

Expect(t, 2, len(split))
Expect(t, true, isValidEmail(email))
}

func TestCompanyEmail(t *testing.T) {
Expand All @@ -60,6 +69,7 @@ func TestCompanyEmail(t *testing.T) {

Expect(t, 2, len(split))
Expect(t, false, strings.Contains(email, " "))
Expect(t, true, isValidEmail(email))
}

func TestPassword(t *testing.T) {
Expand Down

0 comments on commit 0c4e5f4

Please sign in to comment.