forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,289 @@
|
||||
package production
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"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_UsersCreate validates registration before persistence and preserves storage failures.
|
||||
func TestService_UsersCreate(t *testing.T) {
|
||||
t.Run("Positive: validates and persists the original profile", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
expected := dto.UserDTO{Username: "alice", DisplayName: "Алиса 👋", CreatedAt: time.Unix(1700000000, 0).UTC()}
|
||||
calls := 0
|
||||
repo.createUser = func(_ context.Context, input dto.UserDTO) (dto.UserDTO, error) {
|
||||
calls++
|
||||
if input.ID == uuid.Nil || input.Username != expected.Username || input.DisplayName != expected.DisplayName {
|
||||
t.Fatalf("wrong persistence input: %+v", input)
|
||||
}
|
||||
expected.ID = input.ID
|
||||
return expected, nil
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
input := dto.UsersCreateRequestDTO{Username: expected.Username, DisplayName: expected.DisplayName}
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersCreate(t.Context(), input)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 || got != expected {
|
||||
t.Fatalf("calls=%d, got=%+v, want=%+v", calls, got, expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestService_UsersCreate_InvalidInput refuses malformed registrations without calling storage.
|
||||
func TestService_UsersCreate_InvalidInput(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input dto.UsersCreateRequestDTO
|
||||
}{
|
||||
{"Negative: short username", dto.UsersCreateRequestDTO{Username: "a", DisplayName: "Alice"}},
|
||||
{"Negative: uppercase username", dto.UsersCreateRequestDTO{Username: "Alice", DisplayName: "Alice"}},
|
||||
{"Negative: blank display name", dto.UsersCreateRequestDTO{Username: "alice", DisplayName: " "}},
|
||||
{"Negative: display name exceeds rune limit", dto.UsersCreateRequestDTO{Username: "alice", DisplayName: strings.Repeat("界", 101)}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Arrange: every store method fails immediately if called.
|
||||
svc := newStubService(t, newStoreStub(t))
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersCreate(t.Context(), tc.input)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrInvalid) || got != (dto.UserDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestService_UsersCreate_StoreErrors preserves the application meaning of persistence failures.
|
||||
func TestService_UsersCreate_StoreErrors(t *testing.T) {
|
||||
unexpected := errors.New("storage unavailable")
|
||||
cases := []struct {
|
||||
name string
|
||||
cause error
|
||||
want error
|
||||
}{
|
||||
{"Negative: duplicate username", store.ErrConflict, service.ErrConflict},
|
||||
{"Negative: capacity exhausted", store.ErrCapacity, service.ErrCapacity},
|
||||
{"Negative: unexpected failure", unexpected, unexpected},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
repo.createUser = func(_ context.Context, _ dto.UserDTO) (dto.UserDTO, error) {
|
||||
return dto.UserDTO{}, tc.cause
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersCreate(t.Context(), dto.UsersCreateRequestDTO{Username: "alice", DisplayName: "Alice"})
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, tc.want) || got != (dto.UserDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v, want=%v", got, err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestService_UsersGetMe loads only the requested profile and hides unknown identities.
|
||||
func TestService_UsersGetMe(t *testing.T) {
|
||||
t.Run("Positive: returns stored profile", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
expected := dto.UserDTO{ID: uuid.New(), Username: "alice", DisplayName: "Alice", CreatedAt: time.Unix(1700000000, 0).UTC()}
|
||||
calls := 0
|
||||
repo.user = func(_ context.Context, id uuid.UUID) (dto.UserDTO, error) {
|
||||
calls++
|
||||
if id != expected.ID {
|
||||
t.Fatalf("profile queried for %s instead of %s", id, expected.ID)
|
||||
}
|
||||
return expected, nil
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersGetMe(t.Context(), expected.ID.String())
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 || got != expected {
|
||||
t.Fatalf("calls=%d, got=%+v, want=%+v", calls, got, expected)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: unknown identity", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
repo.user = func(_ context.Context, _ uuid.UUID) (dto.UserDTO, error) {
|
||||
return dto.UserDTO{}, store.ErrNotFound
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
wantErr := service.ErrUnauthorized
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersGetMe(t.Context(), uuid.NewString())
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("error=%v, want=%v", err, wantErr)
|
||||
}
|
||||
if got != (dto.UserDTO{}) {
|
||||
t.Fatalf("failed operation returned a profile: %+v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: storage failure preserved", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
wantErr := errors.New("profile unavailable")
|
||||
repo.user = func(_ context.Context, _ uuid.UUID) (dto.UserDTO, error) {
|
||||
return dto.UserDTO{}, wantErr
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersGetMe(t.Context(), uuid.NewString())
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("error=%v, want=%v", err, wantErr)
|
||||
}
|
||||
if got != (dto.UserDTO{}) {
|
||||
t.Fatalf("failed operation returned a profile: %+v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestService_UsersGetMe_InvalidIdentity rejects malformed identifiers before any store call.
|
||||
func TestService_UsersGetMe_InvalidIdentity(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
raw string
|
||||
}{
|
||||
{"Negative: missing header", ""},
|
||||
{"Negative: short ID", "invalid"},
|
||||
{"Negative: non-UUID characters", strings.Repeat("z", 36)},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Arrange.
|
||||
svc := newStubService(t, newStoreStub(t))
|
||||
|
||||
// Act.
|
||||
got, err := svc.UsersGetMe(t.Context(), tc.raw)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrUnauthorized) || got != (dto.UserDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestService_UsersCheckIdentity uses the existence query and never loads a profile.
|
||||
func TestService_UsersCheckIdentity(t *testing.T) {
|
||||
t.Run("Positive: existence check without profile loading", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
id := uuid.New()
|
||||
calls := 0
|
||||
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
|
||||
calls++
|
||||
if !slices.Equal(ids, []uuid.UUID{id}) {
|
||||
t.Fatalf("queried IDs=%v", ids)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
|
||||
// Act.
|
||||
err := svc.UsersCheckIdentity(t.Context(), id.String())
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, nil) || calls != 1 {
|
||||
t.Fatalf("calls=%d, error=%v", calls, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: identity does not exist", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
id := uuid.New()
|
||||
calls := 0
|
||||
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
|
||||
calls++
|
||||
if !slices.Equal(ids, []uuid.UUID{id}) {
|
||||
t.Fatalf("queried IDs=%v", ids)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
|
||||
// Act.
|
||||
err := svc.UsersCheckIdentity(t.Context(), id.String())
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrUnauthorized) || calls != 1 {
|
||||
t.Fatalf("calls=%d, error=%v", calls, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: existence lookup fails", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newStoreStub(t)
|
||||
id := uuid.New()
|
||||
calls := 0
|
||||
failure := errors.New("identity unavailable")
|
||||
repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) {
|
||||
calls++
|
||||
if !slices.Equal(ids, []uuid.UUID{id}) {
|
||||
t.Fatalf("queried IDs=%v", ids)
|
||||
}
|
||||
return false, failure
|
||||
}
|
||||
svc := newStubService(t, repo)
|
||||
|
||||
// Act.
|
||||
err := svc.UsersCheckIdentity(t.Context(), id.String())
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, failure) || calls != 1 {
|
||||
t.Fatalf("calls=%d, error=%v", calls, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: malformed identity skips storage", func(t *testing.T) {
|
||||
// Arrange.
|
||||
svc := newStubService(t, newStoreStub(t))
|
||||
|
||||
// Act.
|
||||
err := svc.UsersCheckIdentity(t.Context(), "invalid")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, service.ErrUnauthorized) {
|
||||
t.Fatalf("error=%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user