File size: 2,926 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package smtpsrv

import (
	"bytes"
	"context"
	"errors"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/alert"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/util/log"
)

func TestSession_Auth(t *testing.T) {
	var sess Session
	err := sess.AuthPlain("", "")
	assert.ErrorContains(t, err, "not supported")
}

func TestSession_Mail(t *testing.T) {
	var sess Session
	err := sess.Mail("", nil)
	assert.ErrorContains(t, err, "sender address")

	err = sess.Mail("test", nil)
	assert.ErrorContains(t, err, "sender address")

	err = sess.Mail("test@localhost", nil)
	assert.NoError(t, err)
}

func TestSession_Rcpt(t *testing.T) {
	var sess Session
	sess.cfg.MaxRecipients = 1
	sess.cfg.BackgroundContext = func() context.Context { return log.WithLogger(context.Background(), log.NewLogger()) }

	err := sess.Rcpt("", nil)
	assert.ErrorContains(t, err, "recipient address")

	err = sess.Rcpt("test", nil)
	assert.ErrorContains(t, err, "recipient address")

	err = sess.Rcpt("test@localhost", nil)
	assert.ErrorContains(t, err, "domain not handled here")

	sess.cfg.Domain = "localhost"
	err = sess.Rcpt("test@localhost", nil)
	assert.ErrorContains(t, err, "recipient address") // must be uuid

	authCtx := sess.cfg.BackgroundContext()

	sess.cfg.AuthorizeFunc = func(ctx context.Context, id string) (context.Context, error) {
		t.Helper()
		assert.Equal(t, "00000000-0000-0000-0000-000000000000", id)
		return authCtx, nil
	}

	err = sess.Rcpt("00000000-0000-0000-0000-000000000000+dedup-value@localhost", nil)
	assert.NoError(t, err)
	assert.Contains(t, sess.authCtx, authCtx)
	assert.Equal(t, "dedup-value", sess.dedup)

	errFailed := errors.New("failed")
	sess.cfg.AuthorizeFunc = func(ctx context.Context, id string) (context.Context, error) {
		return sess.cfg.BackgroundContext(), errFailed
	}

	sess.authCtx = nil
	err = sess.Rcpt("00000000-0000-0000-0000-000000000000+dedup-value@localhost", nil)
	assert.ErrorContains(t, err, "local error")
}

func TestSession_Data(t *testing.T) {
	var sess Session
	err := sess.Data(bytes.NewReader(nil))
	assert.ErrorContains(t, err, "RCPT TO")

	sess.authCtx = []context.Context{permission.ServiceContext(context.Background(), "svc")}
	sess.from = "test@localhost"
	sess.dedup = "dedup-value"

	var createdAlert bool
	sess.cfg.CreateAlertFunc = func(ctx context.Context, a *alert.Alert) error {
		t.Helper()
		createdAlert = true
		assert.Equal(t, "dedup-value", a.DedupKey().Payload)
		assert.Equal(t, "test", a.Summary)
		assert.Equal(t, "From: test@localhost\n\nHello, world!", a.Details)
		assert.Equal(t, alert.SourceEmail, a.Source)
		assert.Equal(t, alert.StatusTriggered, a.Status)
		assert.Equal(t, permission.ServiceID(ctx), a.ServiceID)
		return nil
	}

	err = sess.Data(strings.NewReader("Subject: test\r\n\r\nHello, world!"))
	assert.NoError(t, err)
	assert.True(t, createdAlert, "CreateAlertFunc not called")
}