Debian Codegraph @

pkg/services/email/smtp/smtp_auth_test.go

raw
1 package smtp
2
3 import (
4 "net/smtp"
5
6 ginkgo "github.com/onsi/ginkgo/v2"
7 gomega "github.com/onsi/gomega"
8 )
9
10 var _ = ginkgo.Describe("newAuth", func() {
11 ginkgo.It("should return a nil mechanism when Auth is None", func() {
12 got, err := newAuth(&Config{Auth: AuthTypes.None})
13 gomega.Expect(err).NotTo(gomega.HaveOccurred())
14 gomega.Expect(got).To(gomega.BeNil())
15 })
16
17 ginkgo.It("should return PlainAuth when Auth is Plain", func() {
18 got, err := newAuth(&Config{
19 Auth: AuthTypes.Plain,
20 Username: "user",
21 Password: "pass",
22 Host: "mail.example.com",
23 })
24 gomega.Expect(err).NotTo(gomega.HaveOccurred())
25 gomega.Expect(got).NotTo(gomega.BeNil())
26
27 want := smtp.PlainAuth("", "user", "pass", "mail.example.com")
28 gomega.Expect(got).To(gomega.BeAssignableToTypeOf(want))
29 })
30
31 ginkgo.It("should return CRAMMD5Auth when Auth is CRAMMD5", func() {
32 got, err := newAuth(&Config{
33 Auth: AuthTypes.CRAMMD5,
34 Username: "user",
35 Password: "pass",
36 })
37 gomega.Expect(err).NotTo(gomega.HaveOccurred())
38 gomega.Expect(got).NotTo(gomega.BeNil())
39 gomega.Expect(got).To(gomega.BeAssignableToTypeOf(smtp.CRAMMD5Auth("user", "pass")))
40 })
41
42 ginkgo.It("should return oauth2Auth when Auth is OAuth2", func() {
43 got, err := newAuth(&Config{
44 Auth: AuthTypes.OAuth2,
45 Username: "user",
46 Password: "token",
47 })
48 gomega.Expect(err).NotTo(gomega.HaveOccurred())
49
50 _, ok := got.(*oauth2Auth)
51 gomega.Expect(ok).To(gomega.BeTrue())
52 })
53
54 ginkgo.It("should return loginAuth when Auth is Login", func() {
55 got, err := newAuth(&Config{
56 Auth: AuthTypes.Login,
57 Username: "user",
58 Password: "pass",
59 Host: "mail.example.com",
60 })
61 gomega.Expect(err).NotTo(gomega.HaveOccurred())
62
63 _, ok := got.(*loginAuth)
64 gomega.Expect(ok).To(gomega.BeTrue())
65 })
66
67 ginkgo.It("should fail when Auth is Unknown", func() {
68 got, err := newAuth(&Config{Auth: AuthTypes.Unknown})
69 gomega.Expect(got).To(gomega.BeNil())
70 gomega.Expect(err).To(matchFailure(FailAuthType))
71 gomega.Expect(err.Error()).To(gomega.ContainSubstring("Unknown"))
72 })
73
74 ginkgo.It("should fail when Auth is not a known method", func() {
75 got, err := newAuth(&Config{Auth: authType(99)})
76 gomega.Expect(got).To(gomega.BeNil())
77 gomega.Expect(err).To(matchFailure(FailAuthType))
78 })
79 })