60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package sendemailcode
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"galaxy/authsession/internal/domain/common"
|
|
"galaxy/authsession/internal/testkit"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
func TestExecuteLogsSafeOutcomeFields(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
logger, buffer := newObservedServiceLogger()
|
|
service, err := NewWithObservability(
|
|
&testkit.InMemoryChallengeStore{},
|
|
&testkit.InMemoryUserDirectory{},
|
|
&testkit.SequenceIDGenerator{ChallengeIDs: []common.ChallengeID{"challenge-1"}},
|
|
testkit.FixedCodeGenerator{Code: "654321"},
|
|
testkit.DeterministicCodeHasher{},
|
|
&testkit.RecordingMailSender{},
|
|
nil,
|
|
testkit.FixedClock{Time: time.Unix(10, 0).UTC()},
|
|
logger,
|
|
nil,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
_, err = service.Execute(context.Background(), Input{Email: "pilot@example.com"})
|
|
require.NoError(t, err)
|
|
|
|
logOutput := buffer.String()
|
|
assert.Contains(t, logOutput, "send_email_code")
|
|
assert.Contains(t, logOutput, "challenge-1")
|
|
assert.Contains(t, logOutput, "\"outcome\":\"sent\"")
|
|
assert.NotContains(t, logOutput, "pilot@example.com")
|
|
assert.NotContains(t, logOutput, "654321")
|
|
}
|
|
|
|
func newObservedServiceLogger() (*zap.Logger, *bytes.Buffer) {
|
|
buffer := &bytes.Buffer{}
|
|
encoderConfig := zap.NewProductionEncoderConfig()
|
|
encoderConfig.TimeKey = ""
|
|
|
|
core := zapcore.NewCore(
|
|
zapcore.NewJSONEncoder(encoderConfig),
|
|
zapcore.AddSync(buffer),
|
|
zap.DebugLevel,
|
|
)
|
|
|
|
return zap.New(core), buffer
|
|
}
|