all repos

onasty @ 5179466

a one-time notes service

onasty/internal/mailer/testing_mailer.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
refactor(mailer): add mutex to testing mailer, to avoid race-conditions (#27), 1 year ago
1
package mailer
2
3
import (
4
	"context"
5
	"sync"
6
)
7
8
var _ Mailer = (*TestMailer)(nil)
9
10
type TestMailer struct {
11
	mu sync.Mutex
12
13
	emails map[string]string
14
}
15
16
// NewTestMailer create a mailer for tests
17
// that implementation of Mailer stores all sent email in memory
18
// to get the last email sent to a specific email use GetLastSentEmailToEmail
19
func NewTestMailer() *TestMailer {
20
	return &TestMailer{ //nolint:exhaustruct
21
		emails: make(map[string]string),
22
	}
23
}
24
25
func (t *TestMailer) Send(_ context.Context, to, _, content string) error {
26
	t.mu.Lock()
27
	defer t.mu.Unlock()
28
29
	t.emails[to] = content
30
31
	return nil
32
}
33
34
// GetLastSentEmailToEmail returns the last email sent to a specific email
35
func (t *TestMailer) GetLastSentEmailToEmail(email string) string {
36
	t.mu.Lock()
37
	defer t.mu.Unlock()
38
39
	e := t.emails[email]
40
41
	return e
42
}