Skip to content

Commit

Permalink
Merge pull request #28 from systemli/Filter-non-valid-emails-from-API…
Browse files Browse the repository at this point in the history
…-requests

♻️ Filter non-valid emails from API requests
  • Loading branch information
0x46616c6b authored Dec 4, 2024
2 parents 17f88cd + 0b1aec1 commit d3a9109
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{"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").
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/user@example.org").
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/alias@example.com").
Expand Down

0 comments on commit d3a9109

Please sign in to comment.