Skip to content

Commit

Permalink
Merge pull request #44 from kartverket/skip1180
Browse files Browse the repository at this point in the history
[SKIP-1180] - Add some tests
  • Loading branch information
martinhny authored Jan 2, 2024
2 parents e21ca7c + 7207c6a commit a9bb845
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 28 deletions.
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ require (
github.com/google/uuid v1.5.0
github.com/microsoftgraph/msgraph-sdk-go v1.28.0
github.com/mnako/letters v0.2.2
github.com/onsi/ginkgo/v2 v2.13.2
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
)

require (
Expand All @@ -27,7 +28,10 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
Expand Down Expand Up @@ -55,6 +59,7 @@ require (
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/std-uritemplate/std-uritemplate/go v0.0.47 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
Expand All @@ -66,6 +71,7 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cjlapao/common-go v0.0.39 h1:bAAUrj2B9v0kMzbAOhzjSmiyDy+rd56r2sy7oEiQLlA=
github.com/cjlapao/common-go v0.0.39/go.mod h1:M3dzazLjTjEtZJbbxoA5ZDiGCiHmpwqW9l4UWaddwOA=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand All @@ -32,16 +35,23 @@ github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down Expand Up @@ -76,6 +86,10 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mnako/letters v0.2.2 h1:koVahnzazHIq3aQnjVZE5SBJ7ZG/mXK6uSM4Jh70Naw=
github.com/mnako/letters v0.2.2/go.mod h1:8DKs/xAQ9A7TP8zrLiZPvlrX5synZnvRyTT5Ja+QUK4=
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
Expand Down Expand Up @@ -118,6 +132,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -138,13 +153,18 @@ golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand Down
18 changes: 9 additions & 9 deletions pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (

// The Backend implements SMTP server methods.
type Backend struct {
sender *skysender.Sender
basicAuth *config.BasicAuthConfig
Sender skysender.Sender
BasicAuth *config.BasicAuthConfig
}

func NewBackend(cfg *config.SkylineConfig) *Backend {
return &Backend{
sender: createSender(cfg),
basicAuth: cfg.BasicAuthConfig,
Sender: createSender(cfg),
BasicAuth: cfg.BasicAuthConfig,
}
}

Expand All @@ -32,21 +32,21 @@ func (b *Backend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
return &Session{
ctx: ctx,
log: logger,
sender: b.sender,
sender: b.Sender,
validateCredentials: b.checkCredentials,
}, nil
}

func (b *Backend) checkCredentials(username string, password string) bool {
if !b.basicAuth.Enabled {
if !b.BasicAuth.Enabled {
slog.Warn("basic auth disabled, but validation called anyway")
return true
}

return username == b.basicAuth.Username && password == b.basicAuth.Password
return username == b.BasicAuth.Username && password == b.BasicAuth.Password
}

func createSender(cfg *config.SkylineConfig) *skysender.Sender {
func createSender(cfg *config.SkylineConfig) skysender.Sender {
var configuredSender skysender.Sender

switch cfg.SenderType {
Expand All @@ -70,5 +70,5 @@ func createSender(cfg *config.SkylineConfig) *skysender.Sender {
os.Exit(1)
}

return &configuredSender
return configuredSender
}
4 changes: 2 additions & 2 deletions pkg/backend/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Session struct {
auth bool
ctx context.Context
log *slog.Logger
sender *sender.Sender
sender sender.Sender
validateCredentials func(string, string) bool
}

Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *Session) Data(r io.Reader) error {
emailsProcessed.Inc()
s.log.Debug("received and parsed email", "mail", msg)

err = (*s.sender).Send(s.ctx, msg)
err = (s.sender).Send(s.ctx, msg)
if err != nil {
s.log.Warn("could not send email", "error", err)
emailsFailed.Inc()
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/config_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
31 changes: 15 additions & 16 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestSenderType_IsValid(t *testing.T) {
var tests = []struct {
name string
input SenderType
want bool
}{
{"msgraph should be valid", 0, true},
{"Dummy should be valid", 1, true},
{"random should be invalid", 2, false},
}
for _, tt := range tests {
assert.Equal(t, SenderType.IsValid(tt.input), tt.want, tt.name)
}
}
var _ = Describe("Config", func() {
Context("Sender type is valid", func() {
It("msgraph should return true", func() {
Expect(SenderType.IsValid(MsGraph)).To(BeTrue())
})
It("dummy should return true", func() {
Expect(SenderType.IsValid(Dummy)).To(BeTrue())
})
It("other should return false", func() {
Expect(SenderType.IsValid(2)).ToNot(BeTrue())
})
})
})
73 changes: 73 additions & 0 deletions pkg/sender/office365_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package sender

import (
. "github.com/kartverket/skyline/pkg/email"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/mnako/letters"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"net/mail"
"time"
)

var _ = Describe("Office365", func() {
recipient := []*mail.Address{{Address: "skip@kartverket.no", Name: "SKIP"}}
ccrecipient := []*mail.Address{{Address: "cc@kartverket.no", Name: "cc"}}
bccrecipient := []*mail.Address{{Address: "bcc@kartverket.no", Name: "bcc"}}
subject := "this is a subject"
txtMsg := "This is a test message"
date := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
htmlMsg := "<html><body>This is a test message</body></html>"
skylineEmail := SkylineEmail{
Email: letters.Email{
Headers: letters.Headers{
ContentType: letters.ContentTypeHeader{
ContentType: "text/plain",
},
To: recipient,
Cc: ccrecipient,
Bcc: bccrecipient,
Date: date,
Subject: subject,
},
Text: txtMsg,
},
}
Context("when mapping", func() {
It("should map basic email to graph email", func() {
graphMail, err := mapToGraphMail(&skylineEmail)
Expect(err).To(BeNil())
Expect(graphMail.GetMessage().GetSubject()).To(Equal(&subject))
Expect(graphMail.GetMessage().GetToRecipients()).To(Equal(toGraphRecipient(recipient...)))
Expect(graphMail.GetMessage().GetCcRecipients()).To(Equal(toGraphRecipient(ccrecipient...)))
Expect(graphMail.GetMessage().GetBccRecipients()).To(Equal(toGraphRecipient(bccrecipient...)))
Expect(graphMail.GetMessage().GetSentDateTime()).To(Equal(&date))
contentType := graphmodels.TEXT_BODYTYPE
Expect(graphMail.GetMessage().GetBody().GetContentType()).To(Equal(&contentType))
Expect(graphMail.GetMessage().GetBody().GetContent()).To(Equal(&txtMsg))
})
It("should map html email to graph email", func() {
skylineEmail.Headers.ContentType.ContentType = "text/html"
skylineEmail.HTML = htmlMsg
graphMail, err := mapToGraphMail(&skylineEmail)
Expect(err).To(BeNil())
contentType := graphmodels.HTML_BODYTYPE
Expect(graphMail.GetMessage().GetBody().GetContentType()).To(Equal(&contentType))
Expect(graphMail.GetMessage().GetBody().GetContent()).To(Equal(&htmlMsg))
})
It("should error if not valid type", func() {
skylineEmail.Headers.ContentType.ContentType = "err"
_, err := mapToGraphMail(&skylineEmail)
Expect(err).ToNot(BeNil())
})
It("should map MIME email to graph email, html type", func() {
skylineEmail.Headers.ContentType.ContentType = "multipart/alternative"
skylineEmail.HTML = htmlMsg
graphMail, err := mapToGraphMail(&skylineEmail)
Expect(err).To(BeNil())
contentType := graphmodels.HTML_BODYTYPE
Expect(graphMail.GetMessage().GetBody().GetContentType()).To(Equal(&contentType))
Expect(graphMail.GetMessage().GetBody().GetContent()).To(Equal(&htmlMsg))
})
})
})
13 changes: 13 additions & 0 deletions pkg/sender/sender_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sender

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestSender(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Sender Suite")
}
Loading

0 comments on commit a9bb845

Please sign in to comment.