diff --git a/userli.go b/userli.go index b380011..6ebc632 100644 --- a/userli.go +++ b/userli.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "time" ) @@ -30,6 +31,10 @@ func NewUserli(token, baseURL string) *Userli { } func (u *Userli) GetAliases(email string) ([]string, error) { + if !strings.Contains(email, "@") { + return []string{}, nil + } + resp, err := u.call(fmt.Sprintf("%s/api/postfix/alias/%s", u.baseURL, email)) if err != nil { return []string{}, err @@ -60,6 +65,10 @@ func (u *Userli) GetDomain(domain string) (bool, error) { } func (u *Userli) GetMailbox(email string) (bool, error) { + if !strings.Contains(email, "@") { + return false, nil + } + resp, err := u.call(fmt.Sprintf("%s/api/postfix/mailbox/%s", u.baseURL, email)) if err != nil { return false, err @@ -75,6 +84,10 @@ func (u *Userli) GetMailbox(email string) (bool, error) { } func (u *Userli) GetSenders(email string) ([]string, error) { + if !strings.Contains(email, "@") { + return []string{}, nil + } + resp, err := u.call(fmt.Sprintf("%s/api/postfix/senders/%s", u.baseURL, email)) if err != nil { return []string{}, err diff --git a/userli_test.go b/userli_test.go index b86f0e0..7fcbfdf 100644 --- a/userli_test.go +++ b/userli_test.go @@ -37,6 +37,12 @@ func (s *UserliTestSuite) TestGetAliases() { s.Equal([]string{"source1@example.com", "source2@example.com"}, aliases) }) + s.Run("no email", func() { + aliases, err := s.userli.GetAliases("alias") + s.NoError(err) + s.Empty(aliases) + }) + s.Run("error", func() { gock.New("http://localhost:8000"). Get("/api/postfix/alias/alias@example.com"). @@ -120,6 +126,12 @@ func (s *UserliTestSuite) TestGetMailbox() { s.True(gock.IsDone()) }) + s.Run("no email", func() { + active, err := s.userli.GetMailbox("user") + s.NoError(err) + s.False(active) + }) + s.Run("not found", func() { gock.New("http://localhost:8000"). Get("/api/postfix/mailbox/user@example.org"). @@ -170,6 +182,12 @@ func (s *UserliTestSuite) TestGetSenders() { s.True(gock.IsDone()) }) + s.Run("no email", func() { + senders, err := s.userli.GetSenders("user") + s.NoError(err) + s.Empty(senders) + }) + s.Run("alias success", func() { gock.New("http://localhost:8000"). Get("/api/postfix/senders/alias@example.com").