58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
|
|
package email
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"sync"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
)
|
||
|
|
|
||
|
|
type recordingHook struct {
|
||
|
|
mu sync.Mutex
|
||
|
|
calls []hookCall
|
||
|
|
}
|
||
|
|
|
||
|
|
type hookCall struct {
|
||
|
|
key string
|
||
|
|
value string
|
||
|
|
seconds int
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *recordingHook) SetexCtx(_ context.Context, key, value string, seconds int) error {
|
||
|
|
h.mu.Lock()
|
||
|
|
defer h.mu.Unlock()
|
||
|
|
h.calls = append(h.calls, hookCall{key: key, value: value, seconds: seconds})
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMockSender_RedisHookWritesBodyPerRecipient(t *testing.T) {
|
||
|
|
hook := &recordingHook{}
|
||
|
|
s := NewMockSender(WithMockRedis(hook, 30))
|
||
|
|
|
||
|
|
_, err := s.Send(context.Background(), &Message{
|
||
|
|
From: "noreply@k6.local",
|
||
|
|
To: []string{"alice@example.com", "bob@example.com"},
|
||
|
|
Subject: "code",
|
||
|
|
Body: "code is 123456",
|
||
|
|
})
|
||
|
|
require.NoError(t, err)
|
||
|
|
|
||
|
|
hook.mu.Lock()
|
||
|
|
defer hook.mu.Unlock()
|
||
|
|
require.Len(t, hook.calls, 2)
|
||
|
|
assert.Equal(t, MockEmailRedisKeyPrefix+"alice@example.com", hook.calls[0].key)
|
||
|
|
assert.Equal(t, MockEmailRedisKeyPrefix+"bob@example.com", hook.calls[1].key)
|
||
|
|
for _, c := range hook.calls {
|
||
|
|
assert.Equal(t, "code is 123456", c.value)
|
||
|
|
assert.Equal(t, 30, c.seconds)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMockSender_NoHookSkipsRedis(t *testing.T) {
|
||
|
|
s := NewMockSender()
|
||
|
|
_, err := s.Send(context.Background(), &Message{To: []string{"x@y"}, Body: "z"})
|
||
|
|
require.NoError(t, err)
|
||
|
|
}
|