🏷️ Refactored Email domain
This commit is contained in:
parent
757c06842c
commit
dd5484b7d7
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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/")},
|
||||
|
|
Loading…
Reference in New Issue