Skip to content

Commit

Permalink
Merge pull request #2 from NGRsoftlab/feat-tests-and-upgrade
Browse files Browse the repository at this point in the history
feat: tests, cosmetics, test auth method and go packages upgrade
  • Loading branch information
NGRsoftlab authored May 23, 2024
2 parents 51860a0 + 17cc5af commit aae0014
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
go-version: '1.22.3'

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# emailer
Tiny lib for email (with attachments) sending (smtp)
Tiny lib for email (with attachments) sending (smtp, only login auth is supported now)

# import
```import "github.com/NGRsoftlab/ngr-emailer"```
Expand Down
20 changes: 16 additions & 4 deletions emailer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2020-2023 NGR Softlab
//
// Copyright 2020-2024 NGR Softlab
package emailer

import (
Expand Down Expand Up @@ -39,14 +38,27 @@ func NewSender(login, password, email, server string) *Sender {

/////////////////////////////////////////////

// Send send smtp pack (mail)
// Send send smtp pack (mail) with login auth
func (s *Sender) Send() error {
err := smtp.SendMail(s.ServerSMTP,
LoginAuth(s.Login, s.Password),
s.Login, s.to, s.message)

if err != nil {
logging.Logger.Error("SEND ERROR: ", err)
logging.Logger.Errorf("send error: %s", err.Error())
return err
}
return nil
}

// SendWithAuth send smtp pack (mail) with custom auth
func (s *Sender) SendWithAuth(auth smtp.Auth) error {
err := smtp.SendMail(s.ServerSMTP,
auth,
s.Login, s.to, s.message)

if err != nil {
logging.Logger.Errorf("send error: %s", err.Error())
return err
}
return nil
Expand Down
61 changes: 53 additions & 8 deletions emailer_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2020-2023 NGR Softlab
//
// Copyright 2020-2024 NGR Softlab
package emailer

import (
"crypto/tls"
"fmt"
"github.com/stretchr/testify/require"
"testing"
"time"
)

/////////////////////////////////////////////////
// Put correct here before testing.
func TestSimpleMail(t *testing.T) {
func TestNewMessage(t *testing.T) {
s := NewSender(
"",
"",
Expand Down Expand Up @@ -39,8 +39,53 @@ func TestSimpleMail(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = s.Send()
if err != nil {
t.Fatal(err)
}

func TestTestLoginAuth(t *testing.T) {
type testParams struct {
host string
port int
tlsOn bool
tlsConfig *tls.Config
user string
password string
connTimeout time.Duration
}

tests := []struct {
name string
params testParams
wantAuthError bool
wantConnError bool
}{
{
name: "connection error (bad host)",
params: testParams{
host: "test",
port: 25,
tlsOn: false,
user: "test",
password: "test",
connTimeout: time.Second * 2,
},
wantConnError: true,
wantAuthError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
connErr, authErr := TestLoginAuth(
tt.params.host, tt.params.port,
tt.params.tlsOn, tt.params.tlsConfig,
tt.params.user, tt.params.password,
tt.params.connTimeout)
if tt.wantConnError {
require.Error(t, connErr)
}
if tt.wantAuthError {
require.Error(t, authErr)
}
})
}
}
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
module github.com/NGRsoftlab/ngr-emailer

go 1.13
go 1.22.3

require (
github.com/NGRsoftlab/ngr-logging v1.0.0
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
18 changes: 12 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
github.com/NGRsoftlab/ngr-logging v1.0.0 h1:Yp42kvw/bofZ6xXC5jPlPx1HNabZQY9cvzGnB0earJY=
github.com/NGRsoftlab/ngr-logging v1.0.0/go.mod h1:99kZ+XwSK7rKRitmhZvqOdYnPf9Qepywt3zjJJcJDME=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
66 changes: 63 additions & 3 deletions login_auth.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright 2020-2023 NGR Softlab
//
// Copyright 2020-2024 NGR Softlab
package emailer

import (
"context"
"crypto/tls"
"errors"
"fmt"
logging "github.com/NGRsoftlab/ngr-logging"
"net/smtp"
"time"
)

// loginAuth struct for login auth
Expand All @@ -18,7 +22,7 @@ func LoginAuth(username, password string) smtp.Auth {
}

// Start login auth start
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
func (a *loginAuth) Start(_ *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}

Expand All @@ -36,3 +40,59 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
}
return nil, nil
}

////////////////////////////////////////////////////

// TestLoginAuth - test smtp server login auth
func TestLoginAuth(
host string, port int,
tlsOn bool, tlsConfig *tls.Config,
user, password string,
connTimeout time.Duration) (connError error, authError error) {

c, connError := smtp.Dial(fmt.Sprintf("%s:%v", host, port))
if connError != nil {
return connError, nil
}
defer func() {
if errClose := c.Close(); errClose != nil {
logging.Logger.Errorf("smtp conn close error: %s", errClose.Error())
}
}()

if tlsOn {
if connError = c.StartTLS(tlsConfig); connError != nil {
logging.Logger.Errorf("start tsl error: %s", connError.Error())
return connError, nil
}
}

ctx, cancel := context.WithTimeout(
context.Background(),
connTimeout)

go func(ctx context.Context) {
defer cancel()

if authError = c.Auth(LoginAuth(user, password)); authError != nil {
logging.Logger.Errorf("auth error: %s", authError.Error())
}
}(ctx)

select {
case <-ctx.Done():
switch ctx.Err() {
case context.DeadlineExceeded:
logging.Logger.Error("smtp auth conn deadline")
authError = errors.New("auth conn deadline is over")
case context.Canceled:
logging.Logger.Info("smtp auth conn cancel by timeout")
}
}

if authError != nil {
logging.Logger.Errorf("smtp auth conn test error: %s", authError.Error())
}

return connError, authError
}
1 change: 0 additions & 1 deletion message_params.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2020-2023 NGR Softlab
//
package emailer

type MessageParams struct {
Expand Down

0 comments on commit aae0014

Please sign in to comment.