-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp-client_test.go
171 lines (138 loc) · 3.99 KB
/
smtp-client_test.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"os"
"strings"
"testing"
smtpmock "github.com/mocktools/go-smtp-mock/v2"
)
func TestParseBody(t *testing.T) {
exampleFull, err := os.ReadFile("examples/full-feedback.json")
if err != nil {
t.Errorf("Cannot read example file")
}
testCases := []struct {
name string
input []byte
expected string
expectError bool
}{
{
name: "valid json",
input: exampleFull,
expected: `Title: Full Feedback Form
From: test.app
Rate: 10 (numbers)
Q: What's great (if anything)?
A: I like speed.
Q: What do you find product useful for?
A: To transfer personal files.
Q: What's missing or what's not great?
A: Ability to do multiple file transfer.
`,
},
{
name: "valid no rating",
input: []byte(`{"channel":"test.app","feedback":{"title":"Full Feedback Form","rate":{"type":"numbers","value":null},"questions":[{"question":"What's great (if anything)?","answer":"Nothing"},{"question":"What do you find product useful for?","answer":""},{"question":"What's missing or what's not great?","answer":""}]}}`),
expected: `Title: Full Feedback Form
From: test.app
Rate: not rated
Q: What's great (if anything)?
A: Nothing
Q: What do you find product useful for?
A:
Q: What's missing or what's not great?
A:
`,
},
{
name: "valid only rating",
input: []byte(`{"channel":"test.app","feedback":{"title":"Full Feedback Form","rate":{"type":"numbers","value":3},"questions":[{"question":"What's great (if anything)?","answer":""},{"question":"What do you find product useful for?","answer":""},{"question":"What's missing or what's not great?","answer":""}]}}`),
expected: `Title: Full Feedback Form
From: test.app
Rate: 3 (numbers)
Q: What's great (if anything)?
A:
Q: What do you find product useful for?
A:
Q: What's missing or what's not great?
A:
`,
},
{
name: "invalid json",
input: []byte(`{"Questions": [`),
expectError: true,
},
{
name: "json, incorrect template",
input: []byte(`{"Test": []}`),
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := parseBody(tc.input)
if tc.expectError {
if err == nil {
t.Errorf("expected error but got none")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if result != tc.expected {
t.Errorf("expected:\n%q\n, but got:\n%q\n", tc.expected, result)
}
})
}
}
func TestConnectAndSendEmailTls(t *testing.T) {
t.Setenv("SMTP_USE_TLS", "true")
t.Setenv("SMTP_USERNAME", "test")
// Write Tls verification test
hostname := "smtp.gmail.com"
port := uint(465)
fromAddr := "feedback@test.test"
toAddr := "no-reply@test.test"
subject := "Test Email"
body := payload
err := connectAndSendEmail(hostname, port, fromAddr, toAddr, subject, body)
expectMsg := "Username and Password not accepted"
if !strings.Contains(err.Error(), expectMsg) {
t.Errorf("Invalid message, got:\n %v, \nexpected:\n %v",
err, expectMsg)
}
}
func TestConnectAndSendEmailInsecureTls(t *testing.T) {
t.Setenv("SMTP_USE_INSECURE_TLS", "true")
t.Setenv("SMTP_USERNAME", "test")
//Mock smtp server
server := smtpmock.New(smtpmock.ConfigurationAttr{
LogToStdout: false,
LogServerActivity: false,
})
// To start server use Start() method
if err := server.Start(); err != nil {
fmt.Println(err)
}
hostAddress, portNumber := "127.0.0.1", server.PortNumber()
// Write Tls verification test
hostname := hostAddress
port := uint(portNumber)
fromAddr := "feedback@test.test"
toAddr := "no-reply@test.test"
subject := "Test Email"
body := payload
err := connectAndSendEmail(hostname, port, fromAddr, toAddr, subject, body)
// Not the best verification, however library doesn't have yet TLS support
expectMsg := "first record does not look like a TLS handshake"
if !strings.Contains(err.Error(), expectMsg) {
t.Errorf("Invalid message, got:\n %v, \nexpected:\n %v",
err, expectMsg)
}
if err := server.Stop(); err != nil {
fmt.Println(err)
}
}