forked from mtusi2026/mtusi2026lab3
refactor: разделил приложение на domain application adapters и HTTP DTO
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab3/internal/domain"
|
||||
)
|
||||
|
||||
const cursorTestKey = "cursor-test-key-with-more-than-thirty-two-bytes"
|
||||
|
||||
func TestCursorCodecRoundTrip(t *testing.T) {
|
||||
codec, err := NewCursorCodec(cursorTestKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := domain.MessageCursor{
|
||||
CreatedAt: time.Date(2026, 10, 18, 12, 30, 0, 123, time.UTC),
|
||||
MessageID: "42eea32e-d9b5-4bc6-b4fc-94e9b0477adf",
|
||||
ChatID: "9fa3f49c-e62e-4209-8414-d6d5f69805bf",
|
||||
}
|
||||
token, err := codec.EncodeMessage(expected)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, err := codec.DecodeMessage(token, expected.ChatID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if *actual != expected {
|
||||
t.Fatalf("cursor changed after round trip: got %#v, want %#v", *actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorCodecRejectsTamperingAndWrongScope(t *testing.T) {
|
||||
codec, err := NewCursorCodec(cursorTestKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := codec.EncodeChat(domain.ChatCursor{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ChatID: "42eea32e-d9b5-4bc6-b4fc-94e9b0477adf",
|
||||
UserID: "9fa3f49c-e62e-4209-8414-d6d5f69805bf",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
replacement := "A"
|
||||
if token[len(token)-1:] == replacement {
|
||||
replacement = "B"
|
||||
}
|
||||
tampered := token[:len(token)-1] + replacement
|
||||
if _, err = codec.DecodeChat(tampered, "9fa3f49c-e62e-4209-8414-d6d5f69805bf"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("tampered cursor: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
if _, err = codec.DecodeChat(token, "e6ca4af0-69ed-4784-8ce0-ee035299fe08"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("wrong scope: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
if _, err = codec.DecodeMessage(token, "9fa3f49c-e62e-4209-8414-d6d5f69805bf"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("wrong cursor kind: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorCodecRequiresStrongSharedKey(t *testing.T) {
|
||||
if _, err := NewCursorCodec("too-short"); err == nil {
|
||||
t.Fatal("expected short cursor signing key to be rejected")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user