Skip to content

Commit

Permalink
♻️ Filter non-valid emails from API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Dec 4, 2024
1 parent a25b45a commit 0b1aec1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions userli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions userli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (s *UserliTestSuite) TestGetAliases() {
s.Equal([]string{"[email protected]", "[email protected]"}, 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/[email protected]").
Expand Down Expand Up @@ -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/[email protected]").
Expand Down Expand Up @@ -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/[email protected]").
Expand Down

0 comments on commit 0b1aec1

Please sign in to comment.