232 lines
7.4 KiB
Go
232 lines
7.4 KiB
Go
package production
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/cursor"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
|
|
)
|
|
|
|
// TestService_MessagesGetPage validates access and pagination before issuing a typed store query.
|
|
func TestService_MessagesGetPage(t *testing.T) {
|
|
t.Run("Positive: continuation uses scope, position and lookahead", func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}}
|
|
allowChatAccess(repo, actor, chat)
|
|
|
|
previous := cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()}
|
|
records := []dto.MessageDTO{
|
|
{ID: uuid.New(), CreatedAt: time.Unix(1700000001, 0).UTC()},
|
|
{ID: uuid.New(), CreatedAt: time.Unix(1700000002, 0).UTC()},
|
|
{ID: uuid.New(), CreatedAt: time.Unix(1700000003, 0).UTC()},
|
|
}
|
|
calls := 0
|
|
repo.messages = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) {
|
|
calls++
|
|
expected := dto.PositionDTO{ID: previous.ID, CreatedAt: previous.CreatedAt}
|
|
if id != chat.ID || position != expected || limit != 3 {
|
|
t.Fatalf("wrong page query: id=%s, position=%v, limit=%d", id, position, limit)
|
|
}
|
|
return records, nil
|
|
}
|
|
svc := newStubService(t, repo)
|
|
resource := "messages:" + chat.ID.String()
|
|
token, err := svc.cursors.Encode(resource, previous)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Limit: "2", Cursor: token}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 1 || !reflect.DeepEqual(got.Items, records[:2]) || got.NextCursor == nil {
|
|
t.Fatalf("calls=%d, page=%+v", calls, got)
|
|
}
|
|
position, err := svc.cursors.Decode(*got.NextCursor, resource)
|
|
expected := cursor.Position{ID: records[1].ID, CreatedAt: records[1].CreatedAt}
|
|
if err != nil || position != expected {
|
|
t.Fatalf("next position=%v, want=%v, error=%v", position, expected, err)
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Positive: empty history uses default limit and no continuation", func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}}
|
|
allowChatAccess(repo, actor, chat)
|
|
|
|
calls := 0
|
|
repo.messages = func(_ context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) {
|
|
calls++
|
|
if id != chat.ID || position != (dto.PositionDTO{}) || limit != 51 {
|
|
t.Fatalf("wrong initial page query: id=%s, position=%v, limit=%d", id, position, limit)
|
|
}
|
|
return []dto.MessageDTO{}, nil
|
|
}
|
|
svc := newStubService(t, repo)
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String()}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 1 || got.Items == nil || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("calls=%d, page=%+v", calls, got)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: cursor for another resource skips page lookup", func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}}
|
|
allowChatAccess(repo, actor, chat)
|
|
|
|
svc := newStubService(t, repo)
|
|
token, err := svc.cursors.Encode("another-resource", cursor.Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Cursor: token}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("page=%+v, error=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: codec failure skips page lookup", func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}}
|
|
allowChatAccess(repo, actor, chat)
|
|
|
|
failure := errors.New("cursor unavailable")
|
|
svc := newStubService(t, repo)
|
|
svc.cursors = failingCursor{failure: failure}
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Cursor: "token"}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("page=%+v, error=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: page storage fails", func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}}
|
|
allowChatAccess(repo, actor, chat)
|
|
|
|
failure := errors.New("page unavailable")
|
|
repo.messages = func(_ context.Context, _ uuid.UUID, _ dto.PositionDTO, _ int) ([]dto.MessageDTO, error) {
|
|
return nil, failure
|
|
}
|
|
svc := newStubService(t, repo)
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String()}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if !errors.Is(err, failure) || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("page=%+v, error=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: malformed identity skips all storage", func(t *testing.T) {
|
|
// Arrange.
|
|
svc := newStubService(t, newStoreStub(t))
|
|
input := dto.PageRequestDTO{UserID: "invalid", ChatID: uuid.NewString()}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if !errors.Is(err, service.ErrUnauthorized) || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("page=%+v, error=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: outsider cannot read message history", func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{uuid.New()}}
|
|
allowChatAccess(repo, actor, chat)
|
|
svc := newStubService(t, repo)
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String()}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if !errors.Is(err, service.ErrNotFound) || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("page=%+v, error=%v", got, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestService_MessagesGetPage_InvalidLimit rejects invalid limits before reading stored pages.
|
|
func TestService_MessagesGetPage_InvalidLimit(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
limit string
|
|
}{
|
|
{"Negative: zero limit", "0"},
|
|
{"Negative: negative limit", "-1"},
|
|
{"Negative: limit above maximum", "101"},
|
|
{"Negative: nonnumeric limit", "text"},
|
|
{"Negative: fractional limit", "1.5"},
|
|
{"Negative: integer overflow", "99999999999999999999999999"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Arrange.
|
|
repo := newStoreStub(t)
|
|
actor := uuid.New()
|
|
chat := dto.ChatDTO{ID: uuid.New(), MemberIDs: []uuid.UUID{actor}}
|
|
allowChatAccess(repo, actor, chat)
|
|
|
|
svc := newStubService(t, repo)
|
|
input := dto.PageRequestDTO{UserID: actor.String(), ChatID: chat.ID.String(), Limit: tc.limit}
|
|
|
|
// Act.
|
|
got, err := svc.MessagesGetPage(t.Context(), input)
|
|
|
|
// Assert.
|
|
if !errors.Is(err, service.ErrInvalid) || len(got.Items) != 0 || got.NextCursor != nil {
|
|
t.Fatalf("page=%+v, error=%v", got, err)
|
|
}
|
|
})
|
|
}
|
|
}
|