all repos

onasty @ af0a94f

a one-time notes service

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

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
package mailer

import (
	"context"
	"sync"
)

var _ Mailer = (*TestMailer)(nil)

type TestMailer struct {
	mu sync.Mutex

	emails map[string]string
}

// NewTestMailer create a mailer for tests
// that implementation of Mailer stores all sent email in memory
// to get the last email sent to a specific email use GetLastSentEmailToEmail
func NewTestMailer() *TestMailer {
	return &TestMailer{ //nolint:exhaustruct
		emails: make(map[string]string),
	}
}

func (t *TestMailer) Send(_ context.Context, to, _, content string) error {
	t.mu.Lock()
	defer t.mu.Unlock()

	t.emails[to] = content

	return nil
}

// GetLastSentEmailToEmail returns the last email sent to a specific email
func (t *TestMailer) GetLastSentEmailToEmail(email string) string {
	t.mu.Lock()
	defer t.mu.Unlock()

	e := t.emails[email]

	return e
}