forked from mtusi2026/mtusi2026lab2
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"mtusi2026lab2/internal/model"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestStrictJSON(t *testing.T) {
|
|
for _, body := range []string{`{"text":"hello"} garbage`, `{"text":"hello"}{"text":"again"}`} {
|
|
req := httptest.NewRequest("POST", "/", strings.NewReader(body))
|
|
var v createMessageRequest
|
|
if err := decodeOneObject(req, &v); err == nil {
|
|
t.Fatalf("accepted trailing JSON: %q", body)
|
|
}
|
|
}
|
|
}
|
|
func TestMessageCursorBoundToChat(t *testing.T) {
|
|
chatA, chatB := uuid.New(), uuid.New()
|
|
cursor := encodeMessageCursor(model.Message{ID: uuid.New(), ChatID: chatA, CreatedAt: time.Now().UTC()})
|
|
if _, _, err := decodeMessageCursor(cursor, chatA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := decodeMessageCursor(cursor, chatB); err == nil {
|
|
t.Fatal("accepted cursor for another chat")
|
|
}
|
|
}
|
|
func TestChatCursorBoundToUser(t *testing.T) {
|
|
a, b := uuid.New(), uuid.New()
|
|
cursor := encodeChatCursor(a, model.Chat{ID: uuid.New(), CreatedAt: time.Now().UTC()})
|
|
if _, _, err := decodeChatCursor(cursor, a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := decodeChatCursor(cursor, b); err == nil {
|
|
t.Fatal("accepted cursor for another user")
|
|
}
|
|
}
|
|
func TestEmptyLoginBody(t *testing.T) {
|
|
req := httptest.NewRequest("POST", "/", strings.NewReader(`{"ignored":true}`))
|
|
if err := requireEmptyBody(req); err == nil {
|
|
t.Fatal("accepted login body")
|
|
}
|
|
}
|