Files
mtusi2026lab3/internal/service/production/chats_test.go
T

262 lines
8.1 KiB
Go

package production
import (
"context"
"errors"
"reflect"
"slices"
"testing"
"time"
"github.com/google/uuid"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store"
)
// TestService_ChatsCreate checks membership in one batch and persists only validated chats.
func TestService_ChatsCreate(t *testing.T) {
t.Run("Positive: one membership query and original chat fields", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor, other := uuid.New(), uuid.New()
var queries [][]uuid.UUID
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
queries = append(queries, slices.Clone(ids))
return true, nil
}
var persisted dto.ChatDTO
repo.createChat = func(_ context.Context, input dto.ChatDTO) (dto.ChatDTO, error) {
persisted = input
input.CreatedAt = time.Unix(1700000000, 0).UTC()
return input, nil
}
svc := newStubService(t, repo)
input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: " Team 👋 ", MemberIDs: []string{actor.String(), other.String()}}
// Act.
got, err := svc.ChatsCreate(t.Context(), input)
// Assert.
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(queries, [][]uuid.UUID{{actor}, {actor, other}}) {
t.Fatalf("existence queries=%v", queries)
}
if persisted.ID == uuid.Nil || persisted.Title != input.Title || !slices.Equal(persisted.MemberIDs, []uuid.UUID{actor, other}) {
t.Fatalf("wrong persistence input: %+v", persisted)
}
if got.ID != persisted.ID || got.CreatedAt.IsZero() || got.Title != input.Title || !slices.Equal(got.MemberIDs, persisted.MemberIDs) {
t.Fatalf("wrong returned chat: %+v", got)
}
})
t.Run("Negative: unknown member prevents persistence", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor, missing := uuid.New(), uuid.New()
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
return !slices.Contains(ids, missing), nil
}
svc := newStubService(t, repo)
input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: "Team", MemberIDs: []string{actor.String(), missing.String()}}
// Act.
got, err := svc.ChatsCreate(t.Context(), input)
// Assert.
if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v", got, err)
}
})
t.Run("Negative: membership lookup failure prevents persistence", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor, other := uuid.New(), uuid.New()
failure := errors.New("membership unavailable")
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
if len(ids) == 1 {
return true, nil
}
return false, failure
}
svc := newStubService(t, repo)
input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: "Team", MemberIDs: []string{actor.String(), other.String()}}
// Act.
got, err := svc.ChatsCreate(t.Context(), input)
// Assert.
if !errors.Is(err, failure) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v", got, err)
}
})
t.Run("Negative: unknown creator prevents membership and persistence calls", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
repo.usersExist = func(_ context.Context, _ []uuid.UUID) (bool, error) {
return false, nil
}
svc := newStubService(t, repo)
actor := uuid.New()
input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: "Team", MemberIDs: []string{actor.String()}}
// Act.
got, err := svc.ChatsCreate(t.Context(), input)
// Assert.
if !errors.Is(err, service.ErrUnauthorized) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v", got, err)
}
})
}
// TestService_ChatsCreate_InvalidInput rejects malformed chat fields before checking member existence.
func TestService_ChatsCreate_InvalidInput(t *testing.T) {
actor := uuid.New()
cases := []struct {
name string
title string
members []string
}{
{"Negative: empty member set", "Team", nil},
{"Negative: duplicate member", "Team", []string{actor.String(), actor.String()}},
{"Negative: caller omitted", "Team", []string{uuid.NewString()}},
{"Negative: malformed member ID", "Team", []string{actor.String(), "invalid"}},
{"Negative: blank title", " ", []string{actor.String()}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
calls := 0
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
calls++
if calls != 1 || !slices.Equal(ids, []uuid.UUID{actor}) {
t.Fatalf("unexpected membership check: %v", ids)
}
return true, nil
}
svc := newStubService(t, repo)
input := dto.ChatsCreateRequestDTO{UserID: actor.String(), Title: tc.title, MemberIDs: tc.members}
// Act.
got, err := svc.ChatsCreate(t.Context(), input)
// Assert.
if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v", got, err)
}
if calls != 1 {
t.Fatalf("identity checks=%d, want=1", calls)
}
})
}
}
// TestService_ChatsGetByID authorizes the requested chat and conceals inaccessible records.
func TestService_ChatsGetByID(t *testing.T) {
t.Run("Positive: member receives the complete chat", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor := uuid.New()
expected := dto.ChatDTO{ID: uuid.New(), Title: "Team", MemberIDs: []uuid.UUID{actor}, CreatedAt: time.Unix(1700000000, 0).UTC()}
allowChatAccess(repo, actor, expected)
svc := newStubService(t, repo)
// Act.
got, err := svc.ChatsGetByID(t.Context(), actor.String(), expected.ID.String())
// Assert.
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, expected) {
t.Fatalf("chat=%+v, want=%+v", got, expected)
}
})
t.Run("Negative: outsider receives no chat details", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor := uuid.New()
chat := dto.ChatDTO{ID: uuid.New(), Title: "Private", MemberIDs: []uuid.UUID{uuid.New()}}
allowChatAccess(repo, actor, chat)
svc := newStubService(t, repo)
// Act.
got, err := svc.ChatsGetByID(t.Context(), actor.String(), chat.ID.String())
// Assert.
if !errors.Is(err, service.ErrNotFound) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("private chat exposed: %+v, error=%v", got, err)
}
})
t.Run("Negative: malformed chat ID skips chat lookup", func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor := uuid.New()
allowIdentity(repo, actor)
svc := newStubService(t, repo)
// Act.
got, err := svc.ChatsGetByID(t.Context(), actor.String(), "invalid")
// Assert.
if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v", got, err)
}
})
t.Run("Negative: malformed identity skips storage", func(t *testing.T) {
// Arrange.
svc := newStubService(t, newStoreStub(t))
// Act.
got, err := svc.ChatsGetByID(t.Context(), "invalid", uuid.NewString())
// Assert.
if !errors.Is(err, service.ErrUnauthorized) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v", got, err)
}
})
}
// TestService_ChatsGetByID_StoreErrors translates missing records and retains unexpected failures.
func TestService_ChatsGetByID_StoreErrors(t *testing.T) {
failure := errors.New("chat lookup unavailable")
cases := []struct {
name string
cause error
want error
}{
{"Negative: absent chat", store.ErrNotFound, service.ErrNotFound},
{"Negative: storage unavailable", failure, failure},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
repo := newStoreStub(t)
actor := uuid.New()
allowIdentity(repo, actor)
repo.chat = func(_ context.Context, _ uuid.UUID) (dto.ChatDTO, error) {
return dto.ChatDTO{}, tc.cause
}
svc := newStubService(t, repo)
// Act.
got, err := svc.ChatsGetByID(t.Context(), actor.String(), uuid.NewString())
// Assert.
if !errors.Is(err, tc.want) || !reflect.DeepEqual(got, dto.ChatDTO{}) {
t.Fatalf("result=%+v, error=%v, want=%v", got, err, tc.want)
}
})
}
}