204 lines
7.0 KiB
Go
204 lines
7.0 KiB
Go
package testkit
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/stretchr/testify/require"
|
|
"testing"
|
|
|
|
"galaxy/authsession/internal/domain/common"
|
|
"galaxy/authsession/internal/domain/userresolution"
|
|
"galaxy/authsession/internal/ports"
|
|
)
|
|
|
|
func TestInMemoryUserDirectoryResolveExistingCreatableAndBlocked(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := &InMemoryUserDirectory{}
|
|
if err := directory.SeedExisting(common.Email("existing@example.com"), common.UserID("user-existing")); err != nil {
|
|
require.Failf(t, "test failed", "SeedExisting() returned error: %v", err)
|
|
}
|
|
if err := directory.SeedBlockedEmail(common.Email("blocked@example.com"), userresolution.BlockReasonCode("policy_block")); err != nil {
|
|
require.Failf(t, "test failed", "SeedBlockedEmail() returned error: %v", err)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
email common.Email
|
|
wantKind userresolution.Kind
|
|
}{
|
|
{name: "existing", email: common.Email("existing@example.com"), wantKind: userresolution.KindExisting},
|
|
{name: "creatable", email: common.Email("new@example.com"), wantKind: userresolution.KindCreatable},
|
|
{name: "blocked", email: common.Email("blocked@example.com"), wantKind: userresolution.KindBlocked},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := directory.ResolveByEmail(context.Background(), tt.email)
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "ResolveByEmail() returned error: %v", err)
|
|
}
|
|
if got.Kind != tt.wantKind {
|
|
require.Failf(t, "test failed", "ResolveByEmail().Kind = %q, want %q", got.Kind, tt.wantKind)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInMemoryUserDirectoryEnsureUserExistingCreatedAndBlocked(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := &InMemoryUserDirectory{}
|
|
if err := directory.SeedExisting(common.Email("existing@example.com"), common.UserID("user-existing")); err != nil {
|
|
require.Failf(t, "test failed", "SeedExisting() returned error: %v", err)
|
|
}
|
|
if err := directory.SeedBlockedEmail(common.Email("blocked@example.com"), userresolution.BlockReasonCode("policy_block")); err != nil {
|
|
require.Failf(t, "test failed", "SeedBlockedEmail() returned error: %v", err)
|
|
}
|
|
if err := directory.QueueCreatedUserIDs(common.UserID("user-created")); err != nil {
|
|
require.Failf(t, "test failed", "QueueCreatedUserIDs() returned error: %v", err)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
email common.Email
|
|
wantOutcome ports.EnsureUserOutcome
|
|
wantUserID common.UserID
|
|
}{
|
|
{
|
|
name: "existing",
|
|
email: common.Email("existing@example.com"),
|
|
wantOutcome: ports.EnsureUserOutcomeExisting,
|
|
wantUserID: common.UserID("user-existing"),
|
|
},
|
|
{
|
|
name: "created",
|
|
email: common.Email("created@example.com"),
|
|
wantOutcome: ports.EnsureUserOutcomeCreated,
|
|
wantUserID: common.UserID("user-created"),
|
|
},
|
|
{
|
|
name: "blocked",
|
|
email: common.Email("blocked@example.com"),
|
|
wantOutcome: ports.EnsureUserOutcomeBlocked,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := directory.EnsureUserByEmail(context.Background(), tt.email)
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "EnsureUserByEmail() returned error: %v", err)
|
|
}
|
|
if got.Outcome != tt.wantOutcome {
|
|
require.Failf(t, "test failed", "EnsureUserByEmail().Outcome = %q, want %q", got.Outcome, tt.wantOutcome)
|
|
}
|
|
if got.UserID != tt.wantUserID {
|
|
require.Failf(t, "test failed", "EnsureUserByEmail().UserID = %q, want %q", got.UserID, tt.wantUserID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInMemoryUserDirectoryExistsByUserID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := &InMemoryUserDirectory{}
|
|
if err := directory.SeedExisting(common.Email("existing@example.com"), common.UserID("user-existing")); err != nil {
|
|
require.Failf(t, "test failed", "SeedExisting() returned error: %v", err)
|
|
}
|
|
|
|
exists, err := directory.ExistsByUserID(context.Background(), common.UserID("user-existing"))
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "ExistsByUserID() returned error: %v", err)
|
|
}
|
|
if !exists {
|
|
require.FailNow(t, "ExistsByUserID() = false, want true")
|
|
}
|
|
|
|
exists, err = directory.ExistsByUserID(context.Background(), common.UserID("missing"))
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "ExistsByUserID() returned error: %v", err)
|
|
}
|
|
if exists {
|
|
require.FailNow(t, "ExistsByUserID() = true, want false")
|
|
}
|
|
}
|
|
|
|
func TestInMemoryUserDirectoryBlockByEmail(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := &InMemoryUserDirectory{}
|
|
result, err := directory.BlockByEmail(context.Background(), ports.BlockUserByEmailInput{
|
|
Email: common.Email("blocked@example.com"),
|
|
ReasonCode: userresolution.BlockReasonCode("policy_block"),
|
|
})
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "BlockByEmail() returned error: %v", err)
|
|
}
|
|
if result.Outcome != ports.BlockUserOutcomeBlocked {
|
|
require.Failf(t, "test failed", "BlockByEmail().Outcome = %q, want %q", result.Outcome, ports.BlockUserOutcomeBlocked)
|
|
}
|
|
|
|
resolution, err := directory.ResolveByEmail(context.Background(), common.Email("blocked@example.com"))
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "ResolveByEmail() returned error: %v", err)
|
|
}
|
|
if resolution.Kind != userresolution.KindBlocked {
|
|
require.Failf(t, "test failed", "ResolveByEmail().Kind = %q, want %q", resolution.Kind, userresolution.KindBlocked)
|
|
}
|
|
}
|
|
|
|
func TestInMemoryUserDirectoryBlockByUserID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := &InMemoryUserDirectory{}
|
|
if err := directory.SeedExisting(common.Email("pilot@example.com"), common.UserID("user-1")); err != nil {
|
|
require.Failf(t, "test failed", "SeedExisting() returned error: %v", err)
|
|
}
|
|
|
|
result, err := directory.BlockByUserID(context.Background(), ports.BlockUserByIDInput{
|
|
UserID: common.UserID("user-1"),
|
|
ReasonCode: userresolution.BlockReasonCode("policy_block"),
|
|
})
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "BlockByUserID() returned error: %v", err)
|
|
}
|
|
if result.Outcome != ports.BlockUserOutcomeBlocked {
|
|
require.Failf(t, "test failed", "BlockByUserID().Outcome = %q, want %q", result.Outcome, ports.BlockUserOutcomeBlocked)
|
|
}
|
|
|
|
second, err := directory.BlockByUserID(context.Background(), ports.BlockUserByIDInput{
|
|
UserID: common.UserID("user-1"),
|
|
ReasonCode: userresolution.BlockReasonCode("policy_block"),
|
|
})
|
|
if err != nil {
|
|
require.Failf(t, "test failed", "second BlockByUserID() returned error: %v", err)
|
|
}
|
|
if second.Outcome != ports.BlockUserOutcomeAlreadyBlocked {
|
|
require.Failf(t, "test failed", "second BlockByUserID().Outcome = %q, want %q", second.Outcome, ports.BlockUserOutcomeAlreadyBlocked)
|
|
}
|
|
}
|
|
|
|
func TestInMemoryUserDirectoryBlockByUserIDNotFound(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
directory := &InMemoryUserDirectory{}
|
|
|
|
_, err := directory.BlockByUserID(context.Background(), ports.BlockUserByIDInput{
|
|
UserID: common.UserID("missing"),
|
|
ReasonCode: userresolution.BlockReasonCode("policy_block"),
|
|
})
|
|
if !errors.Is(err, ports.ErrNotFound) {
|
|
require.Failf(t, "test failed", "BlockByUserID() error = %v, want ErrNotFound", err)
|
|
}
|
|
}
|