From dd5484b7d750e29fc479d87392b15892d549c20b Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Sun, 9 Jan 2022 01:07:14 +0500 Subject: [PATCH] :label: Refactored Email domain --- internal/domain/email.go | 40 ++++++++++++++++++++++++++- internal/domain/email_test.go | 52 +++++++++++++++++++++++++++++++++++ internal/domain/profile.go | 14 ++-------- 3 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 internal/domain/email_test.go diff --git a/internal/domain/email.go b/internal/domain/email.go index 6454337..43c6bba 100644 --- a/internal/domain/email.go +++ b/internal/domain/email.go @@ -1,3 +1,41 @@ package domain -type Email string +import ( + "strings" + "testing" +) + +type Email struct { + user string + host string +} + +var ErrEmailInvalid error = Error{ + Code: "invalid_request", + Description: "cannot unmarshal email input", +} + +func NewEmail(src string) (*Email, error) { + parts := strings.Split(strings.TrimPrefix(src, "mailto:"), "@") + if len(parts) != 2 { //nolint: gomnd + return nil, ErrEmailInvalid + } + + return &Email{ + user: parts[0], + host: parts[1], + }, nil +} + +func TestEmail(tb testing.TB) *Email { + tb.Helper() + + return &Email{ + user: "user", + host: "example.com", + } +} + +func (e Email) String() string { + return e.user + "@" + e.host +} diff --git a/internal/domain/email_test.go b/internal/domain/email_test.go new file mode 100644 index 0000000..82ee6fc --- /dev/null +++ b/internal/domain/email_test.go @@ -0,0 +1,52 @@ +package domain_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "source.toby3d.me/website/indieauth/internal/domain" +) + +func TestNewEmail(t *testing.T) { + t.Parallel() + + for _, testCase := range []struct { + name string + input string + expError bool + expResult string + }{{ + name: "simple", + input: "user@example.com", + expError: false, + expResult: "user@example.com", + }, { + name: "subaddress", + input: "user+suffix@example.com", + expError: false, + expResult: "user+suffix@example.com", + }, { + name: "prefix", + input: "mailto:user@example.com", + expError: false, + expResult: "user@example.com", + }} { + testCase := testCase + + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + result, err := domain.NewEmail(testCase.input) + if testCase.expError { + assert.Error(t, err) + assert.Nil(t, result) + + return + } + + assert.NoError(t, err) + assert.Equal(t, testCase.expResult, result.String()) + }) + } +} diff --git a/internal/domain/profile.go b/internal/domain/profile.go index 6523a85..0a3689a 100644 --- a/internal/domain/profile.go +++ b/internal/domain/profile.go @@ -8,26 +8,16 @@ import ( type Profile struct { Photo []*URL URL []*URL - Email []Email + Email []*Email Name []string } -// NewProfile creates a new empty Profile. -func NewProfile() *Profile { - return &Profile{ - Email: make([]Email, 0), - Name: make([]string, 0), - Photo: make([]*URL, 0), - URL: make([]*URL, 0), - } -} - // TestProfile returns a valid Profile with the generated test data filled in. func TestProfile(tb testing.TB) *Profile { tb.Helper() return &Profile{ - Email: []Email{"user@example.net"}, + Email: []*Email{TestEmail(tb)}, Name: []string{"Example User"}, Photo: []*URL{TestURL(tb, "https://user.example.net/photo.jpg")}, URL: []*URL{TestURL(tb, "https://user.example.net/")},