diff --git a/go.mod b/go.mod index 7ee66305..3eeb54c4 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.12 require ( github.com/bndr/gojenkins v1.1.0 github.com/distribution/reference v0.6.0 + github.com/emersion/go-smtp v0.24.0 github.com/go-logr/logr v1.4.2 github.com/go-logr/zapr v1.3.0 github.com/golang/mock v1.6.0 @@ -32,6 +33,7 @@ require ( github.com/blang/semver/v4 v4.0.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect github.com/emicklei/go-restful/v3 v3.12.1 // indirect github.com/evanphx/json-patch v5.9.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.9.0 // indirect diff --git a/go.sum b/go.sum index 7edb8e60..e8a53646 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 h1:oP4q0fw+fOSWn3DfFi4EXdT+B+gTtzx8GC9xsc26Znk= +github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= +github.com/emersion/go-smtp v0.24.0 h1:g6AfoF140mvW0vLNPD/LuCBLEAdlxOjIXqbIkJIS6Wk= +github.com/emersion/go-smtp v0.24.0/go.mod h1:ZtRRkbTyp2XTHCA+BmyTFTrj8xY4I+b4McvHxCU2gsQ= github.com/emicklei/go-restful/v3 v3.12.1 h1:PJMDIM/ak7btuL8Ex0iYET9hxM3CI2sjZtzpL63nKAU= github.com/emicklei/go-restful/v3 v3.12.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls= diff --git a/pkg/notifications/smtp/smtp_test.go b/pkg/notifications/smtp/smtp_test.go index f2371566..9096d1f5 100644 --- a/pkg/notifications/smtp/smtp_test.go +++ b/pkg/notifications/smtp/smtp_test.go @@ -1,11 +1,14 @@ package smtp import ( - - //"errors" - + "errors" + "fmt" + "io" + "strings" "testing" + smtp "github.com/emersion/go-smtp" + "github.com/jenkinsci/kubernetes-operator/api/v1alpha2" "github.com/jenkinsci/kubernetes-operator/pkg/notifications/event" "github.com/jenkinsci/kubernetes-operator/pkg/notifications/reason" @@ -16,203 +19,91 @@ import ( ) const ( - // testSMTPUsername = "username" - // testSMTPPassword = "password" + testSMTPUsername = "username" + testSMTPPassword = "password" - // testSMTPPort = 1025 + testSMTPPort = 1025 - // testFrom = "test@localhost" - // testTo = "test.to@localhost" - // testSubject = "Jenkins Operator Notification" + testFrom = "test@localhost" + testTo = "test.to@localhost" + testSubject = "Jenkins Operator Notification" - // // Headers titles - // fromHeader = "From" - // toHeader = "To" - // subjectHeader = "Subject" + fromHeader = "From" + toHeader = "To" + subjectHeader = "Subject" nilConst = "nil" ) var ( -// testPhase = event.PhaseUser -// testCrName = "test-cr" -// testNamespace = "default" -// testReason = reason.NewPodRestart( -// -// reason.KubernetesSource, -// []string{"test-reason-1"}, -// []string{"test-verbose-1"}..., -// -// ) -// testLevel = v1alpha2.NotificationLevelWarning + testPhase = event.PhaseUser + testCrName = "test-cr" + testNamespace = "default" + testReason = reason.NewPodRestart( + reason.KubernetesSource, + []string{"test-reason-1"}, + []string{"test-verbose-1"}..., + ) + testLevel = v1alpha2.NotificationLevelWarning ) -// type testServer struct { -// event event.Event -// } +type testServer struct { + event event.Event +} -// NewSession implements smtp.Backend. -// func (t *testServer) NewSession(c *smtp.Conn) (smtp.Session, error) { -// return testSession{}, nil -// } +func (t *testServer) NewSession(c *smtp.Conn) (smtp.Session, error) { + return &testSession{event: t.event}, nil +} -// // TODO: @brokenpip3 fix me -// func (bkd *testServer) Login(_ *smtp.Conn, username, password string) (smtp.Session, error) { -// if username != testSMTPUsername || password != testSMTPPassword { -// return nil, errors.New("invalid username or password") -// } -// return &testSession{event: bkd.event}, nil -// } +func (bkd *testServer) Login(_ *smtp.Conn, username, password string) (smtp.Session, error) { + if username != testSMTPUsername || password != testSMTPPassword { + return nil, errors.New("invalid username or password") + } + return &testSession{event: bkd.event}, nil +} -// -//// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails -//func (bkd *testServer) AnonymousLogin(_ *smtp.ConnectionState) (smtp.Session, error) { -// return nil, smtp.ErrAuthRequired -//} +type testSession struct { + event event.Event +} -// A Session is returned after successful login. -// type testSession struct { -// event event.Event -// } +func (s *testSession) Mail(from string, opts *smtp.MailOptions) error { + if from != testFrom { + return fmt.Errorf("from header mismatch: got %s expected %s", from, testFrom) + } + return nil +} -// // func (s testSession) Mail(from string, mop *smtp.MailOptions) error { -// // if from != testFrom { -// // return fmt.Errorf("`From` header is not equal: '%s', expected '%s'", from, testFrom) -// // } -// // return nil -// // } +func (s *testSession) Rcpt(to string, opts *smtp.RcptOptions) error { + if to != testTo { + return fmt.Errorf("to header mismatch: got %s expected %s", to, testTo) + } + return nil +} -// // func (s testSession) Rcpt(to string, mop *smtp.RcptOptions) error { -// // if to != testTo { -// // return fmt.Errorf("`To` header is not equal: '%s', expected '%s'", to, testTo) -// // } -// // return nil -// // } +func (s *testSession) Data(r io.Reader) error { + b, err := io.ReadAll(r) + if err != nil { + return err + } -// // // func (s testSession) Data(r io.Reader) error { -// // // contentRegex := regexp.MustCompile(`\t+