86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package user_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"messenger/internal/platform/apperr"
|
|
"messenger/internal/platform/ident"
|
|
"messenger/internal/user"
|
|
"messenger/internal/user/store"
|
|
)
|
|
|
|
func newService() *user.Service {
|
|
return user.NewService(store.NewInMemUsers())
|
|
}
|
|
|
|
func TestRegisterStoresUser(t *testing.T) {
|
|
svc := newService()
|
|
ctx := context.Background()
|
|
u, err := svc.Register(ctx, "alice", "Алиса")
|
|
if err != nil {
|
|
t.Fatalf("Register error: %v", err)
|
|
}
|
|
if u.ID.Version() != 7 {
|
|
t.Fatalf("id must be UUIDv7, got version %d", u.ID.Version())
|
|
}
|
|
got, err := svc.GetByID(ctx, u.ID)
|
|
if err != nil || got != u {
|
|
t.Fatalf("GetByID = %+v, %v; want %+v", got, err, u)
|
|
}
|
|
if ok, err := svc.Exists(ctx, u.ID); err != nil || !ok {
|
|
t.Fatalf("Exists = %v, %v; want true", ok, err)
|
|
}
|
|
}
|
|
|
|
func TestRegisterRejectsInvalidInput(t *testing.T) {
|
|
_, err := newService().Register(context.Background(), "Bad Name", "x")
|
|
if apperr.KindOf(err) != apperr.KindInvalid {
|
|
t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err)
|
|
}
|
|
}
|
|
|
|
func TestRegisterReportsDuplicateAsConflict(t *testing.T) {
|
|
svc := newService()
|
|
ctx := context.Background()
|
|
if _, err := svc.Register(ctx, "alice", "A"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := svc.Register(ctx, "alice", "B")
|
|
if !errors.Is(err, user.ErrUsernameTaken) || apperr.KindOf(err) != apperr.KindConflict {
|
|
t.Fatalf("err = %v, want ErrUsernameTaken with KindConflict", err)
|
|
}
|
|
}
|
|
|
|
func TestGetByIDUnknownIsNotFound(t *testing.T) {
|
|
svc := newService()
|
|
_, err := svc.GetByID(context.Background(), ident.New())
|
|
if !errors.Is(err, user.ErrNotFound) || apperr.KindOf(err) != apperr.KindNotFound {
|
|
t.Fatalf("err = %v, want ErrNotFound with KindNotFound", err)
|
|
}
|
|
if ok, err := svc.Exists(context.Background(), ident.New()); err != nil || ok {
|
|
t.Fatalf("Exists = %v, %v; want false, nil", ok, err)
|
|
}
|
|
}
|
|
|
|
func TestExistingIDsFiltersInOneCall(t *testing.T) {
|
|
svc := newService()
|
|
ctx := context.Background()
|
|
alice, _ := svc.Register(ctx, "alice", "A")
|
|
bob, _ := svc.Register(ctx, "bob", "B")
|
|
unknown := ident.New()
|
|
got, err := svc.ExistingIDs(ctx, []uuid.UUID{unknown, bob.ID, alice.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 || got[0] != bob.ID || got[1] != alice.ID {
|
|
t.Fatalf("ExistingIDs = %v, want bob then alice", got)
|
|
}
|
|
if empty, err := svc.ExistingIDs(ctx, nil); err != nil || len(empty) != 0 {
|
|
t.Fatalf("ExistingIDs(nil) = %v, %v", empty, err)
|
|
}
|
|
}
|