-
Notifications
You must be signed in to change notification settings - Fork 1
/
mocksmtp.go
69 lines (57 loc) · 1.48 KB
/
mocksmtp.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
package mocksmtp
import (
"errors"
"sync"
"github.com/gobuffalo/buffalo/mail"
)
// ErrNoMessage is returned when no message was caught.
var ErrNoMessage = errors.New("no message sent")
// MockSMTP is an in-memory implementation for buffalo `Sender`
// interface. It's intended to catch sent messages for test purposes.
type MockSMTP struct {
messages []mail.Message
mutex sync.RWMutex
}
// Send implements buffalo `Sender` interface, to send mails.
func (s *MockSMTP) Send(m mail.Message) error {
s.mutex.Lock()
defer s.mutex.Unlock()
s.messages = append(s.messages, m)
return nil
}
// LastMessage gets the last sent message, if it exists.
// It returns `NoMessage` error if there is not last message.
func (s *MockSMTP) LastMessage() (mail.Message, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
l := len(s.Messages())
if l == 0 {
return mail.Message{}, ErrNoMessage
}
return s.Messages()[l-1], nil
}
// Messages gets the list of sent messages.
func (s *MockSMTP) Messages() []mail.Message {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.messages
}
// Count gets the amount of sent messages.
func (s *MockSMTP) Count() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return len(s.messages)
}
// Clear destroys all sent messages.
func (s *MockSMTP) Clear() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.messages = []mail.Message{}
}
// New constructs a new MockSMTP.
func New() *MockSMTP {
return &MockSMTP{
messages: []mail.Message{},
mutex: sync.RWMutex{},
}
}