feat: authsession service
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package sendemailcode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
stubmail "galaxy/authsession/internal/adapters/mail"
|
||||
stubuserservice "galaxy/authsession/internal/adapters/userservice"
|
||||
"galaxy/authsession/internal/domain/challenge"
|
||||
"galaxy/authsession/internal/domain/common"
|
||||
"galaxy/authsession/internal/domain/userresolution"
|
||||
"galaxy/authsession/internal/testkit"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExecuteWithRuntimeStubUserDirectory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
seed func(*stubuserservice.StubDirectory) error
|
||||
email string
|
||||
wantStatus challenge.Status
|
||||
wantDeliveryState challenge.DeliveryState
|
||||
wantMailCalls int
|
||||
}{
|
||||
{
|
||||
name: "existing user",
|
||||
email: "pilot@example.com",
|
||||
seed: func(directory *stubuserservice.StubDirectory) error {
|
||||
return directory.SeedExisting(common.Email("pilot@example.com"), common.UserID("user-1"))
|
||||
},
|
||||
wantStatus: challenge.StatusSent,
|
||||
wantDeliveryState: challenge.DeliverySent,
|
||||
wantMailCalls: 1,
|
||||
},
|
||||
{
|
||||
name: "creatable user",
|
||||
email: "new@example.com",
|
||||
seed: func(*stubuserservice.StubDirectory) error { return nil },
|
||||
wantStatus: challenge.StatusSent,
|
||||
wantDeliveryState: challenge.DeliverySent,
|
||||
wantMailCalls: 1,
|
||||
},
|
||||
{
|
||||
name: "blocked email",
|
||||
email: "blocked@example.com",
|
||||
seed: func(directory *stubuserservice.StubDirectory) error {
|
||||
return directory.SeedBlockedEmail(common.Email("blocked@example.com"), userresolution.BlockReasonCode("policy_block"))
|
||||
},
|
||||
wantStatus: challenge.StatusDeliverySuppressed,
|
||||
wantDeliveryState: challenge.DeliverySuppressed,
|
||||
wantMailCalls: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
userDirectory := &stubuserservice.StubDirectory{}
|
||||
require.NoError(t, tt.seed(userDirectory))
|
||||
|
||||
challengeStore := &testkit.InMemoryChallengeStore{}
|
||||
mailSender := &stubmail.StubSender{}
|
||||
service, err := New(
|
||||
challengeStore,
|
||||
userDirectory,
|
||||
&testkit.SequenceIDGenerator{ChallengeIDs: []common.ChallengeID{"challenge-1"}},
|
||||
testkit.FixedCodeGenerator{Code: "654321"},
|
||||
testkit.DeterministicCodeHasher{},
|
||||
mailSender,
|
||||
testkit.FixedClock{Time: time.Unix(10, 0).UTC()},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := service.Execute(context.Background(), Input{Email: tt.email})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "challenge-1", result.ChallengeID)
|
||||
|
||||
record, err := challengeStore.Get(context.Background(), common.ChallengeID("challenge-1"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantStatus, record.Status)
|
||||
assert.Equal(t, tt.wantDeliveryState, record.DeliveryState)
|
||||
assert.Len(t, mailSender.RecordedAttempts(), tt.wantMailCalls)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user