forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
package production
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
|
||||
)
|
||||
|
||||
// TestService_MessagesCreate authorizes the sender before validating and persisting the message.
|
||||
func TestService_MessagesCreate(t *testing.T) {
|
||||
t.Run("Positive: authenticated sender and original text persisted", 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)
|
||||
var persisted dto.MessageDTO
|
||||
repo.createMessage = func(_ context.Context, input dto.MessageDTO) (dto.MessageDTO, error) {
|
||||
persisted = input
|
||||
input.CreatedAt = time.Unix(1700000000, 0).UTC()
|
||||
return input, nil
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: " Original 👋 "}
|
||||
|
||||
// Act.
|
||||
got, err := svc.MessagesCreate(t.Context(), input)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if persisted.ID == uuid.Nil || persisted.SenderID != actor || persisted.ChatID != chat.ID || persisted.Text != input.Text {
|
||||
t.Fatalf("wrong persistence input: %+v", persisted)
|
||||
}
|
||||
if got.ID != persisted.ID || got.SenderID != actor || got.ChatID != chat.ID || got.Text != input.Text || got.CreatedAt.IsZero() {
|
||||
t.Fatalf("wrong acknowledgement: %+v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: outsider cannot persist messages", 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.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: "Valid text"}
|
||||
|
||||
// Act.
|
||||
got, err := svc.MessagesCreate(t.Context(), input)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrNotFound) || !reflect.DeepEqual(got, dto.MessageDTO{}) {
|
||||
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))
|
||||
input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: "invalid", ChatID: uuid.NewString(), Text: "text"}
|
||||
|
||||
// Act.
|
||||
got, err := svc.MessagesCreate(t.Context(), input)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrUnauthorized) || !reflect.DeepEqual(got, dto.MessageDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestService_MessagesCreate_InvalidText refuses invalid text without attempting persistence.
|
||||
func TestService_MessagesCreate_InvalidText(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
text string
|
||||
}{
|
||||
{"Negative: empty text", ""},
|
||||
{"Negative: whitespace only", " \t"},
|
||||
{"Negative: rune limit exceeded", strings.Repeat("界", 4001)},
|
||||
}
|
||||
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.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: tc.text}
|
||||
|
||||
// Act.
|
||||
got, err := svc.MessagesCreate(t.Context(), input)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrInvalid) || !reflect.DeepEqual(got, dto.MessageDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestService_MessagesCreate_StoreErrors propagates persistence failures without acknowledging a record.
|
||||
func TestService_MessagesCreate_StoreErrors(t *testing.T) {
|
||||
failure := errors.New("write unavailable")
|
||||
cases := []struct {
|
||||
name string
|
||||
cause error
|
||||
want error
|
||||
}{
|
||||
{"Negative: capacity exhausted", store.ErrCapacity, service.ErrCapacity},
|
||||
{"Negative: chat disappeared", store.ErrNotFound, service.ErrNotFound},
|
||||
{"Negative: unexpected write failure", failure, failure},
|
||||
}
|
||||
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)
|
||||
repo.createMessage = func(_ context.Context, _ dto.MessageDTO) (dto.MessageDTO, error) {
|
||||
return dto.MessageDTO{}, tc.cause
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
input := dto.MessagesCreateRequestDTO{IdempotencyKey: "fixture-key", UserID: actor.String(), ChatID: chat.ID.String(), Text: "Valid text"}
|
||||
|
||||
// Act.
|
||||
got, err := svc.MessagesCreate(t.Context(), input)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, tc.want) || !reflect.DeepEqual(got, dto.MessageDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v, want=%v", got, err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestIdempotencyKeyValidation checks the exact contract alphabet and length independently.
|
||||
func TestIdempotencyKeyValidation(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
key string
|
||||
want bool
|
||||
}{
|
||||
{name: "missing", key: "", want: false},
|
||||
{name: "allowed alphabet", key: "aZ09._:-", want: true},
|
||||
{name: "space", key: "a b", want: false},
|
||||
{name: "unicode", key: "ключ", want: false},
|
||||
{name: "maximum", key: strings.Repeat("a", 128), want: true},
|
||||
{name: "too long", key: strings.Repeat("a", 129), want: false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := validIdempotencyKey(test.key); got != test.want {
|
||||
t.Fatalf("valid=%v want=%v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user