|
| 1 | +package email |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "gopkg.in/gomail.v2" |
| 13 | + |
| 14 | + "github.com/go-playground/log" |
| 15 | +) |
| 16 | + |
| 17 | +// NOTES: |
| 18 | +// - Run "go test" to run tests |
| 19 | +// - Run "gocov test | gocov report" to report on test converage by file |
| 20 | +// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called |
| 21 | + |
| 22 | +// or |
| 23 | + |
| 24 | +// -- may be a good idea to change to output path to somewherelike /tmp |
| 25 | +// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html |
| 26 | + |
| 27 | +type Client struct { |
| 28 | + conn net.Conn |
| 29 | + address string |
| 30 | + time int64 |
| 31 | + bufin *bufio.Reader |
| 32 | + bufout *bufio.Writer |
| 33 | +} |
| 34 | + |
| 35 | +func (c *Client) w(s string) { |
| 36 | + c.bufout.WriteString(s + "\r\n") |
| 37 | + c.bufout.Flush() |
| 38 | +} |
| 39 | +func (c *Client) r() string { |
| 40 | + reply, err := c.bufin.ReadString('\n') |
| 41 | + if err != nil { |
| 42 | + fmt.Println("e ", err) |
| 43 | + } |
| 44 | + return reply |
| 45 | +} |
| 46 | + |
| 47 | +func handleClient(c *Client, closePrematurly bool) string { |
| 48 | + |
| 49 | + var msg []byte |
| 50 | + |
| 51 | + c.w("220 Welcome to the Jungle") |
| 52 | + msg = append(msg, c.r()...) |
| 53 | + |
| 54 | + c.w("250 No one says helo anymore") |
| 55 | + msg = append(msg, c.r()...) |
| 56 | + |
| 57 | + c.w("250 Sender") |
| 58 | + msg = append(msg, c.r()...) |
| 59 | + |
| 60 | + c.w("250 Recipient") |
| 61 | + msg = append(msg, c.r()...) |
| 62 | + |
| 63 | + c.w("354 Ok Send data ending with <CRLF>.<CRLF>") |
| 64 | + for { |
| 65 | + text := c.r() |
| 66 | + bytes := []byte(text) |
| 67 | + msg = append(msg, bytes...) |
| 68 | + |
| 69 | + // 46 13 10 |
| 70 | + if bytes[0] == 46 && bytes[1] == 13 && bytes[2] == 10 { |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if !closePrematurly { |
| 76 | + c.w("250 server has transmitted the message") |
| 77 | + } |
| 78 | + |
| 79 | + c.conn.Close() |
| 80 | + |
| 81 | + return string(msg) |
| 82 | +} |
| 83 | + |
| 84 | +func TestEmailHandler(t *testing.T) { |
| 85 | + |
| 86 | + tests := []struct { |
| 87 | + expected string |
| 88 | + }{ |
| 89 | + { |
| 90 | + |
| 91 | + }, |
| 92 | + { |
| 93 | + |
| 94 | + }, |
| 95 | + { |
| 96 | + expected: "Subject: debug", |
| 97 | + }, |
| 98 | + { |
| 99 | + expected: "DEBUG", |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + email := New( "localhost", 3041, "", "", "[email protected]", [] string{ "[email protected]"}) |
| 104 | + email.SetBuffersAndWorkers(1, 0) |
| 105 | + email.SetTimestampFormat("MST") |
| 106 | + email.SetKeepAliveTimout(time.Second * 0) |
| 107 | + email.SetEmailTemplate(defaultTemplate) |
| 108 | + email.SetFilenameDisplay(log.Llongfile) |
| 109 | + // email.SetFormatFunc(testFormatFunc) |
| 110 | + log.RegisterHandler(email, log.InfoLevel, log.DebugLevel) |
| 111 | + |
| 112 | + var msg string |
| 113 | + |
| 114 | + server, err := net.Listen("tcp", ":3041") |
| 115 | + if err != nil { |
| 116 | + t.Errorf("Expected <nil> Got '%s'", err) |
| 117 | + } |
| 118 | + |
| 119 | + defer server.Close() |
| 120 | + |
| 121 | + go func() { |
| 122 | + for { |
| 123 | + conn, err := server.Accept() |
| 124 | + if err != nil { |
| 125 | + t.Errorf("Expected <nil> Got '%s'", err) |
| 126 | + } |
| 127 | + |
| 128 | + if conn == nil { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + c := &Client{ |
| 133 | + conn: conn, |
| 134 | + address: conn.RemoteAddr().String(), |
| 135 | + time: time.Now().Unix(), |
| 136 | + bufin: bufio.NewReader(conn), |
| 137 | + bufout: bufio.NewWriter(conn), |
| 138 | + } |
| 139 | + |
| 140 | + msg = handleClient(c, false) |
| 141 | + } |
| 142 | + }() |
| 143 | + |
| 144 | + log.Debug("debug") |
| 145 | + |
| 146 | + for i, tt := range tests { |
| 147 | + if !strings.Contains(msg, tt.expected) { |
| 148 | + t.Errorf("Index %d Expected '%s' Got '%s'", i, tt.expected, msg) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // this is normally not safe, but in these tests won't cause any issue |
| 153 | + // flipping during runtime. |
| 154 | + email.SetFilenameDisplay(log.Lshortfile) |
| 155 | + |
| 156 | + log.Debug("debug") |
| 157 | + |
| 158 | + for i, tt := range tests { |
| 159 | + if !strings.Contains(msg, tt.expected) { |
| 160 | + t.Errorf("Index %d Expected '%s' Got '%s'", i, tt.expected, msg) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestBadDial(t *testing.T) { |
| 166 | + email := New( "localhost", 3041, "", "", "[email protected]", [] string{ "[email protected]"}) |
| 167 | + email.SetFormatFunc(testFormatFunc) |
| 168 | + log.RegisterHandler(email, log.InfoLevel) |
| 169 | + |
| 170 | + log.Info("info test") |
| 171 | +} |
| 172 | + |
| 173 | +func TestBadEmailTemplate(t *testing.T) { |
| 174 | + badTemplate := `{{ .NonExistantField }}` // referencing non-existant field |
| 175 | + email := New( "localhost", 3041, "", "", "[email protected]", [] string{ "[email protected]"}) |
| 176 | + email.SetEmailTemplate(badTemplate) |
| 177 | + log.RegisterHandler(email, log.InfoLevel) |
| 178 | + |
| 179 | + log.Info("info test") |
| 180 | +} |
| 181 | + |
| 182 | +func TestBadSend(t *testing.T) { |
| 183 | + |
| 184 | + email := New( "localhost", 3041, "", "", "[email protected]", [] string{ "[email protected]"}) |
| 185 | + log.RegisterHandler(email, log.InfoLevel) |
| 186 | + |
| 187 | + var msg string |
| 188 | + |
| 189 | + server, err := net.Listen("tcp", ":3041") |
| 190 | + if err != nil { |
| 191 | + t.Errorf("Expected <nil> Got '%s'", err) |
| 192 | + } |
| 193 | + |
| 194 | + defer server.Close() |
| 195 | + |
| 196 | + go func() { |
| 197 | + for { |
| 198 | + conn, err := server.Accept() |
| 199 | + if err != nil { |
| 200 | + t.Errorf("Expected <nil> Got '%s'", err) |
| 201 | + } |
| 202 | + |
| 203 | + if conn == nil { |
| 204 | + continue |
| 205 | + } |
| 206 | + |
| 207 | + c := &Client{ |
| 208 | + conn: conn, |
| 209 | + address: conn.RemoteAddr().String(), |
| 210 | + time: time.Now().Unix(), |
| 211 | + bufin: bufio.NewReader(conn), |
| 212 | + bufout: bufio.NewWriter(conn), |
| 213 | + } |
| 214 | + |
| 215 | + msg = handleClient(c, true) |
| 216 | + } |
| 217 | + }() |
| 218 | + |
| 219 | + log.Info("info") |
| 220 | +} |
| 221 | + |
| 222 | +func testFormatFunc(email *Email) Formatter { |
| 223 | + var err error |
| 224 | + b := new(bytes.Buffer) |
| 225 | + message := gomail.NewMessage() |
| 226 | + message.SetHeader("From", email.From()) |
| 227 | + message.SetHeader("To", email.To()...) |
| 228 | + |
| 229 | + return func(e *log.Entry) *gomail.Message { |
| 230 | + b.Reset() |
| 231 | + if err = email.template.ExecuteTemplate(b, "email", e); err != nil { |
| 232 | + log.WithFields(log.F("error", err)).Error("Error parsing Email handler template") |
| 233 | + } |
| 234 | + |
| 235 | + message.SetHeader("Subject", e.Message) |
| 236 | + message.SetBody(contentType, b.String()) |
| 237 | + |
| 238 | + return message |
| 239 | + } |
| 240 | +} |
0 commit comments