Skip to content

Commit

Permalink
refactor(tests): removed unused dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh-devs committed Jul 28, 2024
1 parent c7a1938 commit 6c2ca15
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 104 deletions.
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ toolchain go1.22.5
require (
github.com/bndr/gojenkins v1.1.0
github.com/distribution/reference v0.6.0
github.com/emersion/go-smtp v0.21.3
github.com/go-logr/logr v1.4.2
github.com/go-logr/zapr v1.3.0
github.com/golang/mock v1.6.0
github.com/mailgun/mailgun-go/v3 v3.6.4
github.com/onsi/ginkgo v1.14.1
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.1
github.com/openshift/api v3.9.0+incompatible
github.com/pkg/errors v0.9.1
Expand All @@ -36,7 +34,6 @@ 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-20231106173351-e73c9f7bad43 // 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
Expand All @@ -46,7 +43,6 @@ require (
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand Down Expand Up @@ -93,7 +89,6 @@ require (
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
Expand Down
6 changes: 1 addition & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ 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-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.21.3 h1:7uVwagE8iPYE48WhNsng3RRpCUpFvNl39JGNSIyGVMY=
github.com/emersion/go-smtp v0.21.3/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
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=
Expand Down Expand Up @@ -56,6 +51,7 @@ github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF
github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
185 changes: 91 additions & 94 deletions pkg/notifications/smtp/smtp_test.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,68 @@
package smtp

import (
"errors"

//"errors"
"fmt"
"io"
"mime/quotedprintable"
"regexp"

"testing"

"github.com/jenkinsci/kubernetes-operator/api/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/notifications/event"
"github.com/jenkinsci/kubernetes-operator/pkg/notifications/reason"

"github.com/emersion/go-smtp"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

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"
// // Headers titles
// 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{}, 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
}
// // 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
// }

//
//// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails
Expand All @@ -74,63 +71,63 @@ func (bkd *testServer) Login(_ *smtp.Conn, username, password string) (smtp.Sess
//}

// A Session is returned after successful login.
type testSession struct {
event event.Event
}

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, 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 {
contentRegex := regexp.MustCompile(`\t+<tr>\n\t+<td><b>(.*):</b></td>\n\t+<td>(.*)</td>\n\t+</tr>`)
headersRegex := regexp.MustCompile(`(.*):\s(.*)`)

b, err := io.ReadAll(quotedprintable.NewReader(r))
if err != nil {
return err
}
content := contentRegex.FindAllStringSubmatch(string(b), -1)
headers := headersRegex.FindAllStringSubmatch(string(b), -1)

if len(content) > 0 {
if s.event.Jenkins.Name == content[0][1] {
return fmt.Errorf("jenkins CR not identical: %s, expected: %s", content[0][1], s.event.Jenkins.Name)
} else if string(s.event.Phase) == content[1][1] {
return fmt.Errorf("phase not identical: %s, expected: %s", content[1][1], s.event.Phase)
}

}

for i := range headers {
switch {
case headers[i][1] == fromHeader && headers[i][2] != testFrom:
return fmt.Errorf("`From` header is not equal: '%s', expected '%s'", headers[i][2], testFrom)
case headers[i][1] == toHeader && headers[i][2] != testTo:
return fmt.Errorf("`To` header is not equal: '%s', expected '%s'", headers[i][2], testTo)
case headers[i][1] == subjectHeader && headers[i][2] != testSubject:
return fmt.Errorf("`Subject` header is not equal: '%s', expected '%s'", headers[i][2], testSubject)
}
}

return nil
}

func (s testSession) Reset() {}
// type testSession struct {
// event event.Event
// }

func (s testSession) Logout() error {
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, 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 {
// // // contentRegex := regexp.MustCompile(`\t+<tr>\n\t+<td><b>(.*):</b></td>\n\t+<td>(.*)</td>\n\t+</tr>`)
// // // headersRegex := regexp.MustCompile(`(.*):\s(.*)`)

// // // b, err := io.ReadAll(quotedprintable.NewReader(r))
// // // if err != nil {
// // // return err
// // // }
// // // content := contentRegex.FindAllStringSubmatch(string(b), -1)
// // // headers := headersRegex.FindAllStringSubmatch(string(b), -1)

// // // if len(content) > 0 {
// // // if s.event.Jenkins.Name == content[0][1] {
// // // return fmt.Errorf("jenkins CR not identical: %s, expected: %s", content[0][1], s.event.Jenkins.Name)
// // // } else if string(s.event.Phase) == content[1][1] {
// // // return fmt.Errorf("phase not identical: %s, expected: %s", content[1][1], s.event.Phase)
// // // }

// // // }

// // // for i := range headers {
// // // switch {
// // // case headers[i][1] == fromHeader && headers[i][2] != testFrom:
// // // return fmt.Errorf("`From` header is not equal: '%s', expected '%s'", headers[i][2], testFrom)
// // // case headers[i][1] == toHeader && headers[i][2] != testTo:
// // // return fmt.Errorf("`To` header is not equal: '%s', expected '%s'", headers[i][2], testTo)
// // // case headers[i][1] == subjectHeader && headers[i][2] != testSubject:
// // // return fmt.Errorf("`Subject` header is not equal: '%s', expected '%s'", headers[i][2], testSubject)
// // // }
// // // }

// // // return nil
// // // }

// func (s testSession) Reset() {}

// func (s testSession) Logout() error {
// return nil
// }

// TODO: @brokenpip3 & @ansh-devs
// TODO: SMTP testing failing due to index out of range error in `Data` method.
Expand Down

0 comments on commit 6c2ca15

Please sign in to comment.